]> Cypherpunks.ru repositories - gostls13.git/blob - src/net/http/client_test.go
net/http: fix hang in probing for a zero-length request body
[gostls13.git] / src / net / http / client_test.go
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.
4
5 // Tests for client.go
6
7 package http_test
8
9 import (
10         "bytes"
11         "context"
12         "crypto/tls"
13         "encoding/base64"
14         "errors"
15         "fmt"
16         "io"
17         "log"
18         "net"
19         . "net/http"
20         "net/http/cookiejar"
21         "net/http/httptest"
22         "net/url"
23         "reflect"
24         "strconv"
25         "strings"
26         "sync"
27         "sync/atomic"
28         "testing"
29         "time"
30 )
31
32 var robotsTxtHandler = HandlerFunc(func(w ResponseWriter, r *Request) {
33         w.Header().Set("Last-Modified", "sometime")
34         fmt.Fprintf(w, "User-agent: go\nDisallow: /something/")
35 })
36
37 // pedanticReadAll works like io.ReadAll but additionally
38 // verifies that r obeys the documented io.Reader contract.
39 func pedanticReadAll(r io.Reader) (b []byte, err error) {
40         var bufa [64]byte
41         buf := bufa[:]
42         for {
43                 n, err := r.Read(buf)
44                 if n == 0 && err == nil {
45                         return nil, fmt.Errorf("Read: n=0 with err=nil")
46                 }
47                 b = append(b, buf[:n]...)
48                 if err == io.EOF {
49                         n, err := r.Read(buf)
50                         if n != 0 || err != io.EOF {
51                                 return nil, fmt.Errorf("Read: n=%d err=%#v after EOF", n, err)
52                         }
53                         return b, nil
54                 }
55                 if err != nil {
56                         return b, err
57                 }
58         }
59 }
60
61 type chanWriter chan string
62
63 func (w chanWriter) Write(p []byte) (n int, err error) {
64         w <- string(p)
65         return len(p), nil
66 }
67
68 func TestClient(t *testing.T) {
69         setParallel(t)
70         defer afterTest(t)
71         ts := httptest.NewServer(robotsTxtHandler)
72         defer ts.Close()
73
74         c := ts.Client()
75         r, err := c.Get(ts.URL)
76         var b []byte
77         if err == nil {
78                 b, err = pedanticReadAll(r.Body)
79                 r.Body.Close()
80         }
81         if err != nil {
82                 t.Error(err)
83         } else if s := string(b); !strings.HasPrefix(s, "User-agent:") {
84                 t.Errorf("Incorrect page body (did not begin with User-agent): %q", s)
85         }
86 }
87
88 func TestClientHead_h1(t *testing.T) { testClientHead(t, h1Mode) }
89 func TestClientHead_h2(t *testing.T) { testClientHead(t, h2Mode) }
90
91 func testClientHead(t *testing.T, h2 bool) {
92         defer afterTest(t)
93         cst := newClientServerTest(t, h2, robotsTxtHandler)
94         defer cst.close()
95
96         r, err := cst.c.Head(cst.ts.URL)
97         if err != nil {
98                 t.Fatal(err)
99         }
100         if _, ok := r.Header["Last-Modified"]; !ok {
101                 t.Error("Last-Modified header not found.")
102         }
103 }
104
105 type recordingTransport struct {
106         req *Request
107 }
108
109 func (t *recordingTransport) RoundTrip(req *Request) (resp *Response, err error) {
110         t.req = req
111         return nil, errors.New("dummy impl")
112 }
113
114 func TestGetRequestFormat(t *testing.T) {
115         setParallel(t)
116         defer afterTest(t)
117         tr := &recordingTransport{}
118         client := &Client{Transport: tr}
119         url := "http://dummy.faketld/"
120         client.Get(url) // Note: doesn't hit network
121         if tr.req.Method != "GET" {
122                 t.Errorf("expected method %q; got %q", "GET", tr.req.Method)
123         }
124         if tr.req.URL.String() != url {
125                 t.Errorf("expected URL %q; got %q", url, tr.req.URL.String())
126         }
127         if tr.req.Header == nil {
128                 t.Errorf("expected non-nil request Header")
129         }
130 }
131
132 func TestPostRequestFormat(t *testing.T) {
133         defer afterTest(t)
134         tr := &recordingTransport{}
135         client := &Client{Transport: tr}
136
137         url := "http://dummy.faketld/"
138         json := `{"key":"value"}`
139         b := strings.NewReader(json)
140         client.Post(url, "application/json", b) // Note: doesn't hit network
141
142         if tr.req.Method != "POST" {
143                 t.Errorf("got method %q, want %q", tr.req.Method, "POST")
144         }
145         if tr.req.URL.String() != url {
146                 t.Errorf("got URL %q, want %q", tr.req.URL.String(), url)
147         }
148         if tr.req.Header == nil {
149                 t.Fatalf("expected non-nil request Header")
150         }
151         if tr.req.Close {
152                 t.Error("got Close true, want false")
153         }
154         if g, e := tr.req.ContentLength, int64(len(json)); g != e {
155                 t.Errorf("got ContentLength %d, want %d", g, e)
156         }
157 }
158
159 func TestPostFormRequestFormat(t *testing.T) {
160         defer afterTest(t)
161         tr := &recordingTransport{}
162         client := &Client{Transport: tr}
163
164         urlStr := "http://dummy.faketld/"
165         form := make(url.Values)
166         form.Set("foo", "bar")
167         form.Add("foo", "bar2")
168         form.Set("bar", "baz")
169         client.PostForm(urlStr, form) // Note: doesn't hit network
170
171         if tr.req.Method != "POST" {
172                 t.Errorf("got method %q, want %q", tr.req.Method, "POST")
173         }
174         if tr.req.URL.String() != urlStr {
175                 t.Errorf("got URL %q, want %q", tr.req.URL.String(), urlStr)
176         }
177         if tr.req.Header == nil {
178                 t.Fatalf("expected non-nil request Header")
179         }
180         if g, e := tr.req.Header.Get("Content-Type"), "application/x-www-form-urlencoded"; g != e {
181                 t.Errorf("got Content-Type %q, want %q", g, e)
182         }
183         if tr.req.Close {
184                 t.Error("got Close true, want false")
185         }
186         // Depending on map iteration, body can be either of these.
187         expectedBody := "foo=bar&foo=bar2&bar=baz"
188         expectedBody1 := "bar=baz&foo=bar&foo=bar2"
189         if g, e := tr.req.ContentLength, int64(len(expectedBody)); g != e {
190                 t.Errorf("got ContentLength %d, want %d", g, e)
191         }
192         bodyb, err := io.ReadAll(tr.req.Body)
193         if err != nil {
194                 t.Fatalf("ReadAll on req.Body: %v", err)
195         }
196         if g := string(bodyb); g != expectedBody && g != expectedBody1 {
197                 t.Errorf("got body %q, want %q or %q", g, expectedBody, expectedBody1)
198         }
199 }
200
201 func TestClientRedirects(t *testing.T) {
202         setParallel(t)
203         defer afterTest(t)
204         var ts *httptest.Server
205         ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
206                 n, _ := strconv.Atoi(r.FormValue("n"))
207                 // Test Referer header. (7 is arbitrary position to test at)
208                 if n == 7 {
209                         if g, e := r.Referer(), ts.URL+"/?n=6"; e != g {
210                                 t.Errorf("on request ?n=7, expected referer of %q; got %q", e, g)
211                         }
212                 }
213                 if n < 15 {
214                         Redirect(w, r, fmt.Sprintf("/?n=%d", n+1), StatusTemporaryRedirect)
215                         return
216                 }
217                 fmt.Fprintf(w, "n=%d", n)
218         }))
219         defer ts.Close()
220
221         c := ts.Client()
222         _, err := c.Get(ts.URL)
223         if e, g := `Get "/?n=10": stopped after 10 redirects`, fmt.Sprintf("%v", err); e != g {
224                 t.Errorf("with default client Get, expected error %q, got %q", e, g)
225         }
226
227         // HEAD request should also have the ability to follow redirects.
228         _, err = c.Head(ts.URL)
229         if e, g := `Head "/?n=10": stopped after 10 redirects`, fmt.Sprintf("%v", err); e != g {
230                 t.Errorf("with default client Head, expected error %q, got %q", e, g)
231         }
232
233         // Do should also follow redirects.
234         greq, _ := NewRequest("GET", ts.URL, nil)
235         _, err = c.Do(greq)
236         if e, g := `Get "/?n=10": stopped after 10 redirects`, fmt.Sprintf("%v", err); e != g {
237                 t.Errorf("with default client Do, expected error %q, got %q", e, g)
238         }
239
240         // Requests with an empty Method should also redirect (Issue 12705)
241         greq.Method = ""
242         _, err = c.Do(greq)
243         if e, g := `Get "/?n=10": stopped after 10 redirects`, fmt.Sprintf("%v", err); e != g {
244                 t.Errorf("with default client Do and empty Method, expected error %q, got %q", e, g)
245         }
246
247         var checkErr error
248         var lastVia []*Request
249         var lastReq *Request
250         c.CheckRedirect = func(req *Request, via []*Request) error {
251                 lastReq = req
252                 lastVia = via
253                 return checkErr
254         }
255         res, err := c.Get(ts.URL)
256         if err != nil {
257                 t.Fatalf("Get error: %v", err)
258         }
259         res.Body.Close()
260         finalURL := res.Request.URL.String()
261         if e, g := "<nil>", fmt.Sprintf("%v", err); e != g {
262                 t.Errorf("with custom client, expected error %q, got %q", e, g)
263         }
264         if !strings.HasSuffix(finalURL, "/?n=15") {
265                 t.Errorf("expected final url to end in /?n=15; got url %q", finalURL)
266         }
267         if e, g := 15, len(lastVia); e != g {
268                 t.Errorf("expected lastVia to have contained %d elements; got %d", e, g)
269         }
270
271         // Test that Request.Cancel is propagated between requests (Issue 14053)
272         creq, _ := NewRequest("HEAD", ts.URL, nil)
273         cancel := make(chan struct{})
274         creq.Cancel = cancel
275         if _, err := c.Do(creq); err != nil {
276                 t.Fatal(err)
277         }
278         if lastReq == nil {
279                 t.Fatal("didn't see redirect")
280         }
281         if lastReq.Cancel != cancel {
282                 t.Errorf("expected lastReq to have the cancel channel set on the initial req")
283         }
284
285         checkErr = errors.New("no redirects allowed")
286         res, err = c.Get(ts.URL)
287         if urlError, ok := err.(*url.Error); !ok || urlError.Err != checkErr {
288                 t.Errorf("with redirects forbidden, expected a *url.Error with our 'no redirects allowed' error inside; got %#v (%q)", err, err)
289         }
290         if res == nil {
291                 t.Fatalf("Expected a non-nil Response on CheckRedirect failure (https://golang.org/issue/3795)")
292         }
293         res.Body.Close()
294         if res.Header.Get("Location") == "" {
295                 t.Errorf("no Location header in Response")
296         }
297 }
298
299 // Tests that Client redirects' contexts are derived from the original request's context.
300 func TestClientRedirectContext(t *testing.T) {
301         setParallel(t)
302         defer afterTest(t)
303         ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
304                 Redirect(w, r, "/", StatusTemporaryRedirect)
305         }))
306         defer ts.Close()
307
308         ctx, cancel := context.WithCancel(context.Background())
309         c := ts.Client()
310         c.CheckRedirect = func(req *Request, via []*Request) error {
311                 cancel()
312                 select {
313                 case <-req.Context().Done():
314                         return nil
315                 case <-time.After(5 * time.Second):
316                         return errors.New("redirected request's context never expired after root request canceled")
317                 }
318         }
319         req, _ := NewRequestWithContext(ctx, "GET", ts.URL, nil)
320         _, err := c.Do(req)
321         ue, ok := err.(*url.Error)
322         if !ok {
323                 t.Fatalf("got error %T; want *url.Error", err)
324         }
325         if ue.Err != context.Canceled {
326                 t.Errorf("url.Error.Err = %v; want %v", ue.Err, context.Canceled)
327         }
328 }
329
330 type redirectTest struct {
331         suffix       string
332         want         int // response code
333         redirectBody string
334 }
335
336 func TestPostRedirects(t *testing.T) {
337         postRedirectTests := []redirectTest{
338                 {"/", 200, "first"},
339                 {"/?code=301&next=302", 200, "c301"},
340                 {"/?code=302&next=302", 200, "c302"},
341                 {"/?code=303&next=301", 200, "c303wc301"}, // Issue 9348
342                 {"/?code=304", 304, "c304"},
343                 {"/?code=305", 305, "c305"},
344                 {"/?code=307&next=303,308,302", 200, "c307"},
345                 {"/?code=308&next=302,301", 200, "c308"},
346                 {"/?code=404", 404, "c404"},
347         }
348
349         wantSegments := []string{
350                 `POST / "first"`,
351                 `POST /?code=301&next=302 "c301"`,
352                 `GET /?code=302 ""`,
353                 `GET / ""`,
354                 `POST /?code=302&next=302 "c302"`,
355                 `GET /?code=302 ""`,
356                 `GET / ""`,
357                 `POST /?code=303&next=301 "c303wc301"`,
358                 `GET /?code=301 ""`,
359                 `GET / ""`,
360                 `POST /?code=304 "c304"`,
361                 `POST /?code=305 "c305"`,
362                 `POST /?code=307&next=303,308,302 "c307"`,
363                 `POST /?code=303&next=308,302 "c307"`,
364                 `GET /?code=308&next=302 ""`,
365                 `GET /?code=302 "c307"`,
366                 `GET / ""`,
367                 `POST /?code=308&next=302,301 "c308"`,
368                 `POST /?code=302&next=301 "c308"`,
369                 `GET /?code=301 ""`,
370                 `GET / ""`,
371                 `POST /?code=404 "c404"`,
372         }
373         want := strings.Join(wantSegments, "\n")
374         testRedirectsByMethod(t, "POST", postRedirectTests, want)
375 }
376
377 func TestDeleteRedirects(t *testing.T) {
378         deleteRedirectTests := []redirectTest{
379                 {"/", 200, "first"},
380                 {"/?code=301&next=302,308", 200, "c301"},
381                 {"/?code=302&next=302", 200, "c302"},
382                 {"/?code=303", 200, "c303"},
383                 {"/?code=307&next=301,308,303,302,304", 304, "c307"},
384                 {"/?code=308&next=307", 200, "c308"},
385                 {"/?code=404", 404, "c404"},
386         }
387
388         wantSegments := []string{
389                 `DELETE / "first"`,
390                 `DELETE /?code=301&next=302,308 "c301"`,
391                 `GET /?code=302&next=308 ""`,
392                 `GET /?code=308 ""`,
393                 `GET / "c301"`,
394                 `DELETE /?code=302&next=302 "c302"`,
395                 `GET /?code=302 ""`,
396                 `GET / ""`,
397                 `DELETE /?code=303 "c303"`,
398                 `GET / ""`,
399                 `DELETE /?code=307&next=301,308,303,302,304 "c307"`,
400                 `DELETE /?code=301&next=308,303,302,304 "c307"`,
401                 `GET /?code=308&next=303,302,304 ""`,
402                 `GET /?code=303&next=302,304 "c307"`,
403                 `GET /?code=302&next=304 ""`,
404                 `GET /?code=304 ""`,
405                 `DELETE /?code=308&next=307 "c308"`,
406                 `DELETE /?code=307 "c308"`,
407                 `DELETE / "c308"`,
408                 `DELETE /?code=404 "c404"`,
409         }
410         want := strings.Join(wantSegments, "\n")
411         testRedirectsByMethod(t, "DELETE", deleteRedirectTests, want)
412 }
413
414 func testRedirectsByMethod(t *testing.T, method string, table []redirectTest, want string) {
415         defer afterTest(t)
416         var log struct {
417                 sync.Mutex
418                 bytes.Buffer
419         }
420         var ts *httptest.Server
421         ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
422                 log.Lock()
423                 slurp, _ := io.ReadAll(r.Body)
424                 fmt.Fprintf(&log.Buffer, "%s %s %q", r.Method, r.RequestURI, slurp)
425                 if cl := r.Header.Get("Content-Length"); r.Method == "GET" && len(slurp) == 0 && (r.ContentLength != 0 || cl != "") {
426                         fmt.Fprintf(&log.Buffer, " (but with body=%T, content-length = %v, %q)", r.Body, r.ContentLength, cl)
427                 }
428                 log.WriteByte('\n')
429                 log.Unlock()
430                 urlQuery := r.URL.Query()
431                 if v := urlQuery.Get("code"); v != "" {
432                         location := ts.URL
433                         if final := urlQuery.Get("next"); final != "" {
434                                 splits := strings.Split(final, ",")
435                                 first, rest := splits[0], splits[1:]
436                                 location = fmt.Sprintf("%s?code=%s", location, first)
437                                 if len(rest) > 0 {
438                                         location = fmt.Sprintf("%s&next=%s", location, strings.Join(rest, ","))
439                                 }
440                         }
441                         code, _ := strconv.Atoi(v)
442                         if code/100 == 3 {
443                                 w.Header().Set("Location", location)
444                         }
445                         w.WriteHeader(code)
446                 }
447         }))
448         defer ts.Close()
449
450         c := ts.Client()
451         for _, tt := range table {
452                 content := tt.redirectBody
453                 req, _ := NewRequest(method, ts.URL+tt.suffix, strings.NewReader(content))
454                 req.GetBody = func() (io.ReadCloser, error) { return io.NopCloser(strings.NewReader(content)), nil }
455                 res, err := c.Do(req)
456
457                 if err != nil {
458                         t.Fatal(err)
459                 }
460                 if res.StatusCode != tt.want {
461                         t.Errorf("POST %s: status code = %d; want %d", tt.suffix, res.StatusCode, tt.want)
462                 }
463         }
464         log.Lock()
465         got := log.String()
466         log.Unlock()
467
468         got = strings.TrimSpace(got)
469         want = strings.TrimSpace(want)
470
471         if got != want {
472                 got, want, lines := removeCommonLines(got, want)
473                 t.Errorf("Log differs after %d common lines.\n\nGot:\n%s\n\nWant:\n%s\n", lines, got, want)
474         }
475 }
476
477 func removeCommonLines(a, b string) (asuffix, bsuffix string, commonLines int) {
478         for {
479                 nl := strings.IndexByte(a, '\n')
480                 if nl < 0 {
481                         return a, b, commonLines
482                 }
483                 line := a[:nl+1]
484                 if !strings.HasPrefix(b, line) {
485                         return a, b, commonLines
486                 }
487                 commonLines++
488                 a = a[len(line):]
489                 b = b[len(line):]
490         }
491 }
492
493 func TestClientRedirectUseResponse(t *testing.T) {
494         setParallel(t)
495         defer afterTest(t)
496         const body = "Hello, world."
497         var ts *httptest.Server
498         ts = httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
499                 if strings.Contains(r.URL.Path, "/other") {
500                         io.WriteString(w, "wrong body")
501                 } else {
502                         w.Header().Set("Location", ts.URL+"/other")
503                         w.WriteHeader(StatusFound)
504                         io.WriteString(w, body)
505                 }
506         }))
507         defer ts.Close()
508
509         c := ts.Client()
510         c.CheckRedirect = func(req *Request, via []*Request) error {
511                 if req.Response == nil {
512                         t.Error("expected non-nil Request.Response")
513                 }
514                 return ErrUseLastResponse
515         }
516         res, err := c.Get(ts.URL)
517         if err != nil {
518                 t.Fatal(err)
519         }
520         if res.StatusCode != StatusFound {
521                 t.Errorf("status = %d; want %d", res.StatusCode, StatusFound)
522         }
523         defer res.Body.Close()
524         slurp, err := io.ReadAll(res.Body)
525         if err != nil {
526                 t.Fatal(err)
527         }
528         if string(slurp) != body {
529                 t.Errorf("body = %q; want %q", slurp, body)
530         }
531 }
532
533 // Issue 17773: don't follow a 308 (or 307) if the response doesn't
534 // have a Location header.
535 func TestClientRedirect308NoLocation(t *testing.T) {
536         setParallel(t)
537         defer afterTest(t)
538         ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
539                 w.Header().Set("Foo", "Bar")
540                 w.WriteHeader(308)
541         }))
542         defer ts.Close()
543         c := ts.Client()
544         res, err := c.Get(ts.URL)
545         if err != nil {
546                 t.Fatal(err)
547         }
548         res.Body.Close()
549         if res.StatusCode != 308 {
550                 t.Errorf("status = %d; want %d", res.StatusCode, 308)
551         }
552         if got := res.Header.Get("Foo"); got != "Bar" {
553                 t.Errorf("Foo header = %q; want Bar", got)
554         }
555 }
556
557 // Don't follow a 307/308 if we can't resent the request body.
558 func TestClientRedirect308NoGetBody(t *testing.T) {
559         setParallel(t)
560         defer afterTest(t)
561         const fakeURL = "https://localhost:1234/" // won't be hit
562         ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
563                 w.Header().Set("Location", fakeURL)
564                 w.WriteHeader(308)
565         }))
566         defer ts.Close()
567         req, err := NewRequest("POST", ts.URL, strings.NewReader("some body"))
568         if err != nil {
569                 t.Fatal(err)
570         }
571         c := ts.Client()
572         req.GetBody = nil // so it can't rewind.
573         res, err := c.Do(req)
574         if err != nil {
575                 t.Fatal(err)
576         }
577         res.Body.Close()
578         if res.StatusCode != 308 {
579                 t.Errorf("status = %d; want %d", res.StatusCode, 308)
580         }
581         if got := res.Header.Get("Location"); got != fakeURL {
582                 t.Errorf("Location header = %q; want %q", got, fakeURL)
583         }
584 }
585
586 var expectedCookies = []*Cookie{
587         {Name: "ChocolateChip", Value: "tasty"},
588         {Name: "First", Value: "Hit"},
589         {Name: "Second", Value: "Hit"},
590 }
591
592 var echoCookiesRedirectHandler = HandlerFunc(func(w ResponseWriter, r *Request) {
593         for _, cookie := range r.Cookies() {
594                 SetCookie(w, cookie)
595         }
596         if r.URL.Path == "/" {
597                 SetCookie(w, expectedCookies[1])
598                 Redirect(w, r, "/second", StatusMovedPermanently)
599         } else {
600                 SetCookie(w, expectedCookies[2])
601                 w.Write([]byte("hello"))
602         }
603 })
604
605 func TestClientSendsCookieFromJar(t *testing.T) {
606         defer afterTest(t)
607         tr := &recordingTransport{}
608         client := &Client{Transport: tr}
609         client.Jar = &TestJar{perURL: make(map[string][]*Cookie)}
610         us := "http://dummy.faketld/"
611         u, _ := url.Parse(us)
612         client.Jar.SetCookies(u, expectedCookies)
613
614         client.Get(us) // Note: doesn't hit network
615         matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
616
617         client.Head(us) // Note: doesn't hit network
618         matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
619
620         client.Post(us, "text/plain", strings.NewReader("body")) // Note: doesn't hit network
621         matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
622
623         client.PostForm(us, url.Values{}) // Note: doesn't hit network
624         matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
625
626         req, _ := NewRequest("GET", us, nil)
627         client.Do(req) // Note: doesn't hit network
628         matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
629
630         req, _ = NewRequest("POST", us, nil)
631         client.Do(req) // Note: doesn't hit network
632         matchReturnedCookies(t, expectedCookies, tr.req.Cookies())
633 }
634
635 // Just enough correctness for our redirect tests. Uses the URL.Host as the
636 // scope of all cookies.
637 type TestJar struct {
638         m      sync.Mutex
639         perURL map[string][]*Cookie
640 }
641
642 func (j *TestJar) SetCookies(u *url.URL, cookies []*Cookie) {
643         j.m.Lock()
644         defer j.m.Unlock()
645         if j.perURL == nil {
646                 j.perURL = make(map[string][]*Cookie)
647         }
648         j.perURL[u.Host] = cookies
649 }
650
651 func (j *TestJar) Cookies(u *url.URL) []*Cookie {
652         j.m.Lock()
653         defer j.m.Unlock()
654         return j.perURL[u.Host]
655 }
656
657 func TestRedirectCookiesJar(t *testing.T) {
658         setParallel(t)
659         defer afterTest(t)
660         var ts *httptest.Server
661         ts = httptest.NewServer(echoCookiesRedirectHandler)
662         defer ts.Close()
663         c := ts.Client()
664         c.Jar = new(TestJar)
665         u, _ := url.Parse(ts.URL)
666         c.Jar.SetCookies(u, []*Cookie{expectedCookies[0]})
667         resp, err := c.Get(ts.URL)
668         if err != nil {
669                 t.Fatalf("Get: %v", err)
670         }
671         resp.Body.Close()
672         matchReturnedCookies(t, expectedCookies, resp.Cookies())
673 }
674
675 func matchReturnedCookies(t *testing.T, expected, given []*Cookie) {
676         if len(given) != len(expected) {
677                 t.Logf("Received cookies: %v", given)
678                 t.Errorf("Expected %d cookies, got %d", len(expected), len(given))
679         }
680         for _, ec := range expected {
681                 foundC := false
682                 for _, c := range given {
683                         if ec.Name == c.Name && ec.Value == c.Value {
684                                 foundC = true
685                                 break
686                         }
687                 }
688                 if !foundC {
689                         t.Errorf("Missing cookie %v", ec)
690                 }
691         }
692 }
693
694 func TestJarCalls(t *testing.T) {
695         defer afterTest(t)
696         ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
697                 pathSuffix := r.RequestURI[1:]
698                 if r.RequestURI == "/nosetcookie" {
699                         return // don't set cookies for this path
700                 }
701                 SetCookie(w, &Cookie{Name: "name" + pathSuffix, Value: "val" + pathSuffix})
702                 if r.RequestURI == "/" {
703                         Redirect(w, r, "http://secondhost.fake/secondpath", 302)
704                 }
705         }))
706         defer ts.Close()
707         jar := new(RecordingJar)
708         c := ts.Client()
709         c.Jar = jar
710         c.Transport.(*Transport).Dial = func(_ string, _ string) (net.Conn, error) {
711                 return net.Dial("tcp", ts.Listener.Addr().String())
712         }
713         _, err := c.Get("http://firsthost.fake/")
714         if err != nil {
715                 t.Fatal(err)
716         }
717         _, err = c.Get("http://firsthost.fake/nosetcookie")
718         if err != nil {
719                 t.Fatal(err)
720         }
721         got := jar.log.String()
722         want := `Cookies("http://firsthost.fake/")
723 SetCookie("http://firsthost.fake/", [name=val])
724 Cookies("http://secondhost.fake/secondpath")
725 SetCookie("http://secondhost.fake/secondpath", [namesecondpath=valsecondpath])
726 Cookies("http://firsthost.fake/nosetcookie")
727 `
728         if got != want {
729                 t.Errorf("Got Jar calls:\n%s\nWant:\n%s", got, want)
730         }
731 }
732
733 // RecordingJar keeps a log of calls made to it, without
734 // tracking any cookies.
735 type RecordingJar struct {
736         mu  sync.Mutex
737         log bytes.Buffer
738 }
739
740 func (j *RecordingJar) SetCookies(u *url.URL, cookies []*Cookie) {
741         j.logf("SetCookie(%q, %v)\n", u, cookies)
742 }
743
744 func (j *RecordingJar) Cookies(u *url.URL) []*Cookie {
745         j.logf("Cookies(%q)\n", u)
746         return nil
747 }
748
749 func (j *RecordingJar) logf(format string, args ...interface{}) {
750         j.mu.Lock()
751         defer j.mu.Unlock()
752         fmt.Fprintf(&j.log, format, args...)
753 }
754
755 func TestStreamingGet_h1(t *testing.T) { testStreamingGet(t, h1Mode) }
756 func TestStreamingGet_h2(t *testing.T) { testStreamingGet(t, h2Mode) }
757
758 func testStreamingGet(t *testing.T, h2 bool) {
759         defer afterTest(t)
760         say := make(chan string)
761         cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
762                 w.(Flusher).Flush()
763                 for str := range say {
764                         w.Write([]byte(str))
765                         w.(Flusher).Flush()
766                 }
767         }))
768         defer cst.close()
769
770         c := cst.c
771         res, err := c.Get(cst.ts.URL)
772         if err != nil {
773                 t.Fatal(err)
774         }
775         var buf [10]byte
776         for _, str := range []string{"i", "am", "also", "known", "as", "comet"} {
777                 say <- str
778                 n, err := io.ReadFull(res.Body, buf[0:len(str)])
779                 if err != nil {
780                         t.Fatalf("ReadFull on %q: %v", str, err)
781                 }
782                 if n != len(str) {
783                         t.Fatalf("Receiving %q, only read %d bytes", str, n)
784                 }
785                 got := string(buf[0:n])
786                 if got != str {
787                         t.Fatalf("Expected %q, got %q", str, got)
788                 }
789         }
790         close(say)
791         _, err = io.ReadFull(res.Body, buf[0:1])
792         if err != io.EOF {
793                 t.Fatalf("at end expected EOF, got %v", err)
794         }
795 }
796
797 type writeCountingConn struct {
798         net.Conn
799         count *int
800 }
801
802 func (c *writeCountingConn) Write(p []byte) (int, error) {
803         *c.count++
804         return c.Conn.Write(p)
805 }
806
807 // TestClientWrites verifies that client requests are buffered and we
808 // don't send a TCP packet per line of the http request + body.
809 func TestClientWrites(t *testing.T) {
810         defer afterTest(t)
811         ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
812         }))
813         defer ts.Close()
814
815         writes := 0
816         dialer := func(netz string, addr string) (net.Conn, error) {
817                 c, err := net.Dial(netz, addr)
818                 if err == nil {
819                         c = &writeCountingConn{c, &writes}
820                 }
821                 return c, err
822         }
823         c := ts.Client()
824         c.Transport.(*Transport).Dial = dialer
825
826         _, err := c.Get(ts.URL)
827         if err != nil {
828                 t.Fatal(err)
829         }
830         if writes != 1 {
831                 t.Errorf("Get request did %d Write calls, want 1", writes)
832         }
833
834         writes = 0
835         _, err = c.PostForm(ts.URL, url.Values{"foo": {"bar"}})
836         if err != nil {
837                 t.Fatal(err)
838         }
839         if writes != 1 {
840                 t.Errorf("Post request did %d Write calls, want 1", writes)
841         }
842 }
843
844 func TestClientInsecureTransport(t *testing.T) {
845         setParallel(t)
846         defer afterTest(t)
847         ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
848                 w.Write([]byte("Hello"))
849         }))
850         errc := make(chanWriter, 10) // but only expecting 1
851         ts.Config.ErrorLog = log.New(errc, "", 0)
852         defer ts.Close()
853
854         // TODO(bradfitz): add tests for skipping hostname checks too?
855         // would require a new cert for testing, and probably
856         // redundant with these tests.
857         c := ts.Client()
858         for _, insecure := range []bool{true, false} {
859                 c.Transport.(*Transport).TLSClientConfig = &tls.Config{
860                         InsecureSkipVerify: insecure,
861                 }
862                 res, err := c.Get(ts.URL)
863                 if (err == nil) != insecure {
864                         t.Errorf("insecure=%v: got unexpected err=%v", insecure, err)
865                 }
866                 if res != nil {
867                         res.Body.Close()
868                 }
869         }
870
871         select {
872         case v := <-errc:
873                 if !strings.Contains(v, "TLS handshake error") {
874                         t.Errorf("expected an error log message containing 'TLS handshake error'; got %q", v)
875                 }
876         case <-time.After(5 * time.Second):
877                 t.Errorf("timeout waiting for logged error")
878         }
879
880 }
881
882 func TestClientErrorWithRequestURI(t *testing.T) {
883         defer afterTest(t)
884         req, _ := NewRequest("GET", "http://localhost:1234/", nil)
885         req.RequestURI = "/this/field/is/illegal/and/should/error/"
886         _, err := DefaultClient.Do(req)
887         if err == nil {
888                 t.Fatalf("expected an error")
889         }
890         if !strings.Contains(err.Error(), "RequestURI") {
891                 t.Errorf("wanted error mentioning RequestURI; got error: %v", err)
892         }
893 }
894
895 func TestClientWithCorrectTLSServerName(t *testing.T) {
896         defer afterTest(t)
897
898         const serverName = "example.com"
899         ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
900                 if r.TLS.ServerName != serverName {
901                         t.Errorf("expected client to set ServerName %q, got: %q", serverName, r.TLS.ServerName)
902                 }
903         }))
904         defer ts.Close()
905
906         c := ts.Client()
907         c.Transport.(*Transport).TLSClientConfig.ServerName = serverName
908         if _, err := c.Get(ts.URL); err != nil {
909                 t.Fatalf("expected successful TLS connection, got error: %v", err)
910         }
911 }
912
913 func TestClientWithIncorrectTLSServerName(t *testing.T) {
914         defer afterTest(t)
915         ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
916         defer ts.Close()
917         errc := make(chanWriter, 10) // but only expecting 1
918         ts.Config.ErrorLog = log.New(errc, "", 0)
919
920         c := ts.Client()
921         c.Transport.(*Transport).TLSClientConfig.ServerName = "badserver"
922         _, err := c.Get(ts.URL)
923         if err == nil {
924                 t.Fatalf("expected an error")
925         }
926         if !strings.Contains(err.Error(), "127.0.0.1") || !strings.Contains(err.Error(), "badserver") {
927                 t.Errorf("wanted error mentioning 127.0.0.1 and badserver; got error: %v", err)
928         }
929         select {
930         case v := <-errc:
931                 if !strings.Contains(v, "TLS handshake error") {
932                         t.Errorf("expected an error log message containing 'TLS handshake error'; got %q", v)
933                 }
934         case <-time.After(5 * time.Second):
935                 t.Errorf("timeout waiting for logged error")
936         }
937 }
938
939 // Test for golang.org/issue/5829; the Transport should respect TLSClientConfig.ServerName
940 // when not empty.
941 //
942 // tls.Config.ServerName (non-empty, set to "example.com") takes
943 // precedence over "some-other-host.tld" which previously incorrectly
944 // took precedence. We don't actually connect to (or even resolve)
945 // "some-other-host.tld", though, because of the Transport.Dial hook.
946 //
947 // The httptest.Server has a cert with "example.com" as its name.
948 func TestTransportUsesTLSConfigServerName(t *testing.T) {
949         defer afterTest(t)
950         ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
951                 w.Write([]byte("Hello"))
952         }))
953         defer ts.Close()
954
955         c := ts.Client()
956         tr := c.Transport.(*Transport)
957         tr.TLSClientConfig.ServerName = "example.com" // one of httptest's Server cert names
958         tr.Dial = func(netw, addr string) (net.Conn, error) {
959                 return net.Dial(netw, ts.Listener.Addr().String())
960         }
961         res, err := c.Get("https://some-other-host.tld/")
962         if err != nil {
963                 t.Fatal(err)
964         }
965         res.Body.Close()
966 }
967
968 func TestResponseSetsTLSConnectionState(t *testing.T) {
969         defer afterTest(t)
970         ts := httptest.NewTLSServer(HandlerFunc(func(w ResponseWriter, r *Request) {
971                 w.Write([]byte("Hello"))
972         }))
973         defer ts.Close()
974
975         c := ts.Client()
976         tr := c.Transport.(*Transport)
977         tr.TLSClientConfig.CipherSuites = []uint16{tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA}
978         tr.TLSClientConfig.MaxVersion = tls.VersionTLS12 // to get to pick the cipher suite
979         tr.Dial = func(netw, addr string) (net.Conn, error) {
980                 return net.Dial(netw, ts.Listener.Addr().String())
981         }
982         res, err := c.Get("https://example.com/")
983         if err != nil {
984                 t.Fatal(err)
985         }
986         defer res.Body.Close()
987         if res.TLS == nil {
988                 t.Fatal("Response didn't set TLS Connection State.")
989         }
990         if got, want := res.TLS.CipherSuite, tls.TLS_RSA_WITH_3DES_EDE_CBC_SHA; got != want {
991                 t.Errorf("TLS Cipher Suite = %d; want %d", got, want)
992         }
993 }
994
995 // Check that an HTTPS client can interpret a particular TLS error
996 // to determine that the server is speaking HTTP.
997 // See golang.org/issue/11111.
998 func TestHTTPSClientDetectsHTTPServer(t *testing.T) {
999         defer afterTest(t)
1000         ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
1001         ts.Config.ErrorLog = quietLog
1002         defer ts.Close()
1003
1004         _, err := Get(strings.Replace(ts.URL, "http", "https", 1))
1005         if got := err.Error(); !strings.Contains(got, "HTTP response to HTTPS client") {
1006                 t.Fatalf("error = %q; want error indicating HTTP response to HTTPS request", got)
1007         }
1008 }
1009
1010 // Verify Response.ContentLength is populated. https://golang.org/issue/4126
1011 func TestClientHeadContentLength_h1(t *testing.T) {
1012         testClientHeadContentLength(t, h1Mode)
1013 }
1014
1015 func TestClientHeadContentLength_h2(t *testing.T) {
1016         testClientHeadContentLength(t, h2Mode)
1017 }
1018
1019 func testClientHeadContentLength(t *testing.T, h2 bool) {
1020         defer afterTest(t)
1021         cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1022                 if v := r.FormValue("cl"); v != "" {
1023                         w.Header().Set("Content-Length", v)
1024                 }
1025         }))
1026         defer cst.close()
1027         tests := []struct {
1028                 suffix string
1029                 want   int64
1030         }{
1031                 {"/?cl=1234", 1234},
1032                 {"/?cl=0", 0},
1033                 {"", -1},
1034         }
1035         for _, tt := range tests {
1036                 req, _ := NewRequest("HEAD", cst.ts.URL+tt.suffix, nil)
1037                 res, err := cst.c.Do(req)
1038                 if err != nil {
1039                         t.Fatal(err)
1040                 }
1041                 if res.ContentLength != tt.want {
1042                         t.Errorf("Content-Length = %d; want %d", res.ContentLength, tt.want)
1043                 }
1044                 bs, err := io.ReadAll(res.Body)
1045                 if err != nil {
1046                         t.Fatal(err)
1047                 }
1048                 if len(bs) != 0 {
1049                         t.Errorf("Unexpected content: %q", bs)
1050                 }
1051         }
1052 }
1053
1054 func TestEmptyPasswordAuth(t *testing.T) {
1055         setParallel(t)
1056         defer afterTest(t)
1057         gopher := "gopher"
1058         ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
1059                 auth := r.Header.Get("Authorization")
1060                 if strings.HasPrefix(auth, "Basic ") {
1061                         encoded := auth[6:]
1062                         decoded, err := base64.StdEncoding.DecodeString(encoded)
1063                         if err != nil {
1064                                 t.Fatal(err)
1065                         }
1066                         expected := gopher + ":"
1067                         s := string(decoded)
1068                         if expected != s {
1069                                 t.Errorf("Invalid Authorization header. Got %q, wanted %q", s, expected)
1070                         }
1071                 } else {
1072                         t.Errorf("Invalid auth %q", auth)
1073                 }
1074         }))
1075         defer ts.Close()
1076         req, err := NewRequest("GET", ts.URL, nil)
1077         if err != nil {
1078                 t.Fatal(err)
1079         }
1080         req.URL.User = url.User(gopher)
1081         c := ts.Client()
1082         resp, err := c.Do(req)
1083         if err != nil {
1084                 t.Fatal(err)
1085         }
1086         defer resp.Body.Close()
1087 }
1088
1089 func TestBasicAuth(t *testing.T) {
1090         defer afterTest(t)
1091         tr := &recordingTransport{}
1092         client := &Client{Transport: tr}
1093
1094         url := "http://My%20User:My%20Pass@dummy.faketld/"
1095         expected := "My User:My Pass"
1096         client.Get(url)
1097
1098         if tr.req.Method != "GET" {
1099                 t.Errorf("got method %q, want %q", tr.req.Method, "GET")
1100         }
1101         if tr.req.URL.String() != url {
1102                 t.Errorf("got URL %q, want %q", tr.req.URL.String(), url)
1103         }
1104         if tr.req.Header == nil {
1105                 t.Fatalf("expected non-nil request Header")
1106         }
1107         auth := tr.req.Header.Get("Authorization")
1108         if strings.HasPrefix(auth, "Basic ") {
1109                 encoded := auth[6:]
1110                 decoded, err := base64.StdEncoding.DecodeString(encoded)
1111                 if err != nil {
1112                         t.Fatal(err)
1113                 }
1114                 s := string(decoded)
1115                 if expected != s {
1116                         t.Errorf("Invalid Authorization header. Got %q, wanted %q", s, expected)
1117                 }
1118         } else {
1119                 t.Errorf("Invalid auth %q", auth)
1120         }
1121 }
1122
1123 func TestBasicAuthHeadersPreserved(t *testing.T) {
1124         defer afterTest(t)
1125         tr := &recordingTransport{}
1126         client := &Client{Transport: tr}
1127
1128         // If Authorization header is provided, username in URL should not override it
1129         url := "http://My%20User@dummy.faketld/"
1130         req, err := NewRequest("GET", url, nil)
1131         if err != nil {
1132                 t.Fatal(err)
1133         }
1134         req.SetBasicAuth("My User", "My Pass")
1135         expected := "My User:My Pass"
1136         client.Do(req)
1137
1138         if tr.req.Method != "GET" {
1139                 t.Errorf("got method %q, want %q", tr.req.Method, "GET")
1140         }
1141         if tr.req.URL.String() != url {
1142                 t.Errorf("got URL %q, want %q", tr.req.URL.String(), url)
1143         }
1144         if tr.req.Header == nil {
1145                 t.Fatalf("expected non-nil request Header")
1146         }
1147         auth := tr.req.Header.Get("Authorization")
1148         if strings.HasPrefix(auth, "Basic ") {
1149                 encoded := auth[6:]
1150                 decoded, err := base64.StdEncoding.DecodeString(encoded)
1151                 if err != nil {
1152                         t.Fatal(err)
1153                 }
1154                 s := string(decoded)
1155                 if expected != s {
1156                         t.Errorf("Invalid Authorization header. Got %q, wanted %q", s, expected)
1157                 }
1158         } else {
1159                 t.Errorf("Invalid auth %q", auth)
1160         }
1161
1162 }
1163
1164 func TestStripPasswordFromError(t *testing.T) {
1165         client := &Client{Transport: &recordingTransport{}}
1166         testCases := []struct {
1167                 desc string
1168                 in   string
1169                 out  string
1170         }{
1171                 {
1172                         desc: "Strip password from error message",
1173                         in:   "http://user:password@dummy.faketld/",
1174                         out:  `Get "http://user:***@dummy.faketld/": dummy impl`,
1175                 },
1176                 {
1177                         desc: "Don't Strip password from domain name",
1178                         in:   "http://user:password@password.faketld/",
1179                         out:  `Get "http://user:***@password.faketld/": dummy impl`,
1180                 },
1181                 {
1182                         desc: "Don't Strip password from path",
1183                         in:   "http://user:password@dummy.faketld/password",
1184                         out:  `Get "http://user:***@dummy.faketld/password": dummy impl`,
1185                 },
1186                 {
1187                         desc: "Strip escaped password",
1188                         in:   "http://user:pa%2Fssword@dummy.faketld/",
1189                         out:  `Get "http://user:***@dummy.faketld/": dummy impl`,
1190                 },
1191         }
1192         for _, tC := range testCases {
1193                 t.Run(tC.desc, func(t *testing.T) {
1194                         _, err := client.Get(tC.in)
1195                         if err.Error() != tC.out {
1196                                 t.Errorf("Unexpected output for %q: expected %q, actual %q",
1197                                         tC.in, tC.out, err.Error())
1198                         }
1199                 })
1200         }
1201 }
1202
1203 func TestClientTimeout_h1(t *testing.T) { testClientTimeout(t, h1Mode) }
1204 func TestClientTimeout_h2(t *testing.T) { testClientTimeout(t, h2Mode) }
1205
1206 func testClientTimeout(t *testing.T, h2 bool) {
1207         setParallel(t)
1208         defer afterTest(t)
1209         testDone := make(chan struct{}) // closed in defer below
1210
1211         sawRoot := make(chan bool, 1)
1212         sawSlow := make(chan bool, 1)
1213         cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1214                 if r.URL.Path == "/" {
1215                         sawRoot <- true
1216                         Redirect(w, r, "/slow", StatusFound)
1217                         return
1218                 }
1219                 if r.URL.Path == "/slow" {
1220                         sawSlow <- true
1221                         w.Write([]byte("Hello"))
1222                         w.(Flusher).Flush()
1223                         <-testDone
1224                         return
1225                 }
1226         }))
1227         defer cst.close()
1228         defer close(testDone) // before cst.close, to unblock /slow handler
1229
1230         // 200ms should be long enough to get a normal request (the /
1231         // handler), but not so long that it makes the test slow.
1232         const timeout = 200 * time.Millisecond
1233         cst.c.Timeout = timeout
1234
1235         res, err := cst.c.Get(cst.ts.URL)
1236         if err != nil {
1237                 if strings.Contains(err.Error(), "Client.Timeout") {
1238                         t.Skipf("host too slow to get fast resource in %v", timeout)
1239                 }
1240                 t.Fatal(err)
1241         }
1242
1243         select {
1244         case <-sawRoot:
1245                 // good.
1246         default:
1247                 t.Fatal("handler never got / request")
1248         }
1249
1250         select {
1251         case <-sawSlow:
1252                 // good.
1253         default:
1254                 t.Fatal("handler never got /slow request")
1255         }
1256
1257         errc := make(chan error, 1)
1258         go func() {
1259                 _, err := io.ReadAll(res.Body)
1260                 errc <- err
1261                 res.Body.Close()
1262         }()
1263
1264         const failTime = 5 * time.Second
1265         select {
1266         case err := <-errc:
1267                 if err == nil {
1268                         t.Fatal("expected error from ReadAll")
1269                 }
1270                 ne, ok := err.(net.Error)
1271                 if !ok {
1272                         t.Errorf("error value from ReadAll was %T; expected some net.Error", err)
1273                 } else if !ne.Timeout() {
1274                         t.Errorf("net.Error.Timeout = false; want true")
1275                 }
1276                 if got := ne.Error(); !strings.Contains(got, "(Client.Timeout") {
1277                         t.Errorf("error string = %q; missing timeout substring", got)
1278                 }
1279         case <-time.After(failTime):
1280                 t.Errorf("timeout after %v waiting for timeout of %v", failTime, timeout)
1281         }
1282 }
1283
1284 func TestClientTimeout_Headers_h1(t *testing.T) { testClientTimeout_Headers(t, h1Mode) }
1285 func TestClientTimeout_Headers_h2(t *testing.T) { testClientTimeout_Headers(t, h2Mode) }
1286
1287 // Client.Timeout firing before getting to the body
1288 func testClientTimeout_Headers(t *testing.T, h2 bool) {
1289         setParallel(t)
1290         defer afterTest(t)
1291         donec := make(chan bool, 1)
1292         cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1293                 <-donec
1294         }), optQuietLog)
1295         defer cst.close()
1296         // Note that we use a channel send here and not a close.
1297         // The race detector doesn't know that we're waiting for a timeout
1298         // and thinks that the waitgroup inside httptest.Server is added to concurrently
1299         // with us closing it. If we timed out immediately, we could close the testserver
1300         // before we entered the handler. We're not timing out immediately and there's
1301         // no way we would be done before we entered the handler, but the race detector
1302         // doesn't know this, so synchronize explicitly.
1303         defer func() { donec <- true }()
1304
1305         cst.c.Timeout = 5 * time.Millisecond
1306         res, err := cst.c.Get(cst.ts.URL)
1307         if err == nil {
1308                 res.Body.Close()
1309                 t.Fatal("got response from Get; expected error")
1310         }
1311         if _, ok := err.(*url.Error); !ok {
1312                 t.Fatalf("Got error of type %T; want *url.Error", err)
1313         }
1314         ne, ok := err.(net.Error)
1315         if !ok {
1316                 t.Fatalf("Got error of type %T; want some net.Error", err)
1317         }
1318         if !ne.Timeout() {
1319                 t.Error("net.Error.Timeout = false; want true")
1320         }
1321         if got := ne.Error(); !strings.Contains(got, "Client.Timeout exceeded") {
1322                 t.Errorf("error string = %q; missing timeout substring", got)
1323         }
1324 }
1325
1326 // Issue 16094: if Client.Timeout is set but not hit, a Timeout error shouldn't be
1327 // returned.
1328 func TestClientTimeoutCancel(t *testing.T) {
1329         setParallel(t)
1330         defer afterTest(t)
1331
1332         testDone := make(chan struct{})
1333         ctx, cancel := context.WithCancel(context.Background())
1334
1335         cst := newClientServerTest(t, h1Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
1336                 w.(Flusher).Flush()
1337                 <-testDone
1338         }))
1339         defer cst.close()
1340         defer close(testDone)
1341
1342         cst.c.Timeout = 1 * time.Hour
1343         req, _ := NewRequest("GET", cst.ts.URL, nil)
1344         req.Cancel = ctx.Done()
1345         res, err := cst.c.Do(req)
1346         if err != nil {
1347                 t.Fatal(err)
1348         }
1349         cancel()
1350         _, err = io.Copy(io.Discard, res.Body)
1351         if err != ExportErrRequestCanceled {
1352                 t.Fatalf("error = %v; want errRequestCanceled", err)
1353         }
1354 }
1355
1356 func TestClientRedirectEatsBody_h1(t *testing.T) { testClientRedirectEatsBody(t, h1Mode) }
1357 func TestClientRedirectEatsBody_h2(t *testing.T) { testClientRedirectEatsBody(t, h2Mode) }
1358 func testClientRedirectEatsBody(t *testing.T, h2 bool) {
1359         setParallel(t)
1360         defer afterTest(t)
1361         saw := make(chan string, 2)
1362         cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1363                 saw <- r.RemoteAddr
1364                 if r.URL.Path == "/" {
1365                         Redirect(w, r, "/foo", StatusFound) // which includes a body
1366                 }
1367         }))
1368         defer cst.close()
1369
1370         res, err := cst.c.Get(cst.ts.URL)
1371         if err != nil {
1372                 t.Fatal(err)
1373         }
1374         _, err = io.ReadAll(res.Body)
1375         res.Body.Close()
1376         if err != nil {
1377                 t.Fatal(err)
1378         }
1379
1380         var first string
1381         select {
1382         case first = <-saw:
1383         default:
1384                 t.Fatal("server didn't see a request")
1385         }
1386
1387         var second string
1388         select {
1389         case second = <-saw:
1390         default:
1391                 t.Fatal("server didn't see a second request")
1392         }
1393
1394         if first != second {
1395                 t.Fatal("server saw different client ports before & after the redirect")
1396         }
1397 }
1398
1399 // eofReaderFunc is an io.Reader that runs itself, and then returns io.EOF.
1400 type eofReaderFunc func()
1401
1402 func (f eofReaderFunc) Read(p []byte) (n int, err error) {
1403         f()
1404         return 0, io.EOF
1405 }
1406
1407 func TestReferer(t *testing.T) {
1408         tests := []struct {
1409                 lastReq, newReq string // from -> to URLs
1410                 want            string
1411         }{
1412                 // don't send user:
1413                 {"http://gopher@test.com", "http://link.com", "http://test.com"},
1414                 {"https://gopher@test.com", "https://link.com", "https://test.com"},
1415
1416                 // don't send a user and password:
1417                 {"http://gopher:go@test.com", "http://link.com", "http://test.com"},
1418                 {"https://gopher:go@test.com", "https://link.com", "https://test.com"},
1419
1420                 // nothing to do:
1421                 {"http://test.com", "http://link.com", "http://test.com"},
1422                 {"https://test.com", "https://link.com", "https://test.com"},
1423
1424                 // https to http doesn't send a referer:
1425                 {"https://test.com", "http://link.com", ""},
1426                 {"https://gopher:go@test.com", "http://link.com", ""},
1427         }
1428         for _, tt := range tests {
1429                 l, err := url.Parse(tt.lastReq)
1430                 if err != nil {
1431                         t.Fatal(err)
1432                 }
1433                 n, err := url.Parse(tt.newReq)
1434                 if err != nil {
1435                         t.Fatal(err)
1436                 }
1437                 r := ExportRefererForURL(l, n)
1438                 if r != tt.want {
1439                         t.Errorf("refererForURL(%q, %q) = %q; want %q", tt.lastReq, tt.newReq, r, tt.want)
1440                 }
1441         }
1442 }
1443
1444 // issue15577Tripper returns a Response with a redirect response
1445 // header and doesn't populate its Response.Request field.
1446 type issue15577Tripper struct{}
1447
1448 func (issue15577Tripper) RoundTrip(*Request) (*Response, error) {
1449         resp := &Response{
1450                 StatusCode: 303,
1451                 Header:     map[string][]string{"Location": {"http://www.example.com/"}},
1452                 Body:       io.NopCloser(strings.NewReader("")),
1453         }
1454         return resp, nil
1455 }
1456
1457 // Issue 15577: don't assume the roundtripper's response populates its Request field.
1458 func TestClientRedirectResponseWithoutRequest(t *testing.T) {
1459         c := &Client{
1460                 CheckRedirect: func(*Request, []*Request) error { return fmt.Errorf("no redirects!") },
1461                 Transport:     issue15577Tripper{},
1462         }
1463         // Check that this doesn't crash:
1464         c.Get("http://dummy.tld")
1465 }
1466
1467 // Issue 4800: copy (some) headers when Client follows a redirect.
1468 func TestClientCopyHeadersOnRedirect(t *testing.T) {
1469         const (
1470                 ua   = "some-agent/1.2"
1471                 xfoo = "foo-val"
1472         )
1473         var ts2URL string
1474         ts1 := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
1475                 want := Header{
1476                         "User-Agent":      []string{ua},
1477                         "X-Foo":           []string{xfoo},
1478                         "Referer":         []string{ts2URL},
1479                         "Accept-Encoding": []string{"gzip"},
1480                 }
1481                 if !reflect.DeepEqual(r.Header, want) {
1482                         t.Errorf("Request.Header = %#v; want %#v", r.Header, want)
1483                 }
1484                 if t.Failed() {
1485                         w.Header().Set("Result", "got errors")
1486                 } else {
1487                         w.Header().Set("Result", "ok")
1488                 }
1489         }))
1490         defer ts1.Close()
1491         ts2 := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
1492                 Redirect(w, r, ts1.URL, StatusFound)
1493         }))
1494         defer ts2.Close()
1495         ts2URL = ts2.URL
1496
1497         c := ts1.Client()
1498         c.CheckRedirect = func(r *Request, via []*Request) error {
1499                 want := Header{
1500                         "User-Agent": []string{ua},
1501                         "X-Foo":      []string{xfoo},
1502                         "Referer":    []string{ts2URL},
1503                 }
1504                 if !reflect.DeepEqual(r.Header, want) {
1505                         t.Errorf("CheckRedirect Request.Header = %#v; want %#v", r.Header, want)
1506                 }
1507                 return nil
1508         }
1509
1510         req, _ := NewRequest("GET", ts2.URL, nil)
1511         req.Header.Add("User-Agent", ua)
1512         req.Header.Add("X-Foo", xfoo)
1513         req.Header.Add("Cookie", "foo=bar")
1514         req.Header.Add("Authorization", "secretpassword")
1515         res, err := c.Do(req)
1516         if err != nil {
1517                 t.Fatal(err)
1518         }
1519         defer res.Body.Close()
1520         if res.StatusCode != 200 {
1521                 t.Fatal(res.Status)
1522         }
1523         if got := res.Header.Get("Result"); got != "ok" {
1524                 t.Errorf("result = %q; want ok", got)
1525         }
1526 }
1527
1528 // Issue 22233: copy host when Client follows a relative redirect.
1529 func TestClientCopyHostOnRedirect(t *testing.T) {
1530         // Virtual hostname: should not receive any request.
1531         virtual := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
1532                 t.Errorf("Virtual host received request %v", r.URL)
1533                 w.WriteHeader(403)
1534                 io.WriteString(w, "should not see this response")
1535         }))
1536         defer virtual.Close()
1537         virtualHost := strings.TrimPrefix(virtual.URL, "http://")
1538         t.Logf("Virtual host is %v", virtualHost)
1539
1540         // Actual hostname: should not receive any request.
1541         const wantBody = "response body"
1542         var tsURL string
1543         var tsHost string
1544         ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
1545                 switch r.URL.Path {
1546                 case "/":
1547                         // Relative redirect.
1548                         if r.Host != virtualHost {
1549                                 t.Errorf("Serving /: Request.Host = %#v; want %#v", r.Host, virtualHost)
1550                                 w.WriteHeader(404)
1551                                 return
1552                         }
1553                         w.Header().Set("Location", "/hop")
1554                         w.WriteHeader(302)
1555                 case "/hop":
1556                         // Absolute redirect.
1557                         if r.Host != virtualHost {
1558                                 t.Errorf("Serving /hop: Request.Host = %#v; want %#v", r.Host, virtualHost)
1559                                 w.WriteHeader(404)
1560                                 return
1561                         }
1562                         w.Header().Set("Location", tsURL+"/final")
1563                         w.WriteHeader(302)
1564                 case "/final":
1565                         if r.Host != tsHost {
1566                                 t.Errorf("Serving /final: Request.Host = %#v; want %#v", r.Host, tsHost)
1567                                 w.WriteHeader(404)
1568                                 return
1569                         }
1570                         w.WriteHeader(200)
1571                         io.WriteString(w, wantBody)
1572                 default:
1573                         t.Errorf("Serving unexpected path %q", r.URL.Path)
1574                         w.WriteHeader(404)
1575                 }
1576         }))
1577         defer ts.Close()
1578         tsURL = ts.URL
1579         tsHost = strings.TrimPrefix(ts.URL, "http://")
1580         t.Logf("Server host is %v", tsHost)
1581
1582         c := ts.Client()
1583         req, _ := NewRequest("GET", ts.URL, nil)
1584         req.Host = virtualHost
1585         resp, err := c.Do(req)
1586         if err != nil {
1587                 t.Fatal(err)
1588         }
1589         defer resp.Body.Close()
1590         if resp.StatusCode != 200 {
1591                 t.Fatal(resp.Status)
1592         }
1593         if got, err := io.ReadAll(resp.Body); err != nil || string(got) != wantBody {
1594                 t.Errorf("body = %q; want %q", got, wantBody)
1595         }
1596 }
1597
1598 // Issue 17494: cookies should be altered when Client follows redirects.
1599 func TestClientAltersCookiesOnRedirect(t *testing.T) {
1600         cookieMap := func(cs []*Cookie) map[string][]string {
1601                 m := make(map[string][]string)
1602                 for _, c := range cs {
1603                         m[c.Name] = append(m[c.Name], c.Value)
1604                 }
1605                 return m
1606         }
1607
1608         ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
1609                 var want map[string][]string
1610                 got := cookieMap(r.Cookies())
1611
1612                 c, _ := r.Cookie("Cycle")
1613                 switch c.Value {
1614                 case "0":
1615                         want = map[string][]string{
1616                                 "Cookie1": {"OldValue1a", "OldValue1b"},
1617                                 "Cookie2": {"OldValue2"},
1618                                 "Cookie3": {"OldValue3a", "OldValue3b"},
1619                                 "Cookie4": {"OldValue4"},
1620                                 "Cycle":   {"0"},
1621                         }
1622                         SetCookie(w, &Cookie{Name: "Cycle", Value: "1", Path: "/"})
1623                         SetCookie(w, &Cookie{Name: "Cookie2", Path: "/", MaxAge: -1}) // Delete cookie from Header
1624                         Redirect(w, r, "/", StatusFound)
1625                 case "1":
1626                         want = map[string][]string{
1627                                 "Cookie1": {"OldValue1a", "OldValue1b"},
1628                                 "Cookie3": {"OldValue3a", "OldValue3b"},
1629                                 "Cookie4": {"OldValue4"},
1630                                 "Cycle":   {"1"},
1631                         }
1632                         SetCookie(w, &Cookie{Name: "Cycle", Value: "2", Path: "/"})
1633                         SetCookie(w, &Cookie{Name: "Cookie3", Value: "NewValue3", Path: "/"}) // Modify cookie in Header
1634                         SetCookie(w, &Cookie{Name: "Cookie4", Value: "NewValue4", Path: "/"}) // Modify cookie in Jar
1635                         Redirect(w, r, "/", StatusFound)
1636                 case "2":
1637                         want = map[string][]string{
1638                                 "Cookie1": {"OldValue1a", "OldValue1b"},
1639                                 "Cookie3": {"NewValue3"},
1640                                 "Cookie4": {"NewValue4"},
1641                                 "Cycle":   {"2"},
1642                         }
1643                         SetCookie(w, &Cookie{Name: "Cycle", Value: "3", Path: "/"})
1644                         SetCookie(w, &Cookie{Name: "Cookie5", Value: "NewValue5", Path: "/"}) // Insert cookie into Jar
1645                         Redirect(w, r, "/", StatusFound)
1646                 case "3":
1647                         want = map[string][]string{
1648                                 "Cookie1": {"OldValue1a", "OldValue1b"},
1649                                 "Cookie3": {"NewValue3"},
1650                                 "Cookie4": {"NewValue4"},
1651                                 "Cookie5": {"NewValue5"},
1652                                 "Cycle":   {"3"},
1653                         }
1654                         // Don't redirect to ensure the loop ends.
1655                 default:
1656                         t.Errorf("unexpected redirect cycle")
1657                         return
1658                 }
1659
1660                 if !reflect.DeepEqual(got, want) {
1661                         t.Errorf("redirect %s, Cookie = %v, want %v", c.Value, got, want)
1662                 }
1663         }))
1664         defer ts.Close()
1665
1666         jar, _ := cookiejar.New(nil)
1667         c := ts.Client()
1668         c.Jar = jar
1669
1670         u, _ := url.Parse(ts.URL)
1671         req, _ := NewRequest("GET", ts.URL, nil)
1672         req.AddCookie(&Cookie{Name: "Cookie1", Value: "OldValue1a"})
1673         req.AddCookie(&Cookie{Name: "Cookie1", Value: "OldValue1b"})
1674         req.AddCookie(&Cookie{Name: "Cookie2", Value: "OldValue2"})
1675         req.AddCookie(&Cookie{Name: "Cookie3", Value: "OldValue3a"})
1676         req.AddCookie(&Cookie{Name: "Cookie3", Value: "OldValue3b"})
1677         jar.SetCookies(u, []*Cookie{{Name: "Cookie4", Value: "OldValue4", Path: "/"}})
1678         jar.SetCookies(u, []*Cookie{{Name: "Cycle", Value: "0", Path: "/"}})
1679         res, err := c.Do(req)
1680         if err != nil {
1681                 t.Fatal(err)
1682         }
1683         defer res.Body.Close()
1684         if res.StatusCode != 200 {
1685                 t.Fatal(res.Status)
1686         }
1687 }
1688
1689 // Part of Issue 4800
1690 func TestShouldCopyHeaderOnRedirect(t *testing.T) {
1691         tests := []struct {
1692                 header     string
1693                 initialURL string
1694                 destURL    string
1695                 want       bool
1696         }{
1697                 {"User-Agent", "http://foo.com/", "http://bar.com/", true},
1698                 {"X-Foo", "http://foo.com/", "http://bar.com/", true},
1699
1700                 // Sensitive headers:
1701                 {"cookie", "http://foo.com/", "http://bar.com/", false},
1702                 {"cookie2", "http://foo.com/", "http://bar.com/", false},
1703                 {"authorization", "http://foo.com/", "http://bar.com/", false},
1704                 {"www-authenticate", "http://foo.com/", "http://bar.com/", false},
1705
1706                 // But subdomains should work:
1707                 {"www-authenticate", "http://foo.com/", "http://foo.com/", true},
1708                 {"www-authenticate", "http://foo.com/", "http://sub.foo.com/", true},
1709                 {"www-authenticate", "http://foo.com/", "http://notfoo.com/", false},
1710                 {"www-authenticate", "http://foo.com/", "https://foo.com/", false},
1711                 {"www-authenticate", "http://foo.com:80/", "http://foo.com/", true},
1712                 {"www-authenticate", "http://foo.com:80/", "http://sub.foo.com/", true},
1713                 {"www-authenticate", "http://foo.com:443/", "https://foo.com/", true},
1714                 {"www-authenticate", "http://foo.com:443/", "https://sub.foo.com/", true},
1715                 {"www-authenticate", "http://foo.com:1234/", "http://foo.com/", false},
1716         }
1717         for i, tt := range tests {
1718                 u0, err := url.Parse(tt.initialURL)
1719                 if err != nil {
1720                         t.Errorf("%d. initial URL %q parse error: %v", i, tt.initialURL, err)
1721                         continue
1722                 }
1723                 u1, err := url.Parse(tt.destURL)
1724                 if err != nil {
1725                         t.Errorf("%d. dest URL %q parse error: %v", i, tt.destURL, err)
1726                         continue
1727                 }
1728                 got := Export_shouldCopyHeaderOnRedirect(tt.header, u0, u1)
1729                 if got != tt.want {
1730                         t.Errorf("%d. shouldCopyHeaderOnRedirect(%q, %q => %q) = %v; want %v",
1731                                 i, tt.header, tt.initialURL, tt.destURL, got, tt.want)
1732                 }
1733         }
1734 }
1735
1736 func TestClientRedirectTypes(t *testing.T) {
1737         setParallel(t)
1738         defer afterTest(t)
1739
1740         tests := [...]struct {
1741                 method       string
1742                 serverStatus int
1743                 wantMethod   string // desired subsequent client method
1744         }{
1745                 0: {method: "POST", serverStatus: 301, wantMethod: "GET"},
1746                 1: {method: "POST", serverStatus: 302, wantMethod: "GET"},
1747                 2: {method: "POST", serverStatus: 303, wantMethod: "GET"},
1748                 3: {method: "POST", serverStatus: 307, wantMethod: "POST"},
1749                 4: {method: "POST", serverStatus: 308, wantMethod: "POST"},
1750
1751                 5: {method: "HEAD", serverStatus: 301, wantMethod: "HEAD"},
1752                 6: {method: "HEAD", serverStatus: 302, wantMethod: "HEAD"},
1753                 7: {method: "HEAD", serverStatus: 303, wantMethod: "HEAD"},
1754                 8: {method: "HEAD", serverStatus: 307, wantMethod: "HEAD"},
1755                 9: {method: "HEAD", serverStatus: 308, wantMethod: "HEAD"},
1756
1757                 10: {method: "GET", serverStatus: 301, wantMethod: "GET"},
1758                 11: {method: "GET", serverStatus: 302, wantMethod: "GET"},
1759                 12: {method: "GET", serverStatus: 303, wantMethod: "GET"},
1760                 13: {method: "GET", serverStatus: 307, wantMethod: "GET"},
1761                 14: {method: "GET", serverStatus: 308, wantMethod: "GET"},
1762
1763                 15: {method: "DELETE", serverStatus: 301, wantMethod: "GET"},
1764                 16: {method: "DELETE", serverStatus: 302, wantMethod: "GET"},
1765                 17: {method: "DELETE", serverStatus: 303, wantMethod: "GET"},
1766                 18: {method: "DELETE", serverStatus: 307, wantMethod: "DELETE"},
1767                 19: {method: "DELETE", serverStatus: 308, wantMethod: "DELETE"},
1768
1769                 20: {method: "PUT", serverStatus: 301, wantMethod: "GET"},
1770                 21: {method: "PUT", serverStatus: 302, wantMethod: "GET"},
1771                 22: {method: "PUT", serverStatus: 303, wantMethod: "GET"},
1772                 23: {method: "PUT", serverStatus: 307, wantMethod: "PUT"},
1773                 24: {method: "PUT", serverStatus: 308, wantMethod: "PUT"},
1774
1775                 25: {method: "MADEUPMETHOD", serverStatus: 301, wantMethod: "GET"},
1776                 26: {method: "MADEUPMETHOD", serverStatus: 302, wantMethod: "GET"},
1777                 27: {method: "MADEUPMETHOD", serverStatus: 303, wantMethod: "GET"},
1778                 28: {method: "MADEUPMETHOD", serverStatus: 307, wantMethod: "MADEUPMETHOD"},
1779                 29: {method: "MADEUPMETHOD", serverStatus: 308, wantMethod: "MADEUPMETHOD"},
1780         }
1781
1782         handlerc := make(chan HandlerFunc, 1)
1783
1784         ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {
1785                 h := <-handlerc
1786                 h(rw, req)
1787         }))
1788         defer ts.Close()
1789
1790         c := ts.Client()
1791         for i, tt := range tests {
1792                 handlerc <- func(w ResponseWriter, r *Request) {
1793                         w.Header().Set("Location", ts.URL)
1794                         w.WriteHeader(tt.serverStatus)
1795                 }
1796
1797                 req, err := NewRequest(tt.method, ts.URL, nil)
1798                 if err != nil {
1799                         t.Errorf("#%d: NewRequest: %v", i, err)
1800                         continue
1801                 }
1802
1803                 c.CheckRedirect = func(req *Request, via []*Request) error {
1804                         if got, want := req.Method, tt.wantMethod; got != want {
1805                                 return fmt.Errorf("#%d: got next method %q; want %q", i, got, want)
1806                         }
1807                         handlerc <- func(rw ResponseWriter, req *Request) {
1808                                 // TODO: Check that the body is valid when we do 307 and 308 support
1809                         }
1810                         return nil
1811                 }
1812
1813                 res, err := c.Do(req)
1814                 if err != nil {
1815                         t.Errorf("#%d: Response: %v", i, err)
1816                         continue
1817                 }
1818
1819                 res.Body.Close()
1820         }
1821 }
1822
1823 // issue18239Body is an io.ReadCloser for TestTransportBodyReadError.
1824 // Its Read returns readErr and increments *readCalls atomically.
1825 // Its Close returns nil and increments *closeCalls atomically.
1826 type issue18239Body struct {
1827         readCalls  *int32
1828         closeCalls *int32
1829         readErr    error
1830 }
1831
1832 func (b issue18239Body) Read([]byte) (int, error) {
1833         atomic.AddInt32(b.readCalls, 1)
1834         return 0, b.readErr
1835 }
1836
1837 func (b issue18239Body) Close() error {
1838         atomic.AddInt32(b.closeCalls, 1)
1839         return nil
1840 }
1841
1842 // Issue 18239: make sure the Transport doesn't retry requests with bodies
1843 // if Request.GetBody is not defined.
1844 func TestTransportBodyReadError(t *testing.T) {
1845         setParallel(t)
1846         defer afterTest(t)
1847         ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
1848                 if r.URL.Path == "/ping" {
1849                         return
1850                 }
1851                 buf := make([]byte, 1)
1852                 n, err := r.Body.Read(buf)
1853                 w.Header().Set("X-Body-Read", fmt.Sprintf("%v, %v", n, err))
1854         }))
1855         defer ts.Close()
1856         c := ts.Client()
1857         tr := c.Transport.(*Transport)
1858
1859         // Do one initial successful request to create an idle TCP connection
1860         // for the subsequent request to reuse. (The Transport only retries
1861         // requests on reused connections.)
1862         res, err := c.Get(ts.URL + "/ping")
1863         if err != nil {
1864                 t.Fatal(err)
1865         }
1866         res.Body.Close()
1867
1868         var readCallsAtomic int32
1869         var closeCallsAtomic int32 // atomic
1870         someErr := errors.New("some body read error")
1871         body := issue18239Body{&readCallsAtomic, &closeCallsAtomic, someErr}
1872
1873         req, err := NewRequest("POST", ts.URL, body)
1874         if err != nil {
1875                 t.Fatal(err)
1876         }
1877         req = req.WithT(t)
1878         _, err = tr.RoundTrip(req)
1879         if err != someErr {
1880                 t.Errorf("Got error: %v; want Request.Body read error: %v", err, someErr)
1881         }
1882
1883         // And verify that our Body wasn't used multiple times, which
1884         // would indicate retries. (as it buggily was during part of
1885         // Go 1.8's dev cycle)
1886         readCalls := atomic.LoadInt32(&readCallsAtomic)
1887         closeCalls := atomic.LoadInt32(&closeCallsAtomic)
1888         if readCalls != 1 {
1889                 t.Errorf("read calls = %d; want 1", readCalls)
1890         }
1891         if closeCalls != 1 {
1892                 t.Errorf("close calls = %d; want 1", closeCalls)
1893         }
1894 }
1895
1896 type roundTripperWithoutCloseIdle struct{}
1897
1898 func (roundTripperWithoutCloseIdle) RoundTrip(*Request) (*Response, error) { panic("unused") }
1899
1900 type roundTripperWithCloseIdle func() // underlying func is CloseIdleConnections func
1901
1902 func (roundTripperWithCloseIdle) RoundTrip(*Request) (*Response, error) { panic("unused") }
1903 func (f roundTripperWithCloseIdle) CloseIdleConnections()               { f() }
1904
1905 func TestClientCloseIdleConnections(t *testing.T) {
1906         c := &Client{Transport: roundTripperWithoutCloseIdle{}}
1907         c.CloseIdleConnections() // verify we don't crash at least
1908
1909         closed := false
1910         var tr RoundTripper = roundTripperWithCloseIdle(func() {
1911                 closed = true
1912         })
1913         c = &Client{Transport: tr}
1914         c.CloseIdleConnections()
1915         if !closed {
1916                 t.Error("not closed")
1917         }
1918 }
1919
1920 func TestClientPropagatesTimeoutToContext(t *testing.T) {
1921         errDial := errors.New("not actually dialing")
1922         c := &Client{
1923                 Timeout: 5 * time.Second,
1924                 Transport: &Transport{
1925                         DialContext: func(ctx context.Context, netw, addr string) (net.Conn, error) {
1926                                 deadline, ok := ctx.Deadline()
1927                                 if !ok {
1928                                         t.Error("no deadline")
1929                                 } else {
1930                                         t.Logf("deadline in %v", deadline.Sub(time.Now()).Round(time.Second/10))
1931                                 }
1932                                 return nil, errDial
1933                         },
1934                 },
1935         }
1936         c.Get("https://example.tld/")
1937 }
1938
1939 func TestClientDoCanceledVsTimeout_h1(t *testing.T) {
1940         testClientDoCanceledVsTimeout(t, h1Mode)
1941 }
1942
1943 func TestClientDoCanceledVsTimeout_h2(t *testing.T) {
1944         testClientDoCanceledVsTimeout(t, h2Mode)
1945 }
1946
1947 // Issue 33545: lock-in the behavior promised by Client.Do's
1948 // docs about request cancellation vs timing out.
1949 func testClientDoCanceledVsTimeout(t *testing.T, h2 bool) {
1950         defer afterTest(t)
1951         cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
1952                 w.Write([]byte("Hello, World!"))
1953         }))
1954         defer cst.close()
1955
1956         cases := []string{"timeout", "canceled"}
1957
1958         for _, name := range cases {
1959                 t.Run(name, func(t *testing.T) {
1960                         var ctx context.Context
1961                         var cancel func()
1962                         if name == "timeout" {
1963                                 ctx, cancel = context.WithTimeout(context.Background(), -time.Nanosecond)
1964                         } else {
1965                                 ctx, cancel = context.WithCancel(context.Background())
1966                                 cancel()
1967                         }
1968                         defer cancel()
1969
1970                         req, _ := NewRequestWithContext(ctx, "GET", cst.ts.URL, nil)
1971                         _, err := cst.c.Do(req)
1972                         if err == nil {
1973                                 t.Fatal("Unexpectedly got a nil error")
1974                         }
1975
1976                         ue := err.(*url.Error)
1977
1978                         var wantIsTimeout bool
1979                         var wantErr error = context.Canceled
1980                         if name == "timeout" {
1981                                 wantErr = context.DeadlineExceeded
1982                                 wantIsTimeout = true
1983                         }
1984                         if g, w := ue.Timeout(), wantIsTimeout; g != w {
1985                                 t.Fatalf("url.Timeout() = %t, want %t", g, w)
1986                         }
1987                         if g, w := ue.Err, wantErr; g != w {
1988                                 t.Errorf("url.Error.Err = %v; want %v", g, w)
1989                         }
1990                 })
1991         }
1992 }
1993
1994 type nilBodyRoundTripper struct{}
1995
1996 func (nilBodyRoundTripper) RoundTrip(req *Request) (*Response, error) {
1997         return &Response{
1998                 StatusCode: StatusOK,
1999                 Status:     StatusText(StatusOK),
2000                 Body:       nil,
2001                 Request:    req,
2002         }, nil
2003 }
2004
2005 func TestClientPopulatesNilResponseBody(t *testing.T) {
2006         c := &Client{Transport: nilBodyRoundTripper{}}
2007
2008         resp, err := c.Get("http://localhost/anything")
2009         if err != nil {
2010                 t.Fatalf("Client.Get rejected Response with nil Body: %v", err)
2011         }
2012
2013         if resp.Body == nil {
2014                 t.Fatalf("Client failed to provide a non-nil Body as documented")
2015         }
2016         defer func() {
2017                 if err := resp.Body.Close(); err != nil {
2018                         t.Fatalf("error from Close on substitute Response.Body: %v", err)
2019                 }
2020         }()
2021
2022         if b, err := io.ReadAll(resp.Body); err != nil {
2023                 t.Errorf("read error from substitute Response.Body: %v", err)
2024         } else if len(b) != 0 {
2025                 t.Errorf("substitute Response.Body was unexpectedly non-empty: %q", b)
2026         }
2027 }
2028
2029 // Issue 40382: Client calls Close multiple times on Request.Body.
2030 func TestClientCallsCloseOnlyOnce(t *testing.T) {
2031         setParallel(t)
2032         defer afterTest(t)
2033         cst := newClientServerTest(t, h1Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
2034                 w.WriteHeader(StatusNoContent)
2035         }))
2036         defer cst.close()
2037
2038         // Issue occurred non-deterministically: needed to occur after a successful
2039         // write (into TCP buffer) but before end of body.
2040         for i := 0; i < 50 && !t.Failed(); i++ {
2041                 body := &issue40382Body{t: t, n: 300000}
2042                 req, err := NewRequest(MethodPost, cst.ts.URL, body)
2043                 if err != nil {
2044                         t.Fatal(err)
2045                 }
2046                 resp, err := cst.tr.RoundTrip(req)
2047                 if err != nil {
2048                         t.Fatal(err)
2049                 }
2050                 resp.Body.Close()
2051         }
2052 }
2053
2054 // issue40382Body is an io.ReadCloser for TestClientCallsCloseOnlyOnce.
2055 // Its Read reads n bytes before returning io.EOF.
2056 // Its Close returns nil but fails the test if called more than once.
2057 type issue40382Body struct {
2058         t                *testing.T
2059         n                int
2060         closeCallsAtomic int32
2061 }
2062
2063 func (b *issue40382Body) Read(p []byte) (int, error) {
2064         switch {
2065         case b.n == 0:
2066                 return 0, io.EOF
2067         case b.n < len(p):
2068                 p = p[:b.n]
2069                 fallthrough
2070         default:
2071                 for i := range p {
2072                         p[i] = 'x'
2073                 }
2074                 b.n -= len(p)
2075                 return len(p), nil
2076         }
2077 }
2078
2079 func (b *issue40382Body) Close() error {
2080         if atomic.AddInt32(&b.closeCallsAtomic, 1) == 2 {
2081                 b.t.Error("Body closed more than once")
2082         }
2083         return nil
2084 }
2085
2086 func TestProbeZeroLengthBody(t *testing.T) {
2087         setParallel(t)
2088         defer afterTest(t)
2089         reqc := make(chan struct{})
2090         cst := newClientServerTest(t, false, HandlerFunc(func(w ResponseWriter, r *Request) {
2091                 close(reqc)
2092                 if _, err := io.Copy(w, r.Body); err != nil {
2093                         t.Errorf("error copying request body: %v", err)
2094                 }
2095         }))
2096         defer cst.close()
2097
2098         bodyr, bodyw := io.Pipe()
2099         var gotBody string
2100         var wg sync.WaitGroup
2101         wg.Add(1)
2102         go func() {
2103                 defer wg.Done()
2104                 req, _ := NewRequest("GET", cst.ts.URL, bodyr)
2105                 res, err := cst.c.Do(req)
2106                 b, err := io.ReadAll(res.Body)
2107                 if err != nil {
2108                         t.Error(err)
2109                 }
2110                 gotBody = string(b)
2111         }()
2112
2113         select {
2114         case <-reqc:
2115                 // Request should be sent after trying to probe the request body for 200ms.
2116         case <-time.After(60 * time.Second):
2117                 t.Errorf("request not sent after 60s")
2118         }
2119
2120         // Write the request body and wait for the request to complete.
2121         const content = "body"
2122         bodyw.Write([]byte(content))
2123         bodyw.Close()
2124         wg.Wait()
2125         if gotBody != content {
2126                 t.Fatalf("server got body %q, want %q", gotBody, content)
2127         }
2128 }