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.
33 var robotsTxtHandler = HandlerFunc(func(w ResponseWriter, r *Request) {
34 w.Header().Set("Last-Modified", "sometime")
35 fmt.Fprintf(w, "User-agent: go\nDisallow: /something/")
38 // pedanticReadAll works like ioutil.ReadAll but additionally
39 // verifies that r obeys the documented io.Reader contract.
40 func pedanticReadAll(r io.Reader) (b []byte, err error) {
45 if n == 0 && err == nil {
46 return nil, fmt.Errorf("Read: n=0 with err=nil")
48 b = append(b, buf[:n]...)
51 if n != 0 || err != io.EOF {
52 return nil, fmt.Errorf("Read: n=%d err=%#v after EOF", n, err)
62 type chanWriter chan string
64 func (w chanWriter) Write(p []byte) (n int, err error) {
69 func TestClient(t *testing.T) {
72 ts := httptest.NewServer(robotsTxtHandler)
75 c := &Client{Transport: &Transport{DisableKeepAlives: true}}
76 r, err := c.Get(ts.URL)
79 b, err = pedanticReadAll(r.Body)
84 } else if s := string(b); !strings.HasPrefix(s, "User-agent:") {
85 t.Errorf("Incorrect page body (did not begin with User-agent): %q", s)
89 func TestClientHead_h1(t *testing.T) { testClientHead(t, h1Mode) }
90 func TestClientHead_h2(t *testing.T) { testClientHead(t, h2Mode) }
92 func testClientHead(t *testing.T, h2 bool) {
94 cst := newClientServerTest(t, h2, robotsTxtHandler)
97 r, err := cst.c.Head(cst.ts.URL)
101 if _, ok := r.Header["Last-Modified"]; !ok {
102 t.Error("Last-Modified header not found.")
106 type recordingTransport struct {
110 func (t *recordingTransport) RoundTrip(req *Request) (resp *Response, err error) {
112 return nil, errors.New("dummy impl")
115 func TestGetRequestFormat(t *testing.T) {
118 tr := &recordingTransport{}
119 client := &Client{Transport: tr}
120 url := "http://dummy.faketld/"
121 client.Get(url) // Note: doesn't hit network
122 if tr.req.Method != "GET" {
123 t.Errorf("expected method %q; got %q", "GET", tr.req.Method)
125 if tr.req.URL.String() != url {
126 t.Errorf("expected URL %q; got %q", url, tr.req.URL.String())
128 if tr.req.Header == nil {
129 t.Errorf("expected non-nil request Header")
133 func TestPostRequestFormat(t *testing.T) {
135 tr := &recordingTransport{}
136 client := &Client{Transport: tr}
138 url := "http://dummy.faketld/"
139 json := `{"key":"value"}`
140 b := strings.NewReader(json)
141 client.Post(url, "application/json", b) // Note: doesn't hit network
143 if tr.req.Method != "POST" {
144 t.Errorf("got method %q, want %q", tr.req.Method, "POST")
146 if tr.req.URL.String() != url {
147 t.Errorf("got URL %q, want %q", tr.req.URL.String(), url)
149 if tr.req.Header == nil {
150 t.Fatalf("expected non-nil request Header")
153 t.Error("got Close true, want false")
155 if g, e := tr.req.ContentLength, int64(len(json)); g != e {
156 t.Errorf("got ContentLength %d, want %d", g, e)
160 func TestPostFormRequestFormat(t *testing.T) {
162 tr := &recordingTransport{}
163 client := &Client{Transport: tr}
165 urlStr := "http://dummy.faketld/"
166 form := make(url.Values)
167 form.Set("foo", "bar")
168 form.Add("foo", "bar2")
169 form.Set("bar", "baz")
170 client.PostForm(urlStr, form) // Note: doesn't hit network
172 if tr.req.Method != "POST" {
173 t.Errorf("got method %q, want %q", tr.req.Method, "POST")
175 if tr.req.URL.String() != urlStr {
176 t.Errorf("got URL %q, want %q", tr.req.URL.String(), urlStr)
178 if tr.req.Header == nil {
179 t.Fatalf("expected non-nil request Header")
181 if g, e := tr.req.Header.Get("Content-Type"), "application/x-www-form-urlencoded"; g != e {
182 t.Errorf("got Content-Type %q, want %q", g, e)
185 t.Error("got Close true, want false")
187 // Depending on map iteration, body can be either of these.
188 expectedBody := "foo=bar&foo=bar2&bar=baz"
189 expectedBody1 := "bar=baz&foo=bar&foo=bar2"
190 if g, e := tr.req.ContentLength, int64(len(expectedBody)); g != e {
191 t.Errorf("got ContentLength %d, want %d", g, e)
193 bodyb, err := ioutil.ReadAll(tr.req.Body)
195 t.Fatalf("ReadAll on req.Body: %v", err)
197 if g := string(bodyb); g != expectedBody && g != expectedBody1 {
198 t.Errorf("got body %q, want %q or %q", g, expectedBody, expectedBody1)
202 func TestClientRedirects(t *testing.T) {
205 var ts *httptest.Server
206 ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
207 n, _ := strconv.Atoi(r.FormValue("n"))
208 // Test Referer header. (7 is arbitrary position to test at)
210 if g, e := r.Referer(), ts.URL+"/?n=6"; e != g {
211 t.Errorf("on request ?n=7, expected referer of %q; got %q", e, g)
215 Redirect(w, r, fmt.Sprintf("/?n=%d", n+1), StatusTemporaryRedirect)
218 fmt.Fprintf(w, "n=%d", n)
223 defer tr.CloseIdleConnections()
225 c := &Client{Transport: tr}
226 _, err := c.Get(ts.URL)
227 if e, g := "Get /?n=10: stopped after 10 redirects", fmt.Sprintf("%v", err); e != g {
228 t.Errorf("with default client Get, expected error %q, got %q", e, g)
231 // HEAD request should also have the ability to follow redirects.
232 _, err = c.Head(ts.URL)
233 if e, g := "Head /?n=10: stopped after 10 redirects", fmt.Sprintf("%v", err); e != g {
234 t.Errorf("with default client Head, expected error %q, got %q", e, g)
237 // Do should also follow redirects.
238 greq, _ := NewRequest("GET", ts.URL, nil)
240 if e, g := "Get /?n=10: stopped after 10 redirects", fmt.Sprintf("%v", err); e != g {
241 t.Errorf("with default client Do, expected error %q, got %q", e, g)
244 // Requests with an empty Method should also redirect (Issue 12705)
247 if e, g := "Get /?n=10: stopped after 10 redirects", fmt.Sprintf("%v", err); e != g {
248 t.Errorf("with default client Do and empty Method, expected error %q, got %q", e, g)
252 var lastVia []*Request
254 c = &Client{CheckRedirect: func(req *Request, via []*Request) error {
259 res, err := c.Get(ts.URL)
261 t.Fatalf("Get error: %v", err)
264 finalUrl := res.Request.URL.String()
265 if e, g := "<nil>", fmt.Sprintf("%v", err); e != g {
266 t.Errorf("with custom client, expected error %q, got %q", e, g)
268 if !strings.HasSuffix(finalUrl, "/?n=15") {
269 t.Errorf("expected final url to end in /?n=15; got url %q", finalUrl)
271 if e, g := 15, len(lastVia); e != g {
272 t.Errorf("expected lastVia to have contained %d elements; got %d", e, g)
275 // Test that Request.Cancel is propagated between requests (Issue 14053)
276 creq, _ := NewRequest("HEAD", ts.URL, nil)
277 cancel := make(chan struct{})
279 if _, err := c.Do(creq); err != nil {
283 t.Fatal("didn't see redirect")
285 if lastReq.Cancel != cancel {
286 t.Errorf("expected lastReq to have the cancel channel set on the initial req")
289 checkErr = errors.New("no redirects allowed")
290 res, err = c.Get(ts.URL)
291 if urlError, ok := err.(*url.Error); !ok || urlError.Err != checkErr {
292 t.Errorf("with redirects forbidden, expected a *url.Error with our 'no redirects allowed' error inside; got %#v (%q)", err, err)
295 t.Fatalf("Expected a non-nil Response on CheckRedirect failure (https://golang.org/issue/3795)")
298 if res.Header.Get("Location") == "" {
299 t.Errorf("no Location header in Response")
303 func TestClientRedirectContext(t *testing.T) {
306 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
307 Redirect(w, r, "/", StatusTemporaryRedirect)
312 defer tr.CloseIdleConnections()
314 ctx, cancel := context.WithCancel(context.Background())
317 CheckRedirect: func(req *Request, via []*Request) error {
320 return errors.New("too many redirects")
325 req, _ := NewRequest("GET", ts.URL, nil)
326 req = req.WithContext(ctx)
328 ue, ok := err.(*url.Error)
330 t.Fatalf("got error %T; want *url.Error", err)
332 if ue.Err != context.Canceled {
333 t.Errorf("url.Error.Err = %v; want %v", ue.Err, context.Canceled)
337 type redirectTest struct {
339 want int // response code
343 func TestPostRedirects(t *testing.T) {
344 postRedirectTests := []redirectTest{
346 {"/?code=301&next=302", 200, "c301"},
347 {"/?code=302&next=302", 200, "c302"},
348 {"/?code=303&next=301", 200, "c303wc301"}, // Issue 9348
349 {"/?code=304", 304, "c304"},
350 {"/?code=305", 305, "c305"},
351 {"/?code=307&next=303,308,302", 200, "c307"},
352 {"/?code=308&next=302,301", 200, "c308"},
353 {"/?code=404", 404, "c404"},
356 wantSegments := []string{
358 `POST /?code=301&next=302 "c301"`,
359 `GET /?code=302 "c301"`,
361 `POST /?code=302&next=302 "c302"`,
362 `GET /?code=302 "c302"`,
364 `POST /?code=303&next=301 "c303wc301"`,
365 `GET /?code=301 "c303wc301"`,
367 `POST /?code=304 "c304"`,
368 `POST /?code=305 "c305"`,
369 `POST /?code=307&next=303,308,302 "c307"`,
370 `POST /?code=303&next=308,302 "c307"`,
371 `GET /?code=308&next=302 "c307"`,
372 `GET /?code=302 "c307"`,
374 `POST /?code=308&next=302,301 "c308"`,
375 `POST /?code=302&next=301 "c308"`,
376 `GET /?code=301 "c308"`,
378 `POST /?code=404 "c404"`,
380 want := strings.Join(wantSegments, "\n")
381 testRedirectsByMethod(t, "POST", postRedirectTests, want)
384 func TestDeleteRedirects(t *testing.T) {
385 deleteRedirectTests := []redirectTest{
387 {"/?code=301&next=302,308", 200, "c301"},
388 {"/?code=302&next=302", 200, "c302"},
389 {"/?code=303", 200, "c303"},
390 {"/?code=307&next=301,308,303,302,304", 304, "c307"},
391 {"/?code=308&next=307", 200, "c308"},
392 {"/?code=404", 404, "c404"},
395 wantSegments := []string{
397 `DELETE /?code=301&next=302,308 "c301"`,
398 `GET /?code=302&next=308 "c301"`,
399 `GET /?code=308 "c301"`,
401 `DELETE /?code=302&next=302 "c302"`,
402 `GET /?code=302 "c302"`,
404 `DELETE /?code=303 "c303"`,
406 `DELETE /?code=307&next=301,308,303,302,304 "c307"`,
407 `DELETE /?code=301&next=308,303,302,304 "c307"`,
408 `GET /?code=308&next=303,302,304 "c307"`,
409 `GET /?code=303&next=302,304 "c307"`,
410 `GET /?code=302&next=304 "c307"`,
411 `GET /?code=304 "c307"`,
412 `DELETE /?code=308&next=307 "c308"`,
413 `DELETE /?code=307 "c308"`,
415 `DELETE /?code=404 "c404"`,
417 want := strings.Join(wantSegments, "\n")
418 testRedirectsByMethod(t, "DELETE", deleteRedirectTests, want)
421 func testRedirectsByMethod(t *testing.T, method string, table []redirectTest, want string) {
427 var ts *httptest.Server
428 ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
430 slurp, _ := ioutil.ReadAll(r.Body)
431 fmt.Fprintf(&log.Buffer, "%s %s %q\n", r.Method, r.RequestURI, slurp)
433 urlQuery := r.URL.Query()
434 if v := urlQuery.Get("code"); v != "" {
436 if final := urlQuery.Get("next"); final != "" {
437 splits := strings.Split(final, ",")
438 first, rest := splits[0], splits[1:]
439 location = fmt.Sprintf("%s?code=%s", location, first)
441 location = fmt.Sprintf("%s&next=%s", location, strings.Join(rest, ","))
444 code, _ := strconv.Atoi(v)
446 w.Header().Set("Location", location)
453 for _, tt := range table {
454 content := tt.redirectBody
455 req, _ := NewRequest(method, ts.URL+tt.suffix, strings.NewReader(content))
456 req.GetBody = func() (io.ReadCloser, error) { return ioutil.NopCloser(strings.NewReader(content)), nil }
457 res, err := DefaultClient.Do(req)
462 if res.StatusCode != tt.want {
463 t.Errorf("POST %s: status code = %d; want %d", tt.suffix, res.StatusCode, tt.want)
470 got = strings.TrimSpace(got)
471 want = strings.TrimSpace(want)
474 t.Errorf("Log differs.\n Got:\n%s\nWant:\n%s\n", got, want)
478 func TestClientRedirectUseResponse(t *testing.T) {
481 const body = "Hello, world."
482 var ts *httptest.Server
483 ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
484 if strings.Contains(r.URL.Path, "/other") {
485 io.WriteString(w, "wrong body")
487 w.Header().Set("Location", ts.URL+"/other")
488 w.WriteHeader(StatusFound)
489 io.WriteString(w, body)
495 defer tr.CloseIdleConnections()
499 CheckRedirect: func(req *Request, via []*Request) error {
500 if req.Response == nil {
501 t.Error("expected non-nil Request.Response")
503 return ErrUseLastResponse
506 res, err := c.Get(ts.URL)
510 if res.StatusCode != StatusFound {
511 t.Errorf("status = %d; want %d", res.StatusCode, StatusFound)
513 defer res.Body.Close()
514 slurp, err := ioutil.ReadAll(res.Body)
518 if string(slurp) != body {
519 t.Errorf("body = %q; want %q", slurp, body)
523 // Issue 17773: don't follow a 308 (or 307) if the response doesn't
524 // have a Location header.
525 func TestClientRedirect308NoLocation(t *testing.T) {
528 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
529 w.Header().Set("Foo", "Bar")
533 res, err := Get(ts.URL)
538 if res.StatusCode != 308 {
539 t.Errorf("status = %d; want %d", res.StatusCode, 308)
541 if got := res.Header.Get("Foo"); got != "Bar" {
542 t.Errorf("Foo header = %q; want Bar", got)
546 // Don't follow a 307/308 if we can't resent the request body.
547 func TestClientRedirect308NoGetBody(t *testing.T) {
550 const fakeURL = "https://localhost:1234/" // won't be hit
551 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
552 w.Header().Set("Location", fakeURL)
556 req, err := NewRequest("POST", ts.URL, strings.NewReader("some body"))
560 req.GetBody = nil // so it can't rewind.
561 res, err := DefaultClient.Do(req)
566 if res.StatusCode != 308 {
567 t.Errorf("status = %d; want %d", res.StatusCode, 308)
569 if got := res.Header.Get("Location"); got != fakeURL {
570 t.Errorf("Location header = %q; want %q", got, fakeURL)
574 var expectedCookies = []*Cookie{
575 {Name: "ChocolateChip", Value: "tasty"},
576 {Name: "First", Value: "Hit"},
577 {Name: "Second", Value: "Hit"},
580 var echoCookiesRedirectHandler = HandlerFunc(func(w ResponseWriter, r *Request) {
581 for _, cookie := range r.Cookies() {
584 if r.URL.Path == "/" {
585 SetCookie(w, expectedCookies[1])
586 Redirect(w, r, "/second", StatusMovedPermanently)
588 SetCookie(w, expectedCookies[2])
589 w.Write([]byte("hello"))
593 func TestClientSendsCookieFromJar(t *testing.T) {
595 tr := &recordingTransport{}
596 client := &Client{Transport: tr}
597 client.Jar = &TestJar{perURL: make(map[string][]*Cookie)}
598 us := "http://dummy.faketld/"
599 u, _ := url.Parse(us)
600 client.Jar.SetCookies(u, expectedCookies)
602 client.Get(us) // Note: doesn't hit network
603 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
605 client.Head(us) // Note: doesn't hit network
606 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
608 client.Post(us, "text/plain", strings.NewReader("body")) // Note: doesn't hit network
609 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
611 client.PostForm(us, url.Values{}) // Note: doesn't hit network
612 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
614 req, _ := NewRequest("GET", us, nil)
615 client.Do(req) // Note: doesn't hit network
616 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
618 req, _ = NewRequest("POST", us, nil)
619 client.Do(req) // Note: doesn't hit network
620 matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
623 // Just enough correctness for our redirect tests. Uses the URL.Host as the
624 // scope of all cookies.
625 type TestJar struct {
627 perURL map[string][]*Cookie
630 func (j *TestJar) SetCookies(u *url.URL, cookies []*Cookie) {
634 j.perURL = make(map[string][]*Cookie)
636 j.perURL[u.Host] = cookies
639 func (j *TestJar) Cookies(u *url.URL) []*Cookie {
642 return j.perURL[u.Host]
645 func TestRedirectCookiesJar(t *testing.T) {
648 var ts *httptest.Server
649 ts = httptest.NewServer(echoCookiesRedirectHandler)
652 defer tr.CloseIdleConnections()
657 u, _ := url.Parse(ts.URL)
658 c.Jar.SetCookies(u, []*Cookie{expectedCookies[0]})
659 resp, err := c.Get(ts.URL)
661 t.Fatalf("Get: %v", err)
664 matchReturnedCookies(t, expectedCookies, resp.Cookies())
667 func matchReturnedCookies(t *testing.T, expected, given []*Cookie) {
668 if len(given) != len(expected) {
669 t.Logf("Received cookies: %v", given)
670 t.Errorf("Expected %d cookies, got %d", len(expected), len(given))
672 for _, ec := range expected {
674 for _, c := range given {
675 if ec.Name == c.Name && ec.Value == c.Value {
681 t.Errorf("Missing cookie %v", ec)
686 func TestJarCalls(t *testing.T) {
688 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
689 pathSuffix := r.RequestURI[1:]
690 if r.RequestURI == "/nosetcookie" {
691 return // don't set cookies for this path
693 SetCookie(w, &Cookie{Name: "name" + pathSuffix, Value: "val" + pathSuffix})
694 if r.RequestURI == "/" {
695 Redirect(w, r, "http://secondhost.fake/secondpath", 302)
699 jar := new(RecordingJar)
702 Transport: &Transport{
703 Dial: func(_ string, _ string) (net.Conn, error) {
704 return net.Dial("tcp", ts.Listener.Addr().String())
708 _, err := c.Get("http://firsthost.fake/")
712 _, err = c.Get("http://firsthost.fake/nosetcookie")
716 got := jar.log.String()
717 want := `Cookies("http://firsthost.fake/")
718 SetCookie("http://firsthost.fake/", [name=val])
719 Cookies("http://secondhost.fake/secondpath")
720 SetCookie("http://secondhost.fake/secondpath", [namesecondpath=valsecondpath])
721 Cookies("http://firsthost.fake/nosetcookie")
724 t.Errorf("Got Jar calls:\n%s\nWant:\n%s", got, want)
728 // RecordingJar keeps a log of calls made to it, without
729 // tracking any cookies.
730 type RecordingJar struct {
735 func (j *RecordingJar) SetCookies(u *url.URL, cookies []*Cookie) {
736 j.logf("SetCookie(%q, %v)\n", u, cookies)
739 func (j *RecordingJar) Cookies(u *url.URL) []*Cookie {
740 j.logf("Cookies(%q)\n", u)
744 func (j *RecordingJar) logf(format string, args ...interface{}) {
747 fmt.Fprintf(&j.log, format, args...)
750 func TestStreamingGet_h1(t *testing.T) { testStreamingGet(t, h1Mode) }
751 func TestStreamingGet_h2(t *testing.T) { testStreamingGet(t, h2Mode) }
753 func testStreamingGet(t *testing.T, h2 bool) {
755 say := make(chan string)
756 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
758 for str := range say {
766 res, err := c.Get(cst.ts.URL)
771 for _, str := range []string{"i", "am", "also", "known", "as", "comet"} {
773 n, err := io.ReadFull(res.Body, buf[0:len(str)])
775 t.Fatalf("ReadFull on %q: %v", str, err)
778 t.Fatalf("Receiving %q, only read %d bytes", str, n)
780 got := string(buf[0:n])
782 t.Fatalf("Expected %q, got %q", str, got)
786 _, err = io.ReadFull(res.Body, buf[0:1])
788 t.Fatalf("at end expected EOF, got %v", err)
792 type writeCountingConn struct {
797 func (c *writeCountingConn) Write(p []byte) (int, error) {
799 return c.Conn.Write(p)
802 // TestClientWrites verifies that client requests are buffered and we
803 // don't send a TCP packet per line of the http request + body.
804 func TestClientWrites(t *testing.T) {
806 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
811 dialer := func(netz string, addr string) (net.Conn, error) {
812 c, err := net.Dial(netz, addr)
814 c = &writeCountingConn{c, &writes}
818 c := &Client{Transport: &Transport{Dial: dialer}}
820 _, err := c.Get(ts.URL)
825 t.Errorf("Get request did %d Write calls, want 1", writes)
829 _, err = c.PostForm(ts.URL, url.Values{"foo": {"bar"}})
834 t.Errorf("Post request did %d Write calls, want 1", writes)
838 func TestClientInsecureTransport(t *testing.T) {
841 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
842 w.Write([]byte("Hello"))
844 errc := make(chanWriter, 10) // but only expecting 1
845 ts.Config.ErrorLog = log.New(errc, "", 0)
848 // TODO(bradfitz): add tests for skipping hostname checks too?
849 // would require a new cert for testing, and probably
850 // redundant with these tests.
851 for _, insecure := range []bool{true, false} {
853 TLSClientConfig: &tls.Config{
854 InsecureSkipVerify: insecure,
857 defer tr.CloseIdleConnections()
858 c := &Client{Transport: tr}
859 res, err := c.Get(ts.URL)
860 if (err == nil) != insecure {
861 t.Errorf("insecure=%v: got unexpected err=%v", insecure, err)
870 if !strings.Contains(v, "TLS handshake error") {
871 t.Errorf("expected an error log message containing 'TLS handshake error'; got %q", v)
873 case <-time.After(5 * time.Second):
874 t.Errorf("timeout waiting for logged error")
879 func TestClientErrorWithRequestURI(t *testing.T) {
881 req, _ := NewRequest("GET", "http://localhost:1234/", nil)
882 req.RequestURI = "/this/field/is/illegal/and/should/error/"
883 _, err := DefaultClient.Do(req)
885 t.Fatalf("expected an error")
887 if !strings.Contains(err.Error(), "RequestURI") {
888 t.Errorf("wanted error mentioning RequestURI; got error: %v", err)
892 func newTLSTransport(t *testing.T, ts *httptest.Server) *Transport {
893 certs := x509.NewCertPool()
894 for _, c := range ts.TLS.Certificates {
895 roots, err := x509.ParseCertificates(c.Certificate[len(c.Certificate)-1])
897 t.Fatalf("error parsing server's root cert: %v", err)
899 for _, root := range roots {
904 TLSClientConfig: &tls.Config{RootCAs: certs},
908 func TestClientWithCorrectTLSServerName(t *testing.T) {
911 const serverName = "example.com"
912 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
913 if r.TLS.ServerName != serverName {
914 t.Errorf("expected client to set ServerName %q, got: %q", serverName, r.TLS.ServerName)
919 trans := newTLSTransport(t, ts)
920 trans.TLSClientConfig.ServerName = serverName
921 c := &Client{Transport: trans}
922 if _, err := c.Get(ts.URL); err != nil {
923 t.Fatalf("expected successful TLS connection, got error: %v", err)
927 func TestClientWithIncorrectTLSServerName(t *testing.T) {
929 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
931 errc := make(chanWriter, 10) // but only expecting 1
932 ts.Config.ErrorLog = log.New(errc, "", 0)
934 trans := newTLSTransport(t, ts)
935 trans.TLSClientConfig.ServerName = "badserver"
936 c := &Client{Transport: trans}
937 _, err := c.Get(ts.URL)
939 t.Fatalf("expected an error")
941 if !strings.Contains(err.Error(), "127.0.0.1") || !strings.Contains(err.Error(), "badserver") {
942 t.Errorf("wanted error mentioning 127.0.0.1 and badserver; got error: %v", err)
946 if !strings.Contains(v, "TLS handshake error") {
947 t.Errorf("expected an error log message containing 'TLS handshake error'; got %q", v)
949 case <-time.After(5 * time.Second):
950 t.Errorf("timeout waiting for logged error")
954 // Test for golang.org/issue/5829; the Transport should respect TLSClientConfig.ServerName
957 // tls.Config.ServerName (non-empty, set to "example.com") takes
958 // precedence over "some-other-host.tld" which previously incorrectly
959 // took precedence. We don't actually connect to (or even resolve)
960 // "some-other-host.tld", though, because of the Transport.Dial hook.
962 // The httptest.Server has a cert with "example.com" as its name.
963 func TestTransportUsesTLSConfigServerName(t *testing.T) {
965 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
966 w.Write([]byte("Hello"))
970 tr := newTLSTransport(t, ts)
971 tr.TLSClientConfig.ServerName = "example.com" // one of httptest's Server cert names
972 tr.Dial = func(netw, addr string) (net.Conn, error) {
973 return net.Dial(netw, ts.Listener.Addr().String())
975 defer tr.CloseIdleConnections()
976 c := &Client{Transport: tr}
977 res, err := c.Get("https://some-other-host.tld/")
984 func TestResponseSetsTLSConnectionState(t *testing.T) {
986 ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
987 w.Write([]byte("Hello"))
991 tr := newTLSTransport(t, ts)
992 tr.TLSClientConfig.CipherSuites = []uint16{tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA}
993 tr.Dial = func(netw, addr string) (net.Conn, error) {
994 return net.Dial(netw, ts.Listener.Addr().String())
996 defer tr.CloseIdleConnections()
997 c := &Client{Transport: tr}
998 res, err := c.Get("https://example.com/")
1002 defer res.Body.Close()
1004 t.Fatal("Response didn't set TLS Connection State.")
1006 if got, want := res.TLS.CipherSuite, tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA; got != want {
1007 t.Errorf("TLS Cipher Suite = %d; want %d", got, want)
1011 // Check that an HTTPS client can interpret a particular TLS error
1012 // to determine that the server is speaking HTTP.
1013 // See golang.org/issue/11111.
1014 func TestHTTPSClientDetectsHTTPServer(t *testing.T) {
1016 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
1019 _, err := Get(strings.Replace(ts.URL, "http", "https", 1))
1020 if got := err.Error(); !strings.Contains(got, "HTTP response to HTTPS client") {
1021 t.Fatalf("error = %q; want error indicating HTTP response to HTTPS request", got)
1025 // Verify Response.ContentLength is populated. https://golang.org/issue/4126
1026 func TestClientHeadContentLength_h1(t *testing.T) {
1027 testClientHeadContentLength(t, h1Mode)
1030 func TestClientHeadContentLength_h2(t *testing.T) {
1031 testClientHeadContentLength(t, h2Mode)
1034 func testClientHeadContentLength(t *testing.T, h2 bool) {
1036 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1037 if v := r.FormValue("cl"); v != "" {
1038 w.Header().Set("Content-Length", v)
1046 {"/?cl=1234", 1234},
1050 for _, tt := range tests {
1051 req, _ := NewRequest("HEAD", cst.ts.URL+tt.suffix, nil)
1052 res, err := cst.c.Do(req)
1056 if res.ContentLength != tt.want {
1057 t.Errorf("Content-Length = %d; want %d", res.ContentLength, tt.want)
1059 bs, err := ioutil.ReadAll(res.Body)
1064 t.Errorf("Unexpected content: %q", bs)
1069 func TestEmptyPasswordAuth(t *testing.T) {
1073 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
1074 auth := r.Header.Get("Authorization")
1075 if strings.HasPrefix(auth, "Basic ") {
1077 decoded, err := base64.StdEncoding.DecodeString(encoded)
1081 expected := gopher + ":"
1082 s := string(decoded)
1084 t.Errorf("Invalid Authorization header. Got %q, wanted %q", s, expected)
1087 t.Errorf("Invalid auth %q", auth)
1092 defer tr.CloseIdleConnections()
1093 c := &Client{Transport: tr}
1094 req, err := NewRequest("GET", ts.URL, nil)
1098 req.URL.User = url.User(gopher)
1099 resp, err := c.Do(req)
1103 defer resp.Body.Close()
1106 func TestBasicAuth(t *testing.T) {
1108 tr := &recordingTransport{}
1109 client := &Client{Transport: tr}
1111 url := "http://My%20User:My%20Pass@dummy.faketld/"
1112 expected := "My User:My Pass"
1115 if tr.req.Method != "GET" {
1116 t.Errorf("got method %q, want %q", tr.req.Method, "GET")
1118 if tr.req.URL.String() != url {
1119 t.Errorf("got URL %q, want %q", tr.req.URL.String(), url)
1121 if tr.req.Header == nil {
1122 t.Fatalf("expected non-nil request Header")
1124 auth := tr.req.Header.Get("Authorization")
1125 if strings.HasPrefix(auth, "Basic ") {
1127 decoded, err := base64.StdEncoding.DecodeString(encoded)
1131 s := string(decoded)
1133 t.Errorf("Invalid Authorization header. Got %q, wanted %q", s, expected)
1136 t.Errorf("Invalid auth %q", auth)
1140 func TestBasicAuthHeadersPreserved(t *testing.T) {
1142 tr := &recordingTransport{}
1143 client := &Client{Transport: tr}
1145 // If Authorization header is provided, username in URL should not override it
1146 url := "http://My%20User@dummy.faketld/"
1147 req, err := NewRequest("GET", url, nil)
1151 req.SetBasicAuth("My User", "My Pass")
1152 expected := "My User:My Pass"
1155 if tr.req.Method != "GET" {
1156 t.Errorf("got method %q, want %q", tr.req.Method, "GET")
1158 if tr.req.URL.String() != url {
1159 t.Errorf("got URL %q, want %q", tr.req.URL.String(), url)
1161 if tr.req.Header == nil {
1162 t.Fatalf("expected non-nil request Header")
1164 auth := tr.req.Header.Get("Authorization")
1165 if strings.HasPrefix(auth, "Basic ") {
1167 decoded, err := base64.StdEncoding.DecodeString(encoded)
1171 s := string(decoded)
1173 t.Errorf("Invalid Authorization header. Got %q, wanted %q", s, expected)
1176 t.Errorf("Invalid auth %q", auth)
1181 func TestClientTimeout_h1(t *testing.T) { testClientTimeout(t, h1Mode) }
1182 func TestClientTimeout_h2(t *testing.T) { testClientTimeout(t, h2Mode) }
1184 func testClientTimeout(t *testing.T, h2 bool) {
1185 if testing.Short() {
1186 t.Skip("skipping in short mode")
1189 sawRoot := make(chan bool, 1)
1190 sawSlow := make(chan bool, 1)
1191 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1192 if r.URL.Path == "/" {
1194 Redirect(w, r, "/slow", StatusFound)
1197 if r.URL.Path == "/slow" {
1198 w.Write([]byte("Hello"))
1201 time.Sleep(2 * time.Second)
1206 const timeout = 500 * time.Millisecond
1207 cst.c.Timeout = timeout
1209 res, err := cst.c.Get(cst.ts.URL)
1218 t.Fatal("handler never got / request")
1225 t.Fatal("handler never got /slow request")
1228 errc := make(chan error, 1)
1230 _, err := ioutil.ReadAll(res.Body)
1235 const failTime = timeout * 2
1239 t.Fatal("expected error from ReadAll")
1241 ne, ok := err.(net.Error)
1243 t.Errorf("error value from ReadAll was %T; expected some net.Error", err)
1244 } else if !ne.Timeout() {
1245 t.Errorf("net.Error.Timeout = false; want true")
1247 if got := ne.Error(); !strings.Contains(got, "Client.Timeout exceeded") {
1248 t.Errorf("error string = %q; missing timeout substring", got)
1250 case <-time.After(failTime):
1251 t.Errorf("timeout after %v waiting for timeout of %v", failTime, timeout)
1255 func TestClientTimeout_Headers_h1(t *testing.T) { testClientTimeout_Headers(t, h1Mode) }
1256 func TestClientTimeout_Headers_h2(t *testing.T) { testClientTimeout_Headers(t, h2Mode) }
1258 // Client.Timeout firing before getting to the body
1259 func testClientTimeout_Headers(t *testing.T, h2 bool) {
1260 if testing.Short() {
1261 t.Skip("skipping in short mode")
1264 donec := make(chan bool)
1265 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1269 // Note that we use a channel send here and not a close.
1270 // The race detector doesn't know that we're waiting for a timeout
1271 // and thinks that the waitgroup inside httptest.Server is added to concurrently
1272 // with us closing it. If we timed out immediately, we could close the testserver
1273 // before we entered the handler. We're not timing out immediately and there's
1274 // no way we would be done before we entered the handler, but the race detector
1275 // doesn't know this, so synchronize explicitly.
1276 defer func() { donec <- true }()
1278 cst.c.Timeout = 500 * time.Millisecond
1279 _, err := cst.c.Get(cst.ts.URL)
1281 t.Fatal("got response from Get; expected error")
1283 if _, ok := err.(*url.Error); !ok {
1284 t.Fatalf("Got error of type %T; want *url.Error", err)
1286 ne, ok := err.(net.Error)
1288 t.Fatalf("Got error of type %T; want some net.Error", err)
1291 t.Error("net.Error.Timeout = false; want true")
1293 if got := ne.Error(); !strings.Contains(got, "Client.Timeout exceeded") {
1294 t.Errorf("error string = %q; missing timeout substring", got)
1298 func TestClientRedirectEatsBody_h1(t *testing.T) { testClientRedirectEatsBody(t, h1Mode) }
1299 func TestClientRedirectEatsBody_h2(t *testing.T) { testClientRedirectEatsBody(t, h2Mode) }
1300 func testClientRedirectEatsBody(t *testing.T, h2 bool) {
1303 saw := make(chan string, 2)
1304 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1306 if r.URL.Path == "/" {
1307 Redirect(w, r, "/foo", StatusFound) // which includes a body
1312 res, err := cst.c.Get(cst.ts.URL)
1316 _, err = ioutil.ReadAll(res.Body)
1326 t.Fatal("server didn't see a request")
1331 case second = <-saw:
1333 t.Fatal("server didn't see a second request")
1336 if first != second {
1337 t.Fatal("server saw different client ports before & after the redirect")
1341 // eofReaderFunc is an io.Reader that runs itself, and then returns io.EOF.
1342 type eofReaderFunc func()
1344 func (f eofReaderFunc) Read(p []byte) (n int, err error) {
1349 func TestReferer(t *testing.T) {
1351 lastReq, newReq string // from -> to URLs
1355 {"http://gopher@test.com", "http://link.com", "http://test.com"},
1356 {"https://gopher@test.com", "https://link.com", "https://test.com"},
1358 // don't send a user and password:
1359 {"http://gopher:go@test.com", "http://link.com", "http://test.com"},
1360 {"https://gopher:go@test.com", "https://link.com", "https://test.com"},
1363 {"http://test.com", "http://link.com", "http://test.com"},
1364 {"https://test.com", "https://link.com", "https://test.com"},
1366 // https to http doesn't send a referer:
1367 {"https://test.com", "http://link.com", ""},
1368 {"https://gopher:go@test.com", "http://link.com", ""},
1370 for _, tt := range tests {
1371 l, err := url.Parse(tt.lastReq)
1375 n, err := url.Parse(tt.newReq)
1379 r := ExportRefererForURL(l, n)
1381 t.Errorf("refererForURL(%q, %q) = %q; want %q", tt.lastReq, tt.newReq, r, tt.want)
1386 // issue15577Tripper returns a Response with a redirect response
1387 // header and doesn't populate its Response.Request field.
1388 type issue15577Tripper struct{}
1390 func (issue15577Tripper) RoundTrip(*Request) (*Response, error) {
1393 Header: map[string][]string{"Location": {"http://www.example.com/"}},
1394 Body: ioutil.NopCloser(strings.NewReader("")),
1399 // Issue 15577: don't assume the roundtripper's response populates its Request field.
1400 func TestClientRedirectResponseWithoutRequest(t *testing.T) {
1402 CheckRedirect: func(*Request, []*Request) error { return fmt.Errorf("no redirects!") },
1403 Transport: issue15577Tripper{},
1405 // Check that this doesn't crash:
1406 c.Get("http://dummy.tld")
1409 // Issue 4800: copy (some) headers when Client follows a redirect
1410 func TestClientCopyHeadersOnRedirect(t *testing.T) {
1412 ua = "some-agent/1.2"
1416 ts1 := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
1418 "User-Agent": []string{ua},
1419 "X-Foo": []string{xfoo},
1420 "Referer": []string{ts2URL},
1421 "Accept-Encoding": []string{"gzip"},
1423 if !reflect.DeepEqual(r.Header, want) {
1424 t.Errorf("Request.Header = %#v; want %#v", r.Header, want)
1427 w.Header().Set("Result", "got errors")
1429 w.Header().Set("Result", "ok")
1433 ts2 := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
1434 Redirect(w, r, ts1.URL, StatusFound)
1440 defer tr.CloseIdleConnections()
1443 CheckRedirect: func(r *Request, via []*Request) error {
1445 "User-Agent": []string{ua},
1446 "X-Foo": []string{xfoo},
1447 "Referer": []string{ts2URL},
1449 if !reflect.DeepEqual(r.Header, want) {
1450 t.Errorf("CheckRedirect Request.Header = %#v; want %#v", r.Header, want)
1456 req, _ := NewRequest("GET", ts2.URL, nil)
1457 req.Header.Add("User-Agent", ua)
1458 req.Header.Add("X-Foo", xfoo)
1459 req.Header.Add("Cookie", "foo=bar")
1460 req.Header.Add("Authorization", "secretpassword")
1461 res, err := c.Do(req)
1465 defer res.Body.Close()
1466 if res.StatusCode != 200 {
1469 if got := res.Header.Get("Result"); got != "ok" {
1470 t.Errorf("result = %q; want ok", got)
1474 // Issue 17494: cookies should be altered when Client follows redirects.
1475 func TestClientAltersCookiesOnRedirect(t *testing.T) {
1476 cookieMap := func(cs []*Cookie) map[string][]string {
1477 m := make(map[string][]string)
1478 for _, c := range cs {
1479 m[c.Name] = append(m[c.Name], c.Value)
1484 ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
1485 var want map[string][]string
1486 got := cookieMap(r.Cookies())
1488 c, _ := r.Cookie("Cycle")
1491 want = map[string][]string{
1492 "Cookie1": {"OldValue1a", "OldValue1b"},
1493 "Cookie2": {"OldValue2"},
1494 "Cookie3": {"OldValue3a", "OldValue3b"},
1495 "Cookie4": {"OldValue4"},
1498 SetCookie(w, &Cookie{Name: "Cycle", Value: "1", Path: "/"})
1499 SetCookie(w, &Cookie{Name: "Cookie2", Path: "/", MaxAge: -1}) // Delete cookie from Header
1500 Redirect(w, r, "/", StatusFound)
1502 want = map[string][]string{
1503 "Cookie1": {"OldValue1a", "OldValue1b"},
1504 "Cookie3": {"OldValue3a", "OldValue3b"},
1505 "Cookie4": {"OldValue4"},
1508 SetCookie(w, &Cookie{Name: "Cycle", Value: "2", Path: "/"})
1509 SetCookie(w, &Cookie{Name: "Cookie3", Value: "NewValue3", Path: "/"}) // Modify cookie in Header
1510 SetCookie(w, &Cookie{Name: "Cookie4", Value: "NewValue4", Path: "/"}) // Modify cookie in Jar
1511 Redirect(w, r, "/", StatusFound)
1513 want = map[string][]string{
1514 "Cookie1": {"OldValue1a", "OldValue1b"},
1515 "Cookie3": {"NewValue3"},
1516 "Cookie4": {"NewValue4"},
1519 SetCookie(w, &Cookie{Name: "Cycle", Value: "3", Path: "/"})
1520 SetCookie(w, &Cookie{Name: "Cookie5", Value: "NewValue5", Path: "/"}) // Insert cookie into Jar
1521 Redirect(w, r, "/", StatusFound)
1523 want = map[string][]string{
1524 "Cookie1": {"OldValue1a", "OldValue1b"},
1525 "Cookie3": {"NewValue3"},
1526 "Cookie4": {"NewValue4"},
1527 "Cookie5": {"NewValue5"},
1530 // Don't redirect to ensure the loop ends.
1532 t.Errorf("unexpected redirect cycle")
1536 if !reflect.DeepEqual(got, want) {
1537 t.Errorf("redirect %s, Cookie = %v, want %v", c.Value, got, want)
1543 defer tr.CloseIdleConnections()
1544 jar, _ := cookiejar.New(nil)
1550 u, _ := url.Parse(ts.URL)
1551 req, _ := NewRequest("GET", ts.URL, nil)
1552 req.AddCookie(&Cookie{Name: "Cookie1", Value: "OldValue1a"})
1553 req.AddCookie(&Cookie{Name: "Cookie1", Value: "OldValue1b"})
1554 req.AddCookie(&Cookie{Name: "Cookie2", Value: "OldValue2"})
1555 req.AddCookie(&Cookie{Name: "Cookie3", Value: "OldValue3a"})
1556 req.AddCookie(&Cookie{Name: "Cookie3", Value: "OldValue3b"})
1557 jar.SetCookies(u, []*Cookie{{Name: "Cookie4", Value: "OldValue4", Path: "/"}})
1558 jar.SetCookies(u, []*Cookie{{Name: "Cycle", Value: "0", Path: "/"}})
1559 res, err := c.Do(req)
1563 defer res.Body.Close()
1564 if res.StatusCode != 200 {
1569 // Part of Issue 4800
1570 func TestShouldCopyHeaderOnRedirect(t *testing.T) {
1577 {"User-Agent", "http://foo.com/", "http://bar.com/", true},
1578 {"X-Foo", "http://foo.com/", "http://bar.com/", true},
1580 // Sensitive headers:
1581 {"cookie", "http://foo.com/", "http://bar.com/", false},
1582 {"cookie2", "http://foo.com/", "http://bar.com/", false},
1583 {"authorization", "http://foo.com/", "http://bar.com/", false},
1584 {"www-authenticate", "http://foo.com/", "http://bar.com/", false},
1586 // But subdomains should work:
1587 {"www-authenticate", "http://foo.com/", "http://foo.com/", true},
1588 {"www-authenticate", "http://foo.com/", "http://sub.foo.com/", true},
1589 {"www-authenticate", "http://foo.com/", "http://notfoo.com/", false},
1590 // TODO(bradfitz): make this test work, once issue 16142 is fixed:
1591 // {"www-authenticate", "http://foo.com:80/", "http://foo.com/", true},
1593 for i, tt := range tests {
1594 u0, err := url.Parse(tt.initialURL)
1596 t.Errorf("%d. initial URL %q parse error: %v", i, tt.initialURL, err)
1599 u1, err := url.Parse(tt.destURL)
1601 t.Errorf("%d. dest URL %q parse error: %v", i, tt.destURL, err)
1604 got := Export_shouldCopyHeaderOnRedirect(tt.header, u0, u1)
1606 t.Errorf("%d. shouldCopyHeaderOnRedirect(%q, %q => %q) = %v; want %v",
1607 i, tt.header, tt.initialURL, tt.destURL, got, tt.want)
1612 func TestClientRedirectTypes(t *testing.T) {
1616 tests := [...]struct {
1619 wantMethod string // desired subsequent client method
1621 0: {method: "POST", serverStatus: 301, wantMethod: "GET"},
1622 1: {method: "POST", serverStatus: 302, wantMethod: "GET"},
1623 2: {method: "POST", serverStatus: 303, wantMethod: "GET"},
1624 3: {method: "POST", serverStatus: 307, wantMethod: "POST"},
1625 4: {method: "POST", serverStatus: 308, wantMethod: "POST"},
1627 5: {method: "HEAD", serverStatus: 301, wantMethod: "GET"},
1628 6: {method: "HEAD", serverStatus: 302, wantMethod: "GET"},
1629 7: {method: "HEAD", serverStatus: 303, wantMethod: "GET"},
1630 8: {method: "HEAD", serverStatus: 307, wantMethod: "HEAD"},
1631 9: {method: "HEAD", serverStatus: 308, wantMethod: "HEAD"},
1633 10: {method: "GET", serverStatus: 301, wantMethod: "GET"},
1634 11: {method: "GET", serverStatus: 302, wantMethod: "GET"},
1635 12: {method: "GET", serverStatus: 303, wantMethod: "GET"},
1636 13: {method: "GET", serverStatus: 307, wantMethod: "GET"},
1637 14: {method: "GET", serverStatus: 308, wantMethod: "GET"},
1639 15: {method: "DELETE", serverStatus: 301, wantMethod: "GET"},
1640 16: {method: "DELETE", serverStatus: 302, wantMethod: "GET"},
1641 17: {method: "DELETE", serverStatus: 303, wantMethod: "GET"},
1642 18: {method: "DELETE", serverStatus: 307, wantMethod: "DELETE"},
1643 19: {method: "DELETE", serverStatus: 308, wantMethod: "DELETE"},
1645 20: {method: "PUT", serverStatus: 301, wantMethod: "GET"},
1646 21: {method: "PUT", serverStatus: 302, wantMethod: "GET"},
1647 22: {method: "PUT", serverStatus: 303, wantMethod: "GET"},
1648 23: {method: "PUT", serverStatus: 307, wantMethod: "PUT"},
1649 24: {method: "PUT", serverStatus: 308, wantMethod: "PUT"},
1651 25: {method: "MADEUPMETHOD", serverStatus: 301, wantMethod: "GET"},
1652 26: {method: "MADEUPMETHOD", serverStatus: 302, wantMethod: "GET"},
1653 27: {method: "MADEUPMETHOD", serverStatus: 303, wantMethod: "GET"},
1654 28: {method: "MADEUPMETHOD", serverStatus: 307, wantMethod: "MADEUPMETHOD"},
1655 29: {method: "MADEUPMETHOD", serverStatus: 308, wantMethod: "MADEUPMETHOD"},
1658 handlerc := make(chan HandlerFunc, 1)
1660 ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {
1667 defer tr.CloseIdleConnections()
1669 for i, tt := range tests {
1670 handlerc <- func(w ResponseWriter, r *Request) {
1671 w.Header().Set("Location", ts.URL)
1672 w.WriteHeader(tt.serverStatus)
1675 req, err := NewRequest(tt.method, ts.URL, nil)
1677 t.Errorf("#%d: NewRequest: %v", i, err)
1681 c := &Client{Transport: tr}
1682 c.CheckRedirect = func(req *Request, via []*Request) error {
1683 if got, want := req.Method, tt.wantMethod; got != want {
1684 return fmt.Errorf("#%d: got next method %q; want %q", i, got, want)
1686 handlerc <- func(rw ResponseWriter, req *Request) {
1687 // TODO: Check that the body is valid when we do 307 and 308 support
1692 res, err := c.Do(req)
1694 t.Errorf("#%d: Response: %v", i, err)