1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
30 var robotsTxtHandler = HandlerFunc(func(w ResponseWriter, r *Request) {
31 w.Header().Set("Last-Modified", "sometime")
32 fmt.Fprintf(w, "User-agent: go\nDisallow: /something/")
35 // pedanticReadAll works like ioutil.ReadAll but additionally
36 // verifies that r obeys the documented io.Reader contract.
37 func pedanticReadAll(r io.Reader) (b []byte, err error) {
42 if n == 0 && err == nil {
43 return nil, fmt.Errorf("Read: n=0 with err=nil")
45 b = append(b, buf[:n]...)
48 if n != 0 || err != io.EOF {
49 return nil, fmt.Errorf("Read: n=%d err=%#v after EOF", n, err)
59 type chanWriter chan string
61 func (w chanWriter) Write(p []byte) (n int, err error) {
66 func TestClient(t *testing.T) {
68 ts := httptest.NewServer(robotsTxtHandler)
74 b, err = pedanticReadAll(r.Body)
79 } else if s := string(b); !strings.HasPrefix(s, "User-agent:") {
80 t.Errorf("Incorrect page body (did not begin with User-agent): %q", s)
84 func TestClientHead_h1(t *testing.T) { testClientHead(t, h1Mode) }
85 func TestClientHead_h2(t *testing.T) { testClientHead(t, h2Mode) }
87 func testClientHead(t *testing.T, h2 bool) {
89 cst := newClientServerTest(t, h2, robotsTxtHandler)
92 r, err := cst.c.Head(cst.ts.URL)
96 if _, ok := r.Header["Last-Modified"]; !ok {
97 t.Error("Last-Modified header not found.")
101 type recordingTransport struct {
105 func (t *recordingTransport) RoundTrip(req *Request) (resp *Response, err error) {
107 return nil, errors.New("dummy impl")
110 func TestGetRequestFormat(t *testing.T) {
112 tr := &recordingTransport{}
113 client := &Client{Transport: tr}
114 url := "http://dummy.faketld/"
115 client.Get(url) // Note: doesn't hit network
116 if tr.req.Method != "GET" {
117 t.Errorf("expected method %q; got %q", "GET", tr.req.Method)
119 if tr.req.URL.String() != url {
120 t.Errorf("expected URL %q; got %q", url, tr.req.URL.String())
122 if tr.req.Header == nil {
123 t.Errorf("expected non-nil request Header")
127 func TestPostRequestFormat(t *testing.T) {
129 tr := &recordingTransport{}
130 client := &Client{Transport: tr}
132 url := "http://dummy.faketld/"
133 json := `{"key":"value"}`
134 b := strings.NewReader(json)
135 client.Post(url, "application/json", b) // Note: doesn't hit network
137 if tr.req.Method != "POST" {
138 t.Errorf("got method %q, want %q", tr.req.Method, "POST")
140 if tr.req.URL.String() != url {
141 t.Errorf("got URL %q, want %q", tr.req.URL.String(), url)
143 if tr.req.Header == nil {
144 t.Fatalf("expected non-nil request Header")
147 t.Error("got Close true, want false")
149 if g, e := tr.req.ContentLength, int64(len(json)); g != e {
150 t.Errorf("got ContentLength %d, want %d", g, e)
154 func TestPostFormRequestFormat(t *testing.T) {
156 tr := &recordingTransport{}
157 client := &Client{Transport: tr}
159 urlStr := "http://dummy.faketld/"
160 form := make(url.Values)
161 form.Set("foo", "bar")
162 form.Add("foo", "bar2")
163 form.Set("bar", "baz")
164 client.PostForm(urlStr, form) // Note: doesn't hit network
166 if tr.req.Method != "POST" {
167 t.Errorf("got method %q, want %q", tr.req.Method, "POST")
169 if tr.req.URL.String() != urlStr {
170 t.Errorf("got URL %q, want %q", tr.req.URL.String(), urlStr)
172 if tr.req.Header == nil {
173 t.Fatalf("expected non-nil request Header")
175 if g, e := tr.req.Header.Get("Content-Type"), "application/x-www-form-urlencoded"; g != e {
176 t.Errorf("got Content-Type %q, want %q", g, e)
179 t.Error("got Close true, want false")
181 // Depending on map iteration, body can be either of these.
182 expectedBody := "foo=bar&foo=bar2&bar=baz"
183 expectedBody1 := "bar=baz&foo=bar&foo=bar2"
184 if g, e := tr.req.ContentLength, int64(len(expectedBody)); g != e {
185 t.Errorf("got ContentLength %d, want %d", g, e)
187 bodyb, err := ioutil.ReadAll(tr.req.Body)
189 t.Fatalf("ReadAll on req.Body: %v", err)
191 if g := string(bodyb); g != expectedBody && g != expectedBody1 {
192 t.Errorf("got body %q, want %q or %q", g, expectedBody, expectedBody1)
196 func TestClientRedirects(t *testing.T) {
198 var ts *httptest.Server
199 ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
200 n, _ := strconv.Atoi(r.FormValue("n"))
201 // Test Referer header. (7 is arbitrary position to test at)
203 if g, e := r.Referer(), ts.URL+"/?n=6"; e != g {
204 t.Errorf("on request ?n=7, expected referer of %q; got %q", e, g)
208 Redirect(w, r, fmt.Sprintf("/?n=%d", n+1), StatusFound)
211 fmt.Fprintf(w, "n=%d", n)
216 _, err := c.Get(ts.URL)
217 if e, g := "Get /?n=10: stopped after 10 redirects", fmt.Sprintf("%v", err); e != g {
218 t.Errorf("with default client Get, expected error %q, got %q", e, g)
221 // HEAD request should also have the ability to follow redirects.
222 _, err = c.Head(ts.URL)
223 if e, g := "Head /?n=10: stopped after 10 redirects", fmt.Sprintf("%v", err); e != g {
224 t.Errorf("with default client Head, expected error %q, got %q", e, g)
227 // Do should also follow redirects.
228 greq, _ := NewRequest("GET", ts.URL, nil)
230 if e, g := "Get /?n=10: stopped after 10 redirects", fmt.Sprintf("%v", err); e != g {
231 t.Errorf("with default client Do, expected error %q, got %q", e, g)
234 // Requests with an empty Method should also redirect (Issue 12705)
237 if e, g := "Get /?n=10: stopped after 10 redirects", fmt.Sprintf("%v", err); e != g {
238 t.Errorf("with default client Do and empty Method, expected error %q, got %q", e, g)
242 var lastVia []*Request
243 c = &Client{CheckRedirect: func(_ *Request, via []*Request) error {
247 res, err := c.Get(ts.URL)
249 t.Fatalf("Get error: %v", err)
252 finalUrl := res.Request.URL.String()
253 if e, g := "<nil>", fmt.Sprintf("%v", err); e != g {
254 t.Errorf("with custom client, expected error %q, got %q", e, g)
256 if !strings.HasSuffix(finalUrl, "/?n=15") {
257 t.Errorf("expected final url to end in /?n=15; got url %q", finalUrl)
259 if e, g := 15, len(lastVia); e != g {
260 t.Errorf("expected lastVia to have contained %d elements; got %d", e, g)
263 checkErr = errors.New("no redirects allowed")
264 res, err = c.Get(ts.URL)
265 if urlError, ok := err.(*url.Error); !ok || urlError.Err != checkErr {
266 t.Errorf("with redirects forbidden, expected a *url.Error with our 'no redirects allowed' error inside; got %#v (%q)", err, err)
269 t.Fatalf("Expected a non-nil Response on CheckRedirect failure (https://golang.org/issue/3795)")
272 if res.Header.Get("Location") == "" {
273 t.Errorf("no Location header in Response")
277 func TestPostRedirects(t *testing.T) {
283 var ts *httptest.Server
284 ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
286 fmt.Fprintf(&log.Buffer, "%s %s ", r.Method, r.RequestURI)
288 if v := r.URL.Query().Get("code"); v != "" {
289 code, _ := strconv.Atoi(v)
291 w.Header().Set("Location", ts.URL)
299 want int // response code
307 for _, tt := range tests {
308 res, err := Post(ts.URL+tt.suffix, "text/plain", strings.NewReader("Some content"))
312 if res.StatusCode != tt.want {
313 t.Errorf("POST %s: status code = %d; want %d", tt.suffix, res.StatusCode, tt.want)
319 want := "POST / POST /?code=301 POST /?code=302 GET / POST /?code=303 GET / POST /?code=404 "
321 t.Errorf("Log differs.\n Got: %q\nWant: %q", got, want)
325 var expectedCookies = []*Cookie{
326 {Name: "ChocolateChip", Value: "tasty"},
327 {Name: "First", Value: "Hit"},
328 {Name: "Second", Value: "Hit"},
331 var echoCookiesRedirectHandler = HandlerFunc(func(w ResponseWriter, r *Request) {
332 for _, cookie := range r.Cookies() {
335 if r.URL.Path == "/" {
336 SetCookie(w, expectedCookies[1])
337 Redirect(w, r, "/second", StatusMovedPermanently)
339 SetCookie(w, expectedCookies[2])
340 w.Write([]byte("hello"))
344 func TestClientSendsCookieFromJar(t *testing.T) {
346 tr := &recordingTransport{}
347 client := &Client{Transport: tr}
348 client.Jar = &TestJar{perURL: make(map[string][]*Cookie)}
349 us := "http://dummy.faketld/"
350 u, _ := url.Parse(us)
351 client.Jar.SetCookies(u, expectedCookies)
353 client.Get(us) // Note: doesn't hit network
354 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
356 client.Head(us) // Note: doesn't hit network
357 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
359 client.Post(us, "text/plain", strings.NewReader("body")) // Note: doesn't hit network
360 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
362 client.PostForm(us, url.Values{}) // Note: doesn't hit network
363 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
365 req, _ := NewRequest("GET", us, nil)
366 client.Do(req) // Note: doesn't hit network
367 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
369 req, _ = NewRequest("POST", us, nil)
370 client.Do(req) // Note: doesn't hit network
371 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
374 // Just enough correctness for our redirect tests. Uses the URL.Host as the
375 // scope of all cookies.
376 type TestJar struct {
378 perURL map[string][]*Cookie
381 func (j *TestJar) SetCookies(u *url.URL, cookies []*Cookie) {
385 j.perURL = make(map[string][]*Cookie)
387 j.perURL[u.Host] = cookies
390 func (j *TestJar) Cookies(u *url.URL) []*Cookie {
393 return j.perURL[u.Host]
396 func TestRedirectCookiesJar(t *testing.T) {
398 var ts *httptest.Server
399 ts = httptest.NewServer(echoCookiesRedirectHandler)
404 u, _ := url.Parse(ts.URL)
405 c.Jar.SetCookies(u, []*Cookie{expectedCookies[0]})
406 resp, err := c.Get(ts.URL)
408 t.Fatalf("Get: %v", err)
411 matchReturnedCookies(t, expectedCookies, resp.Cookies())
414 func matchReturnedCookies(t *testing.T, expected, given []*Cookie) {
415 if len(given) != len(expected) {
416 t.Logf("Received cookies: %v", given)
417 t.Errorf("Expected %d cookies, got %d", len(expected), len(given))
419 for _, ec := range expected {
421 for _, c := range given {
422 if ec.Name == c.Name && ec.Value == c.Value {
428 t.Errorf("Missing cookie %v", ec)
433 func TestJarCalls(t *testing.T) {
435 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
436 pathSuffix := r.RequestURI[1:]
437 if r.RequestURI == "/nosetcookie" {
438 return // don't set cookies for this path
440 SetCookie(w, &Cookie{Name: "name" + pathSuffix, Value: "val" + pathSuffix})
441 if r.RequestURI == "/" {
442 Redirect(w, r, "http://secondhost.fake/secondpath", 302)
446 jar := new(RecordingJar)
449 Transport: &Transport{
450 Dial: func(_ string, _ string) (net.Conn, error) {
451 return net.Dial("tcp", ts.Listener.Addr().String())
455 _, err := c.Get("http://firsthost.fake/")
459 _, err = c.Get("http://firsthost.fake/nosetcookie")
463 got := jar.log.String()
464 want := `Cookies("http://firsthost.fake/")
465 SetCookie("http://firsthost.fake/", [name=val])
466 Cookies("http://secondhost.fake/secondpath")
467 SetCookie("http://secondhost.fake/secondpath", [namesecondpath=valsecondpath])
468 Cookies("http://firsthost.fake/nosetcookie")
471 t.Errorf("Got Jar calls:\n%s\nWant:\n%s", got, want)
475 // RecordingJar keeps a log of calls made to it, without
476 // tracking any cookies.
477 type RecordingJar struct {
482 func (j *RecordingJar) SetCookies(u *url.URL, cookies []*Cookie) {
483 j.logf("SetCookie(%q, %v)\n", u, cookies)
486 func (j *RecordingJar) Cookies(u *url.URL) []*Cookie {
487 j.logf("Cookies(%q)\n", u)
491 func (j *RecordingJar) logf(format string, args ...interface{}) {
494 fmt.Fprintf(&j.log, format, args...)
497 func TestStreamingGet_h1(t *testing.T) { testStreamingGet(t, h1Mode) }
498 func TestStreamingGet_h2(t *testing.T) { testStreamingGet(t, h2Mode) }
500 func testStreamingGet(t *testing.T, h2 bool) {
502 say := make(chan string)
503 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
505 for str := range say {
513 res, err := c.Get(cst.ts.URL)
518 for _, str := range []string{"i", "am", "also", "known", "as", "comet"} {
520 n, err := io.ReadFull(res.Body, buf[0:len(str)])
522 t.Fatalf("ReadFull on %q: %v", str, err)
525 t.Fatalf("Receiving %q, only read %d bytes", str, n)
527 got := string(buf[0:n])
529 t.Fatalf("Expected %q, got %q", str, got)
533 _, err = io.ReadFull(res.Body, buf[0:1])
535 t.Fatalf("at end expected EOF, got %v", err)
539 type writeCountingConn struct {
544 func (c *writeCountingConn) Write(p []byte) (int, error) {
546 return c.Conn.Write(p)
549 // TestClientWrites verifies that client requests are buffered and we
550 // don't send a TCP packet per line of the http request + body.
551 func TestClientWrites(t *testing.T) {
553 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
558 dialer := func(netz string, addr string) (net.Conn, error) {
559 c, err := net.Dial(netz, addr)
561 c = &writeCountingConn{c, &writes}
565 c := &Client{Transport: &Transport{Dial: dialer}}
567 _, err := c.Get(ts.URL)
572 t.Errorf("Get request did %d Write calls, want 1", writes)
576 _, err = c.PostForm(ts.URL, url.Values{"foo": {"bar"}})
581 t.Errorf("Post request did %d Write calls, want 1", writes)
585 func TestClientInsecureTransport(t *testing.T) {
587 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
588 w.Write([]byte("Hello"))
590 errc := make(chanWriter, 10) // but only expecting 1
591 ts.Config.ErrorLog = log.New(errc, "", 0)
594 // TODO(bradfitz): add tests for skipping hostname checks too?
595 // would require a new cert for testing, and probably
596 // redundant with these tests.
597 for _, insecure := range []bool{true, false} {
599 TLSClientConfig: &tls.Config{
600 InsecureSkipVerify: insecure,
603 defer tr.CloseIdleConnections()
604 c := &Client{Transport: tr}
605 res, err := c.Get(ts.URL)
606 if (err == nil) != insecure {
607 t.Errorf("insecure=%v: got unexpected err=%v", insecure, err)
616 if !strings.Contains(v, "TLS handshake error") {
617 t.Errorf("expected an error log message containing 'TLS handshake error'; got %q", v)
619 case <-time.After(5 * time.Second):
620 t.Errorf("timeout waiting for logged error")
625 func TestClientErrorWithRequestURI(t *testing.T) {
627 req, _ := NewRequest("GET", "http://localhost:1234/", nil)
628 req.RequestURI = "/this/field/is/illegal/and/should/error/"
629 _, err := DefaultClient.Do(req)
631 t.Fatalf("expected an error")
633 if !strings.Contains(err.Error(), "RequestURI") {
634 t.Errorf("wanted error mentioning RequestURI; got error: %v", err)
638 func newTLSTransport(t *testing.T, ts *httptest.Server) *Transport {
639 certs := x509.NewCertPool()
640 for _, c := range ts.TLS.Certificates {
641 roots, err := x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
643 t.Fatalf("error parsing server's root cert: %v", err)
645 for _, root := range roots {
650 TLSClientConfig: &tls.Config{RootCAs: certs},
654 func TestClientWithCorrectTLSServerName(t *testing.T) {
657 const serverName = "example.com"
658 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
659 if r.TLS.ServerName != serverName {
660 t.Errorf("expected client to set ServerName %q, got: %q", serverName, r.TLS.ServerName)
665 trans := newTLSTransport(t, ts)
666 trans.TLSClientConfig.ServerName = serverName
667 c := &Client{Transport: trans}
668 if _, err := c.Get(ts.URL); err != nil {
669 t.Fatalf("expected successful TLS connection, got error: %v", err)
673 func TestClientWithIncorrectTLSServerName(t *testing.T) {
675 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
677 errc := make(chanWriter, 10) // but only expecting 1
678 ts.Config.ErrorLog = log.New(errc, "", 0)
680 trans := newTLSTransport(t, ts)
681 trans.TLSClientConfig.ServerName = "badserver"
682 c := &Client{Transport: trans}
683 _, err := c.Get(ts.URL)
685 t.Fatalf("expected an error")
687 if !strings.Contains(err.Error(), "127.0.0.1") || !strings.Contains(err.Error(), "badserver") {
688 t.Errorf("wanted error mentioning 127.0.0.1 and badserver; got error: %v", err)
692 if !strings.Contains(v, "TLS handshake error") {
693 t.Errorf("expected an error log message containing 'TLS handshake error'; got %q", v)
695 case <-time.After(5 * time.Second):
696 t.Errorf("timeout waiting for logged error")
700 // Test for golang.org/issue/5829; the Transport should respect TLSClientConfig.ServerName
703 // tls.Config.ServerName (non-empty, set to "example.com") takes
704 // precedence over "some-other-host.tld" which previously incorrectly
705 // took precedence. We don't actually connect to (or even resolve)
706 // "some-other-host.tld", though, because of the Transport.Dial hook.
708 // The httptest.Server has a cert with "example.com" as its name.
709 func TestTransportUsesTLSConfigServerName(t *testing.T) {
711 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
712 w.Write([]byte("Hello"))
716 tr := newTLSTransport(t, ts)
717 tr.TLSClientConfig.ServerName = "example.com" // one of httptest's Server cert names
718 tr.Dial = func(netw, addr string) (net.Conn, error) {
719 return net.Dial(netw, ts.Listener.Addr().String())
721 defer tr.CloseIdleConnections()
722 c := &Client{Transport: tr}
723 res, err := c.Get("https://some-other-host.tld/")
730 func TestResponseSetsTLSConnectionState(t *testing.T) {
732 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
733 w.Write([]byte("Hello"))
737 tr := newTLSTransport(t, ts)
738 tr.TLSClientConfig.CipherSuites = []uint16{tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA}
739 tr.Dial = func(netw, addr string) (net.Conn, error) {
740 return net.Dial(netw, ts.Listener.Addr().String())
742 defer tr.CloseIdleConnections()
743 c := &Client{Transport: tr}
744 res, err := c.Get("https://example.com/")
748 defer res.Body.Close()
750 t.Fatal("Response didn't set TLS Connection State.")
752 if got, want := res.TLS.CipherSuite, tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA; got != want {
753 t.Errorf("TLS Cipher Suite = %d; want %d", got, want)
757 // Check that an HTTPS client can interpret a particular TLS error
758 // to determine that the server is speaking HTTP.
759 // See golang.org/issue/11111.
760 func TestHTTPSClientDetectsHTTPServer(t *testing.T) {
762 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
765 _, err := Get(strings.Replace(ts.URL, "http", "https", 1))
766 if got := err.Error(); !strings.Contains(got, "HTTP response to HTTPS client") {
767 t.Fatalf("error = %q; want error indicating HTTP response to HTTPS request", got)
771 // Verify Response.ContentLength is populated. https://golang.org/issue/4126
772 func TestClientHeadContentLength_h1(t *testing.T) {
773 testClientHeadContentLength(t, h1Mode)
776 func TestClientHeadContentLength_h2(t *testing.T) {
777 testClientHeadContentLength(t, h2Mode)
780 func testClientHeadContentLength(t *testing.T, h2 bool) {
782 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
783 if v := r.FormValue("cl"); v != "" {
784 w.Header().Set("Content-Length", v)
796 for _, tt := range tests {
797 req, _ := NewRequest("HEAD", cst.ts.URL+tt.suffix, nil)
798 res, err := cst.c.Do(req)
802 if res.ContentLength != tt.want {
803 t.Errorf("Content-Length = %d; want %d", res.ContentLength, tt.want)
805 bs, err := ioutil.ReadAll(res.Body)
810 t.Errorf("Unexpected content: %q", bs)
815 func TestEmptyPasswordAuth(t *testing.T) {
818 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
819 auth := r.Header.Get("Authorization")
820 if strings.HasPrefix(auth, "Basic ") {
822 decoded, err := base64.StdEncoding.DecodeString(encoded)
826 expected := gopher + ":"
829 t.Errorf("Invalid Authorization header. Got %q, wanted %q", s, expected)
832 t.Errorf("Invalid auth %q", auth)
837 req, err := NewRequest("GET", ts.URL, nil)
841 req.URL.User = url.User(gopher)
842 resp, err := c.Do(req)
846 defer resp.Body.Close()
849 func TestBasicAuth(t *testing.T) {
851 tr := &recordingTransport{}
852 client := &Client{Transport: tr}
854 url := "http://My%20User:My%20Pass@dummy.faketld/"
855 expected := "My User:My Pass"
858 if tr.req.Method != "GET" {
859 t.Errorf("got method %q, want %q", tr.req.Method, "GET")
861 if tr.req.URL.String() != url {
862 t.Errorf("got URL %q, want %q", tr.req.URL.String(), url)
864 if tr.req.Header == nil {
865 t.Fatalf("expected non-nil request Header")
867 auth := tr.req.Header.Get("Authorization")
868 if strings.HasPrefix(auth, "Basic ") {
870 decoded, err := base64.StdEncoding.DecodeString(encoded)
876 t.Errorf("Invalid Authorization header. Got %q, wanted %q", s, expected)
879 t.Errorf("Invalid auth %q", auth)
883 func TestBasicAuthHeadersPreserved(t *testing.T) {
885 tr := &recordingTransport{}
886 client := &Client{Transport: tr}
888 // If Authorization header is provided, username in URL should not override it
889 url := "http://My%20User@dummy.faketld/"
890 req, err := NewRequest("GET", url, nil)
894 req.SetBasicAuth("My User", "My Pass")
895 expected := "My User:My Pass"
898 if tr.req.Method != "GET" {
899 t.Errorf("got method %q, want %q", tr.req.Method, "GET")
901 if tr.req.URL.String() != url {
902 t.Errorf("got URL %q, want %q", tr.req.URL.String(), url)
904 if tr.req.Header == nil {
905 t.Fatalf("expected non-nil request Header")
907 auth := tr.req.Header.Get("Authorization")
908 if strings.HasPrefix(auth, "Basic ") {
910 decoded, err := base64.StdEncoding.DecodeString(encoded)
916 t.Errorf("Invalid Authorization header. Got %q, wanted %q", s, expected)
919 t.Errorf("Invalid auth %q", auth)
924 func TestClientTimeout_h1(t *testing.T) { testClientTimeout(t, h1Mode) }
925 func TestClientTimeout_h2(t *testing.T) {
926 t.Skip("skipping in http2 mode; golang.org/issue/13540")
927 testClientTimeout(t, h2Mode)
930 func testClientTimeout(t *testing.T, h2 bool) {
932 t.Skip("skipping in short mode")
935 sawRoot := make(chan bool, 1)
936 sawSlow := make(chan bool, 1)
937 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
938 if r.URL.Path == "/" {
940 Redirect(w, r, "/slow", StatusFound)
943 if r.URL.Path == "/slow" {
944 w.Write([]byte("Hello"))
947 time.Sleep(2 * time.Second)
952 const timeout = 500 * time.Millisecond
953 cst.c.Timeout = timeout
955 res, err := cst.c.Get(cst.ts.URL)
964 t.Fatal("handler never got / request")
971 t.Fatal("handler never got /slow request")
974 errc := make(chan error, 1)
976 _, err := ioutil.ReadAll(res.Body)
981 const failTime = timeout * 2
985 t.Fatal("expected error from ReadAll")
987 ne, ok := err.(net.Error)
989 t.Errorf("error value from ReadAll was %T; expected some net.Error", err)
990 } else if !ne.Timeout() {
991 t.Errorf("net.Error.Timeout = false; want true")
993 if got := ne.Error(); !strings.Contains(got, "Client.Timeout exceeded") {
994 t.Errorf("error string = %q; missing timeout substring", got)
996 case <-time.After(failTime):
997 t.Errorf("timeout after %v waiting for timeout of %v", failTime, timeout)
1001 func TestClientTimeout_Headers_h1(t *testing.T) { testClientTimeout_Headers(t, h1Mode) }
1002 func TestClientTimeout_Headers_h2(t *testing.T) {
1003 t.Skip("skipping in http2 mode; golang.org/issue/13540")
1004 testClientTimeout_Headers(t, h2Mode)
1007 // Client.Timeout firing before getting to the body
1008 func testClientTimeout_Headers(t *testing.T, h2 bool) {
1009 if testing.Short() {
1010 t.Skip("skipping in short mode")
1013 donec := make(chan bool)
1014 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1018 // Note that we use a channel send here and not a close.
1019 // The race detector doesn't know that we're waiting for a timeout
1020 // and thinks that the waitgroup inside httptest.Server is added to concurrently
1021 // with us closing it. If we timed out immediately, we could close the testserver
1022 // before we entered the handler. We're not timing out immediately and there's
1023 // no way we would be done before we entered the handler, but the race detector
1024 // doesn't know this, so synchronize explicitly.
1025 defer func() { donec <- true }()
1027 cst.c.Timeout = 500 * time.Millisecond
1028 _, err := cst.c.Get(cst.ts.URL)
1030 t.Fatal("got response from Get; expected error")
1032 if _, ok := err.(*url.Error); !ok {
1033 t.Fatalf("Got error of type %T; want *url.Error", err)
1035 ne, ok := err.(net.Error)
1037 t.Fatalf("Got error of type %T; want some net.Error", err)
1040 t.Error("net.Error.Timeout = false; want true")
1042 if got := ne.Error(); !strings.Contains(got, "Client.Timeout exceeded") {
1043 t.Errorf("error string = %q; missing timeout substring", got)
1047 func TestClientRedirectEatsBody_h1(t *testing.T) { testClientRedirectEatsBody(t, h1Mode) }
1048 func TestClientRedirectEatsBody_h2(t *testing.T) { testClientRedirectEatsBody(t, h2Mode) }
1049 func testClientRedirectEatsBody(t *testing.T, h2 bool) {
1051 saw := make(chan string, 2)
1052 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1054 if r.URL.Path == "/" {
1055 Redirect(w, r, "/foo", StatusFound) // which includes a body
1060 res, err := cst.c.Get(cst.ts.URL)
1064 _, err = ioutil.ReadAll(res.Body)
1074 t.Fatal("server didn't see a request")
1079 case second = <-saw:
1081 t.Fatal("server didn't see a second request")
1084 if first != second {
1085 t.Fatal("server saw different client ports before & after the redirect")
1089 // eofReaderFunc is an io.Reader that runs itself, and then returns io.EOF.
1090 type eofReaderFunc func()
1092 func (f eofReaderFunc) Read(p []byte) (n int, err error) {
1097 func TestReferer(t *testing.T) {
1099 lastReq, newReq string // from -> to URLs
1103 {"http://gopher@test.com", "http://link.com", "http://test.com"},
1104 {"https://gopher@test.com", "https://link.com", "https://test.com"},
1106 // don't send a user and password:
1107 {"http://gopher:go@test.com", "http://link.com", "http://test.com"},
1108 {"https://gopher:go@test.com", "https://link.com", "https://test.com"},
1111 {"http://test.com", "http://link.com", "http://test.com"},
1112 {"https://test.com", "https://link.com", "https://test.com"},
1114 // https to http doesn't send a referer:
1115 {"https://test.com", "http://link.com", ""},
1116 {"https://gopher:go@test.com", "http://link.com", ""},
1118 for _, tt := range tests {
1119 l, err := url.Parse(tt.lastReq)
1123 n, err := url.Parse(tt.newReq)
1127 r := ExportRefererForURL(l, n)
1129 t.Errorf("refererForURL(%q, %q) = %q; want %q", tt.lastReq, tt.newReq, r, tt.want)