github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/misc/buildbot/builder/builder_test.go (about)

     1  package main
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"net/url"
     7  	"strings"
     8  	"testing"
     9  )
    10  
    11  func TestSendReport(t *testing.T) {
    12  	wants := []struct {
    13  		host       string
    14  		authHeader bool
    15  		path       string
    16  	}{
    17  		// TODO(wathiede): add https tests if needed.
    18  		{
    19  			host:       "http://HOST",
    20  			authHeader: false,
    21  			path:       reportPrefix,
    22  		},
    23  		{
    24  			host:       "http://user:pass@HOST",
    25  			authHeader: true,
    26  			path:       reportPrefix,
    27  		},
    28  		{
    29  			host:       "http://user:pass@HOST/",
    30  			authHeader: true,
    31  			path:       "/",
    32  		},
    33  		{
    34  			host:       "http://user:pass@HOST/other",
    35  			authHeader: true,
    36  			path:       "/other",
    37  		},
    38  	}
    39  
    40  	reqNum := 0
    41  	ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    42  		defer func() { reqNum++ }()
    43  		if reqNum > len(wants) {
    44  			t.Fatal("Only expected", len(wants), "requests, got", reqNum)
    45  		}
    46  		want := wants[reqNum]
    47  
    48  		gotAuthHeader := r.Header.Get("Authorization") != ""
    49  		if want.authHeader != gotAuthHeader {
    50  			if gotAuthHeader {
    51  				t.Error("Got unexpected Authorization header")
    52  			} else {
    53  				t.Error("Authorization header missing")
    54  			}
    55  		}
    56  
    57  		if r.URL.Path != want.path {
    58  			t.Error("Got path", r.URL.Path, "want", want.path)
    59  		}
    60  	}))
    61  	defer ts.Close()
    62  
    63  	testU, err := url.Parse(ts.URL)
    64  	if err != nil {
    65  		t.Fatal(err)
    66  	}
    67  	var hosts []string
    68  	for _, want := range wants {
    69  		u, err := url.Parse(want.host)
    70  		if err != nil {
    71  			t.Fatal(err)
    72  		}
    73  		u.Host = testU.Host
    74  		hosts = append(hosts, u.String())
    75  	}
    76  	// override --masterhosts.
    77  	*masterHosts = strings.Join(hosts, ",")
    78  	currentBiSuite = &biTestSuite{}
    79  
    80  	sendReport()
    81  
    82  	if reqNum != len(wants) {
    83  		t.Error("Expected", len(wants), "requests, only got", reqNum)
    84  	}
    85  }
    86  
    87  func TestMasterHostsReader(t *testing.T) {
    88  	datum := []struct {
    89  		body string
    90  		good bool
    91  		num  int
    92  	}{
    93  		{
    94  			body: "http://host1",
    95  			good: true,
    96  			num:  1,
    97  		},
    98  		{
    99  			body: "http://host1\n",
   100  			good: true,
   101  			num:  1,
   102  		},
   103  		{
   104  			body: "# Hello\nhttp://host1\n",
   105  			good: false,
   106  			num:  0,
   107  		},
   108  		{
   109  			body: "http://host1\nhttp://host2\n",
   110  			good: true,
   111  			num:  2,
   112  		},
   113  	}
   114  
   115  	for i, d := range datum {
   116  		hosts, err := masterHostsReader(strings.NewReader(d.body))
   117  		if d.good && err != nil {
   118  			t.Error(i, "Unexpected parse failure:", err)
   119  		}
   120  		if !d.good && err == nil {
   121  			t.Error(i, "Expected parse failure, but succeeded")
   122  		}
   123  
   124  		if len(hosts) != d.num {
   125  			t.Error(i, "Expected", d.num, "hosts, got", len(hosts), hosts)
   126  		}
   127  	}
   128  }