...

Source file src/net/http/client_test.go

Documentation: net/http

		 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%[email protected]/"
	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%[email protected]/"
	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:[email protected]/",
	1174  			out:	`Get "http://user:***@dummy.faketld/": dummy impl`,
	1175  		},
	1176  		{
	1177  			desc: "Don't Strip password from domain name",
	1178  			in:	 "http://user:[email protected]/",
	1179  			out:	`Get "http://user:***@password.faketld/": dummy impl`,
	1180  		},
	1181  		{
	1182  			desc: "Don't Strip password from path",
	1183  			in:	 "http://user:[email protected]/password",
	1184  			out:	`Get "http://user:***@dummy.faketld/password": dummy impl`,
	1185  		},
	1186  		{
	1187  			desc: "Strip escaped password",
	1188  			in:	 "http://user:pa%[email protected]/",
	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 TestClientTimeoutDoesNotExpire_h1(t *testing.T) { testClientTimeoutDoesNotExpire(t, h1Mode) }
	1357  func TestClientTimeoutDoesNotExpire_h2(t *testing.T) { testClientTimeoutDoesNotExpire(t, h2Mode) }
	1358  
	1359  // Issue 49366: if Client.Timeout is set but not hit, no error should be returned.
	1360  func testClientTimeoutDoesNotExpire(t *testing.T, h2 bool) {
	1361  	setParallel(t)
	1362  	defer afterTest(t)
	1363  
	1364  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
	1365  		w.Write([]byte("body"))
	1366  	}))
	1367  	defer cst.close()
	1368  
	1369  	cst.c.Timeout = 1 * time.Hour
	1370  	req, _ := NewRequest("GET", cst.ts.URL, nil)
	1371  	res, err := cst.c.Do(req)
	1372  	if err != nil {
	1373  		t.Fatal(err)
	1374  	}
	1375  	if _, err = io.Copy(io.Discard, res.Body); err != nil {
	1376  		t.Fatalf("io.Copy(io.Discard, res.Body) = %v, want nil", err)
	1377  	}
	1378  	if err = res.Body.Close(); err != nil {
	1379  		t.Fatalf("res.Body.Close() = %v, want nil", err)
	1380  	}
	1381  }
	1382  
	1383  func TestClientRedirectEatsBody_h1(t *testing.T) { testClientRedirectEatsBody(t, h1Mode) }
	1384  func TestClientRedirectEatsBody_h2(t *testing.T) { testClientRedirectEatsBody(t, h2Mode) }
	1385  func testClientRedirectEatsBody(t *testing.T, h2 bool) {
	1386  	setParallel(t)
	1387  	defer afterTest(t)
	1388  	saw := make(chan string, 2)
	1389  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
	1390  		saw <- r.RemoteAddr
	1391  		if r.URL.Path == "/" {
	1392  			Redirect(w, r, "/foo", StatusFound) // which includes a body
	1393  		}
	1394  	}))
	1395  	defer cst.close()
	1396  
	1397  	res, err := cst.c.Get(cst.ts.URL)
	1398  	if err != nil {
	1399  		t.Fatal(err)
	1400  	}
	1401  	_, err = io.ReadAll(res.Body)
	1402  	res.Body.Close()
	1403  	if err != nil {
	1404  		t.Fatal(err)
	1405  	}
	1406  
	1407  	var first string
	1408  	select {
	1409  	case first = <-saw:
	1410  	default:
	1411  		t.Fatal("server didn't see a request")
	1412  	}
	1413  
	1414  	var second string
	1415  	select {
	1416  	case second = <-saw:
	1417  	default:
	1418  		t.Fatal("server didn't see a second request")
	1419  	}
	1420  
	1421  	if first != second {
	1422  		t.Fatal("server saw different client ports before & after the redirect")
	1423  	}
	1424  }
	1425  
	1426  // eofReaderFunc is an io.Reader that runs itself, and then returns io.EOF.
	1427  type eofReaderFunc func()
	1428  
	1429  func (f eofReaderFunc) Read(p []byte) (n int, err error) {
	1430  	f()
	1431  	return 0, io.EOF
	1432  }
	1433  
	1434  func TestReferer(t *testing.T) {
	1435  	tests := []struct {
	1436  		lastReq, newReq string // from -> to URLs
	1437  		want						string
	1438  	}{
	1439  		// don't send user:
	1440  		{"http://[email protected]", "http://link.com", "http://test.com"},
	1441  		{"https://[email protected]", "https://link.com", "https://test.com"},
	1442  
	1443  		// don't send a user and password:
	1444  		{"http://gopher:[email protected]", "http://link.com", "http://test.com"},
	1445  		{"https://gopher:[email protected]", "https://link.com", "https://test.com"},
	1446  
	1447  		// nothing to do:
	1448  		{"http://test.com", "http://link.com", "http://test.com"},
	1449  		{"https://test.com", "https://link.com", "https://test.com"},
	1450  
	1451  		// https to http doesn't send a referer:
	1452  		{"https://test.com", "http://link.com", ""},
	1453  		{"https://gopher:[email protected]", "http://link.com", ""},
	1454  	}
	1455  	for _, tt := range tests {
	1456  		l, err := url.Parse(tt.lastReq)
	1457  		if err != nil {
	1458  			t.Fatal(err)
	1459  		}
	1460  		n, err := url.Parse(tt.newReq)
	1461  		if err != nil {
	1462  			t.Fatal(err)
	1463  		}
	1464  		r := ExportRefererForURL(l, n)
	1465  		if r != tt.want {
	1466  			t.Errorf("refererForURL(%q, %q) = %q; want %q", tt.lastReq, tt.newReq, r, tt.want)
	1467  		}
	1468  	}
	1469  }
	1470  
	1471  // issue15577Tripper returns a Response with a redirect response
	1472  // header and doesn't populate its Response.Request field.
	1473  type issue15577Tripper struct{}
	1474  
	1475  func (issue15577Tripper) RoundTrip(*Request) (*Response, error) {
	1476  	resp := &Response{
	1477  		StatusCode: 303,
	1478  		Header:		 map[string][]string{"Location": {"http://www.example.com/"}},
	1479  		Body:			 io.NopCloser(strings.NewReader("")),
	1480  	}
	1481  	return resp, nil
	1482  }
	1483  
	1484  // Issue 15577: don't assume the roundtripper's response populates its Request field.
	1485  func TestClientRedirectResponseWithoutRequest(t *testing.T) {
	1486  	c := &Client{
	1487  		CheckRedirect: func(*Request, []*Request) error { return fmt.Errorf("no redirects!") },
	1488  		Transport:		 issue15577Tripper{},
	1489  	}
	1490  	// Check that this doesn't crash:
	1491  	c.Get("http://dummy.tld")
	1492  }
	1493  
	1494  // Issue 4800: copy (some) headers when Client follows a redirect.
	1495  func TestClientCopyHeadersOnRedirect(t *testing.T) {
	1496  	const (
	1497  		ua	 = "some-agent/1.2"
	1498  		xfoo = "foo-val"
	1499  	)
	1500  	var ts2URL string
	1501  	ts1 := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
	1502  		want := Header{
	1503  			"User-Agent":			[]string{ua},
	1504  			"X-Foo":					 []string{xfoo},
	1505  			"Referer":				 []string{ts2URL},
	1506  			"Accept-Encoding": []string{"gzip"},
	1507  		}
	1508  		if !reflect.DeepEqual(r.Header, want) {
	1509  			t.Errorf("Request.Header = %#v; want %#v", r.Header, want)
	1510  		}
	1511  		if t.Failed() {
	1512  			w.Header().Set("Result", "got errors")
	1513  		} else {
	1514  			w.Header().Set("Result", "ok")
	1515  		}
	1516  	}))
	1517  	defer ts1.Close()
	1518  	ts2 := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
	1519  		Redirect(w, r, ts1.URL, StatusFound)
	1520  	}))
	1521  	defer ts2.Close()
	1522  	ts2URL = ts2.URL
	1523  
	1524  	c := ts1.Client()
	1525  	c.CheckRedirect = func(r *Request, via []*Request) error {
	1526  		want := Header{
	1527  			"User-Agent": []string{ua},
	1528  			"X-Foo":			[]string{xfoo},
	1529  			"Referer":		[]string{ts2URL},
	1530  		}
	1531  		if !reflect.DeepEqual(r.Header, want) {
	1532  			t.Errorf("CheckRedirect Request.Header = %#v; want %#v", r.Header, want)
	1533  		}
	1534  		return nil
	1535  	}
	1536  
	1537  	req, _ := NewRequest("GET", ts2.URL, nil)
	1538  	req.Header.Add("User-Agent", ua)
	1539  	req.Header.Add("X-Foo", xfoo)
	1540  	req.Header.Add("Cookie", "foo=bar")
	1541  	req.Header.Add("Authorization", "secretpassword")
	1542  	res, err := c.Do(req)
	1543  	if err != nil {
	1544  		t.Fatal(err)
	1545  	}
	1546  	defer res.Body.Close()
	1547  	if res.StatusCode != 200 {
	1548  		t.Fatal(res.Status)
	1549  	}
	1550  	if got := res.Header.Get("Result"); got != "ok" {
	1551  		t.Errorf("result = %q; want ok", got)
	1552  	}
	1553  }
	1554  
	1555  // Issue 22233: copy host when Client follows a relative redirect.
	1556  func TestClientCopyHostOnRedirect(t *testing.T) {
	1557  	// Virtual hostname: should not receive any request.
	1558  	virtual := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
	1559  		t.Errorf("Virtual host received request %v", r.URL)
	1560  		w.WriteHeader(403)
	1561  		io.WriteString(w, "should not see this response")
	1562  	}))
	1563  	defer virtual.Close()
	1564  	virtualHost := strings.TrimPrefix(virtual.URL, "http://")
	1565  	t.Logf("Virtual host is %v", virtualHost)
	1566  
	1567  	// Actual hostname: should not receive any request.
	1568  	const wantBody = "response body"
	1569  	var tsURL string
	1570  	var tsHost string
	1571  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
	1572  		switch r.URL.Path {
	1573  		case "/":
	1574  			// Relative redirect.
	1575  			if r.Host != virtualHost {
	1576  				t.Errorf("Serving /: Request.Host = %#v; want %#v", r.Host, virtualHost)
	1577  				w.WriteHeader(404)
	1578  				return
	1579  			}
	1580  			w.Header().Set("Location", "/hop")
	1581  			w.WriteHeader(302)
	1582  		case "/hop":
	1583  			// Absolute redirect.
	1584  			if r.Host != virtualHost {
	1585  				t.Errorf("Serving /hop: Request.Host = %#v; want %#v", r.Host, virtualHost)
	1586  				w.WriteHeader(404)
	1587  				return
	1588  			}
	1589  			w.Header().Set("Location", tsURL+"/final")
	1590  			w.WriteHeader(302)
	1591  		case "/final":
	1592  			if r.Host != tsHost {
	1593  				t.Errorf("Serving /final: Request.Host = %#v; want %#v", r.Host, tsHost)
	1594  				w.WriteHeader(404)
	1595  				return
	1596  			}
	1597  			w.WriteHeader(200)
	1598  			io.WriteString(w, wantBody)
	1599  		default:
	1600  			t.Errorf("Serving unexpected path %q", r.URL.Path)
	1601  			w.WriteHeader(404)
	1602  		}
	1603  	}))
	1604  	defer ts.Close()
	1605  	tsURL = ts.URL
	1606  	tsHost = strings.TrimPrefix(ts.URL, "http://")
	1607  	t.Logf("Server host is %v", tsHost)
	1608  
	1609  	c := ts.Client()
	1610  	req, _ := NewRequest("GET", ts.URL, nil)
	1611  	req.Host = virtualHost
	1612  	resp, err := c.Do(req)
	1613  	if err != nil {
	1614  		t.Fatal(err)
	1615  	}
	1616  	defer resp.Body.Close()
	1617  	if resp.StatusCode != 200 {
	1618  		t.Fatal(resp.Status)
	1619  	}
	1620  	if got, err := io.ReadAll(resp.Body); err != nil || string(got) != wantBody {
	1621  		t.Errorf("body = %q; want %q", got, wantBody)
	1622  	}
	1623  }
	1624  
	1625  // Issue 17494: cookies should be altered when Client follows redirects.
	1626  func TestClientAltersCookiesOnRedirect(t *testing.T) {
	1627  	cookieMap := func(cs []*Cookie) map[string][]string {
	1628  		m := make(map[string][]string)
	1629  		for _, c := range cs {
	1630  			m[c.Name] = append(m[c.Name], c.Value)
	1631  		}
	1632  		return m
	1633  	}
	1634  
	1635  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
	1636  		var want map[string][]string
	1637  		got := cookieMap(r.Cookies())
	1638  
	1639  		c, _ := r.Cookie("Cycle")
	1640  		switch c.Value {
	1641  		case "0":
	1642  			want = map[string][]string{
	1643  				"Cookie1": {"OldValue1a", "OldValue1b"},
	1644  				"Cookie2": {"OldValue2"},
	1645  				"Cookie3": {"OldValue3a", "OldValue3b"},
	1646  				"Cookie4": {"OldValue4"},
	1647  				"Cycle":	 {"0"},
	1648  			}
	1649  			SetCookie(w, &Cookie{Name: "Cycle", Value: "1", Path: "/"})
	1650  			SetCookie(w, &Cookie{Name: "Cookie2", Path: "/", MaxAge: -1}) // Delete cookie from Header
	1651  			Redirect(w, r, "/", StatusFound)
	1652  		case "1":
	1653  			want = map[string][]string{
	1654  				"Cookie1": {"OldValue1a", "OldValue1b"},
	1655  				"Cookie3": {"OldValue3a", "OldValue3b"},
	1656  				"Cookie4": {"OldValue4"},
	1657  				"Cycle":	 {"1"},
	1658  			}
	1659  			SetCookie(w, &Cookie{Name: "Cycle", Value: "2", Path: "/"})
	1660  			SetCookie(w, &Cookie{Name: "Cookie3", Value: "NewValue3", Path: "/"}) // Modify cookie in Header
	1661  			SetCookie(w, &Cookie{Name: "Cookie4", Value: "NewValue4", Path: "/"}) // Modify cookie in Jar
	1662  			Redirect(w, r, "/", StatusFound)
	1663  		case "2":
	1664  			want = map[string][]string{
	1665  				"Cookie1": {"OldValue1a", "OldValue1b"},
	1666  				"Cookie3": {"NewValue3"},
	1667  				"Cookie4": {"NewValue4"},
	1668  				"Cycle":	 {"2"},
	1669  			}
	1670  			SetCookie(w, &Cookie{Name: "Cycle", Value: "3", Path: "/"})
	1671  			SetCookie(w, &Cookie{Name: "Cookie5", Value: "NewValue5", Path: "/"}) // Insert cookie into Jar
	1672  			Redirect(w, r, "/", StatusFound)
	1673  		case "3":
	1674  			want = map[string][]string{
	1675  				"Cookie1": {"OldValue1a", "OldValue1b"},
	1676  				"Cookie3": {"NewValue3"},
	1677  				"Cookie4": {"NewValue4"},
	1678  				"Cookie5": {"NewValue5"},
	1679  				"Cycle":	 {"3"},
	1680  			}
	1681  			// Don't redirect to ensure the loop ends.
	1682  		default:
	1683  			t.Errorf("unexpected redirect cycle")
	1684  			return
	1685  		}
	1686  
	1687  		if !reflect.DeepEqual(got, want) {
	1688  			t.Errorf("redirect %s, Cookie = %v, want %v", c.Value, got, want)
	1689  		}
	1690  	}))
	1691  	defer ts.Close()
	1692  
	1693  	jar, _ := cookiejar.New(nil)
	1694  	c := ts.Client()
	1695  	c.Jar = jar
	1696  
	1697  	u, _ := url.Parse(ts.URL)
	1698  	req, _ := NewRequest("GET", ts.URL, nil)
	1699  	req.AddCookie(&Cookie{Name: "Cookie1", Value: "OldValue1a"})
	1700  	req.AddCookie(&Cookie{Name: "Cookie1", Value: "OldValue1b"})
	1701  	req.AddCookie(&Cookie{Name: "Cookie2", Value: "OldValue2"})
	1702  	req.AddCookie(&Cookie{Name: "Cookie3", Value: "OldValue3a"})
	1703  	req.AddCookie(&Cookie{Name: "Cookie3", Value: "OldValue3b"})
	1704  	jar.SetCookies(u, []*Cookie{{Name: "Cookie4", Value: "OldValue4", Path: "/"}})
	1705  	jar.SetCookies(u, []*Cookie{{Name: "Cycle", Value: "0", Path: "/"}})
	1706  	res, err := c.Do(req)
	1707  	if err != nil {
	1708  		t.Fatal(err)
	1709  	}
	1710  	defer res.Body.Close()
	1711  	if res.StatusCode != 200 {
	1712  		t.Fatal(res.Status)
	1713  	}
	1714  }
	1715  
	1716  // Part of Issue 4800
	1717  func TestShouldCopyHeaderOnRedirect(t *testing.T) {
	1718  	tests := []struct {
	1719  		header		 string
	1720  		initialURL string
	1721  		destURL		string
	1722  		want			 bool
	1723  	}{
	1724  		{"User-Agent", "http://foo.com/", "http://bar.com/", true},
	1725  		{"X-Foo", "http://foo.com/", "http://bar.com/", true},
	1726  
	1727  		// Sensitive headers:
	1728  		{"cookie", "http://foo.com/", "http://bar.com/", false},
	1729  		{"cookie2", "http://foo.com/", "http://bar.com/", false},
	1730  		{"authorization", "http://foo.com/", "http://bar.com/", false},
	1731  		{"www-authenticate", "http://foo.com/", "http://bar.com/", false},
	1732  
	1733  		// But subdomains should work:
	1734  		{"www-authenticate", "http://foo.com/", "http://foo.com/", true},
	1735  		{"www-authenticate", "http://foo.com/", "http://sub.foo.com/", true},
	1736  		{"www-authenticate", "http://foo.com/", "http://notfoo.com/", false},
	1737  		{"www-authenticate", "http://foo.com/", "https://foo.com/", false},
	1738  		{"www-authenticate", "http://foo.com:80/", "http://foo.com/", true},
	1739  		{"www-authenticate", "http://foo.com:80/", "http://sub.foo.com/", true},
	1740  		{"www-authenticate", "http://foo.com:443/", "https://foo.com/", true},
	1741  		{"www-authenticate", "http://foo.com:443/", "https://sub.foo.com/", true},
	1742  		{"www-authenticate", "http://foo.com:1234/", "http://foo.com/", false},
	1743  	}
	1744  	for i, tt := range tests {
	1745  		u0, err := url.Parse(tt.initialURL)
	1746  		if err != nil {
	1747  			t.Errorf("%d. initial URL %q parse error: %v", i, tt.initialURL, err)
	1748  			continue
	1749  		}
	1750  		u1, err := url.Parse(tt.destURL)
	1751  		if err != nil {
	1752  			t.Errorf("%d. dest URL %q parse error: %v", i, tt.destURL, err)
	1753  			continue
	1754  		}
	1755  		got := Export_shouldCopyHeaderOnRedirect(tt.header, u0, u1)
	1756  		if got != tt.want {
	1757  			t.Errorf("%d. shouldCopyHeaderOnRedirect(%q, %q => %q) = %v; want %v",
	1758  				i, tt.header, tt.initialURL, tt.destURL, got, tt.want)
	1759  		}
	1760  	}
	1761  }
	1762  
	1763  func TestClientRedirectTypes(t *testing.T) {
	1764  	setParallel(t)
	1765  	defer afterTest(t)
	1766  
	1767  	tests := [...]struct {
	1768  		method			 string
	1769  		serverStatus int
	1770  		wantMethod	 string // desired subsequent client method
	1771  	}{
	1772  		0: {method: "POST", serverStatus: 301, wantMethod: "GET"},
	1773  		1: {method: "POST", serverStatus: 302, wantMethod: "GET"},
	1774  		2: {method: "POST", serverStatus: 303, wantMethod: "GET"},
	1775  		3: {method: "POST", serverStatus: 307, wantMethod: "POST"},
	1776  		4: {method: "POST", serverStatus: 308, wantMethod: "POST"},
	1777  
	1778  		5: {method: "HEAD", serverStatus: 301, wantMethod: "HEAD"},
	1779  		6: {method: "HEAD", serverStatus: 302, wantMethod: "HEAD"},
	1780  		7: {method: "HEAD", serverStatus: 303, wantMethod: "HEAD"},
	1781  		8: {method: "HEAD", serverStatus: 307, wantMethod: "HEAD"},
	1782  		9: {method: "HEAD", serverStatus: 308, wantMethod: "HEAD"},
	1783  
	1784  		10: {method: "GET", serverStatus: 301, wantMethod: "GET"},
	1785  		11: {method: "GET", serverStatus: 302, wantMethod: "GET"},
	1786  		12: {method: "GET", serverStatus: 303, wantMethod: "GET"},
	1787  		13: {method: "GET", serverStatus: 307, wantMethod: "GET"},
	1788  		14: {method: "GET", serverStatus: 308, wantMethod: "GET"},
	1789  
	1790  		15: {method: "DELETE", serverStatus: 301, wantMethod: "GET"},
	1791  		16: {method: "DELETE", serverStatus: 302, wantMethod: "GET"},
	1792  		17: {method: "DELETE", serverStatus: 303, wantMethod: "GET"},
	1793  		18: {method: "DELETE", serverStatus: 307, wantMethod: "DELETE"},
	1794  		19: {method: "DELETE", serverStatus: 308, wantMethod: "DELETE"},
	1795  
	1796  		20: {method: "PUT", serverStatus: 301, wantMethod: "GET"},
	1797  		21: {method: "PUT", serverStatus: 302, wantMethod: "GET"},
	1798  		22: {method: "PUT", serverStatus: 303, wantMethod: "GET"},
	1799  		23: {method: "PUT", serverStatus: 307, wantMethod: "PUT"},
	1800  		24: {method: "PUT", serverStatus: 308, wantMethod: "PUT"},
	1801  
	1802  		25: {method: "MADEUPMETHOD", serverStatus: 301, wantMethod: "GET"},
	1803  		26: {method: "MADEUPMETHOD", serverStatus: 302, wantMethod: "GET"},
	1804  		27: {method: "MADEUPMETHOD", serverStatus: 303, wantMethod: "GET"},
	1805  		28: {method: "MADEUPMETHOD", serverStatus: 307, wantMethod: "MADEUPMETHOD"},
	1806  		29: {method: "MADEUPMETHOD", serverStatus: 308, wantMethod: "MADEUPMETHOD"},
	1807  	}
	1808  
	1809  	handlerc := make(chan HandlerFunc, 1)
	1810  
	1811  	ts := httptest.NewServer(HandlerFunc(func(rw ResponseWriter, req *Request) {
	1812  		h := <-handlerc
	1813  		h(rw, req)
	1814  	}))
	1815  	defer ts.Close()
	1816  
	1817  	c := ts.Client()
	1818  	for i, tt := range tests {
	1819  		handlerc <- func(w ResponseWriter, r *Request) {
	1820  			w.Header().Set("Location", ts.URL)
	1821  			w.WriteHeader(tt.serverStatus)
	1822  		}
	1823  
	1824  		req, err := NewRequest(tt.method, ts.URL, nil)
	1825  		if err != nil {
	1826  			t.Errorf("#%d: NewRequest: %v", i, err)
	1827  			continue
	1828  		}
	1829  
	1830  		c.CheckRedirect = func(req *Request, via []*Request) error {
	1831  			if got, want := req.Method, tt.wantMethod; got != want {
	1832  				return fmt.Errorf("#%d: got next method %q; want %q", i, got, want)
	1833  			}
	1834  			handlerc <- func(rw ResponseWriter, req *Request) {
	1835  				// TODO: Check that the body is valid when we do 307 and 308 support
	1836  			}
	1837  			return nil
	1838  		}
	1839  
	1840  		res, err := c.Do(req)
	1841  		if err != nil {
	1842  			t.Errorf("#%d: Response: %v", i, err)
	1843  			continue
	1844  		}
	1845  
	1846  		res.Body.Close()
	1847  	}
	1848  }
	1849  
	1850  // issue18239Body is an io.ReadCloser for TestTransportBodyReadError.
	1851  // Its Read returns readErr and increments *readCalls atomically.
	1852  // Its Close returns nil and increments *closeCalls atomically.
	1853  type issue18239Body struct {
	1854  	readCalls	*int32
	1855  	closeCalls *int32
	1856  	readErr		error
	1857  }
	1858  
	1859  func (b issue18239Body) Read([]byte) (int, error) {
	1860  	atomic.AddInt32(b.readCalls, 1)
	1861  	return 0, b.readErr
	1862  }
	1863  
	1864  func (b issue18239Body) Close() error {
	1865  	atomic.AddInt32(b.closeCalls, 1)
	1866  	return nil
	1867  }
	1868  
	1869  // Issue 18239: make sure the Transport doesn't retry requests with bodies
	1870  // if Request.GetBody is not defined.
	1871  func TestTransportBodyReadError(t *testing.T) {
	1872  	setParallel(t)
	1873  	defer afterTest(t)
	1874  	ts := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {
	1875  		if r.URL.Path == "/ping" {
	1876  			return
	1877  		}
	1878  		buf := make([]byte, 1)
	1879  		n, err := r.Body.Read(buf)
	1880  		w.Header().Set("X-Body-Read", fmt.Sprintf("%v, %v", n, err))
	1881  	}))
	1882  	defer ts.Close()
	1883  	c := ts.Client()
	1884  	tr := c.Transport.(*Transport)
	1885  
	1886  	// Do one initial successful request to create an idle TCP connection
	1887  	// for the subsequent request to reuse. (The Transport only retries
	1888  	// requests on reused connections.)
	1889  	res, err := c.Get(ts.URL + "/ping")
	1890  	if err != nil {
	1891  		t.Fatal(err)
	1892  	}
	1893  	res.Body.Close()
	1894  
	1895  	var readCallsAtomic int32
	1896  	var closeCallsAtomic int32 // atomic
	1897  	someErr := errors.New("some body read error")
	1898  	body := issue18239Body{&readCallsAtomic, &closeCallsAtomic, someErr}
	1899  
	1900  	req, err := NewRequest("POST", ts.URL, body)
	1901  	if err != nil {
	1902  		t.Fatal(err)
	1903  	}
	1904  	req = req.WithT(t)
	1905  	_, err = tr.RoundTrip(req)
	1906  	if err != someErr {
	1907  		t.Errorf("Got error: %v; want Request.Body read error: %v", err, someErr)
	1908  	}
	1909  
	1910  	// And verify that our Body wasn't used multiple times, which
	1911  	// would indicate retries. (as it buggily was during part of
	1912  	// Go 1.8's dev cycle)
	1913  	readCalls := atomic.LoadInt32(&readCallsAtomic)
	1914  	closeCalls := atomic.LoadInt32(&closeCallsAtomic)
	1915  	if readCalls != 1 {
	1916  		t.Errorf("read calls = %d; want 1", readCalls)
	1917  	}
	1918  	if closeCalls != 1 {
	1919  		t.Errorf("close calls = %d; want 1", closeCalls)
	1920  	}
	1921  }
	1922  
	1923  type roundTripperWithoutCloseIdle struct{}
	1924  
	1925  func (roundTripperWithoutCloseIdle) RoundTrip(*Request) (*Response, error) { panic("unused") }
	1926  
	1927  type roundTripperWithCloseIdle func() // underlying func is CloseIdleConnections func
	1928  
	1929  func (roundTripperWithCloseIdle) RoundTrip(*Request) (*Response, error) { panic("unused") }
	1930  func (f roundTripperWithCloseIdle) CloseIdleConnections()							 { f() }
	1931  
	1932  func TestClientCloseIdleConnections(t *testing.T) {
	1933  	c := &Client{Transport: roundTripperWithoutCloseIdle{}}
	1934  	c.CloseIdleConnections() // verify we don't crash at least
	1935  
	1936  	closed := false
	1937  	var tr RoundTripper = roundTripperWithCloseIdle(func() {
	1938  		closed = true
	1939  	})
	1940  	c = &Client{Transport: tr}
	1941  	c.CloseIdleConnections()
	1942  	if !closed {
	1943  		t.Error("not closed")
	1944  	}
	1945  }
	1946  
	1947  func TestClientPropagatesTimeoutToContext(t *testing.T) {
	1948  	errDial := errors.New("not actually dialing")
	1949  	c := &Client{
	1950  		Timeout: 5 * time.Second,
	1951  		Transport: &Transport{
	1952  			DialContext: func(ctx context.Context, netw, addr string) (net.Conn, error) {
	1953  				deadline, ok := ctx.Deadline()
	1954  				if !ok {
	1955  					t.Error("no deadline")
	1956  				} else {
	1957  					t.Logf("deadline in %v", deadline.Sub(time.Now()).Round(time.Second/10))
	1958  				}
	1959  				return nil, errDial
	1960  			},
	1961  		},
	1962  	}
	1963  	c.Get("https://example.tld/")
	1964  }
	1965  
	1966  func TestClientDoCanceledVsTimeout_h1(t *testing.T) {
	1967  	testClientDoCanceledVsTimeout(t, h1Mode)
	1968  }
	1969  
	1970  func TestClientDoCanceledVsTimeout_h2(t *testing.T) {
	1971  	testClientDoCanceledVsTimeout(t, h2Mode)
	1972  }
	1973  
	1974  // Issue 33545: lock-in the behavior promised by Client.Do's
	1975  // docs about request cancellation vs timing out.
	1976  func testClientDoCanceledVsTimeout(t *testing.T, h2 bool) {
	1977  	defer afterTest(t)
	1978  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
	1979  		w.Write([]byte("Hello, World!"))
	1980  	}))
	1981  	defer cst.close()
	1982  
	1983  	cases := []string{"timeout", "canceled"}
	1984  
	1985  	for _, name := range cases {
	1986  		t.Run(name, func(t *testing.T) {
	1987  			var ctx context.Context
	1988  			var cancel func()
	1989  			if name == "timeout" {
	1990  				ctx, cancel = context.WithTimeout(context.Background(), -time.Nanosecond)
	1991  			} else {
	1992  				ctx, cancel = context.WithCancel(context.Background())
	1993  				cancel()
	1994  			}
	1995  			defer cancel()
	1996  
	1997  			req, _ := NewRequestWithContext(ctx, "GET", cst.ts.URL, nil)
	1998  			_, err := cst.c.Do(req)
	1999  			if err == nil {
	2000  				t.Fatal("Unexpectedly got a nil error")
	2001  			}
	2002  
	2003  			ue := err.(*url.Error)
	2004  
	2005  			var wantIsTimeout bool
	2006  			var wantErr error = context.Canceled
	2007  			if name == "timeout" {
	2008  				wantErr = context.DeadlineExceeded
	2009  				wantIsTimeout = true
	2010  			}
	2011  			if g, w := ue.Timeout(), wantIsTimeout; g != w {
	2012  				t.Fatalf("url.Timeout() = %t, want %t", g, w)
	2013  			}
	2014  			if g, w := ue.Err, wantErr; g != w {
	2015  				t.Errorf("url.Error.Err = %v; want %v", g, w)
	2016  			}
	2017  		})
	2018  	}
	2019  }
	2020  
	2021  type nilBodyRoundTripper struct{}
	2022  
	2023  func (nilBodyRoundTripper) RoundTrip(req *Request) (*Response, error) {
	2024  	return &Response{
	2025  		StatusCode: StatusOK,
	2026  		Status:		 StatusText(StatusOK),
	2027  		Body:			 nil,
	2028  		Request:		req,
	2029  	}, nil
	2030  }
	2031  
	2032  func TestClientPopulatesNilResponseBody(t *testing.T) {
	2033  	c := &Client{Transport: nilBodyRoundTripper{}}
	2034  
	2035  	resp, err := c.Get("http://localhost/anything")
	2036  	if err != nil {
	2037  		t.Fatalf("Client.Get rejected Response with nil Body: %v", err)
	2038  	}
	2039  
	2040  	if resp.Body == nil {
	2041  		t.Fatalf("Client failed to provide a non-nil Body as documented")
	2042  	}
	2043  	defer func() {
	2044  		if err := resp.Body.Close(); err != nil {
	2045  			t.Fatalf("error from Close on substitute Response.Body: %v", err)
	2046  		}
	2047  	}()
	2048  
	2049  	if b, err := io.ReadAll(resp.Body); err != nil {
	2050  		t.Errorf("read error from substitute Response.Body: %v", err)
	2051  	} else if len(b) != 0 {
	2052  		t.Errorf("substitute Response.Body was unexpectedly non-empty: %q", b)
	2053  	}
	2054  }
	2055  
	2056  // Issue 40382: Client calls Close multiple times on Request.Body.
	2057  func TestClientCallsCloseOnlyOnce(t *testing.T) {
	2058  	setParallel(t)
	2059  	defer afterTest(t)
	2060  	cst := newClientServerTest(t, h1Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
	2061  		w.WriteHeader(StatusNoContent)
	2062  	}))
	2063  	defer cst.close()
	2064  
	2065  	// Issue occurred non-deterministically: needed to occur after a successful
	2066  	// write (into TCP buffer) but before end of body.
	2067  	for i := 0; i < 50 && !t.Failed(); i++ {
	2068  		body := &issue40382Body{t: t, n: 300000}
	2069  		req, err := NewRequest(MethodPost, cst.ts.URL, body)
	2070  		if err != nil {
	2071  			t.Fatal(err)
	2072  		}
	2073  		resp, err := cst.tr.RoundTrip(req)
	2074  		if err != nil {
	2075  			t.Fatal(err)
	2076  		}
	2077  		resp.Body.Close()
	2078  	}
	2079  }
	2080  
	2081  // issue40382Body is an io.ReadCloser for TestClientCallsCloseOnlyOnce.
	2082  // Its Read reads n bytes before returning io.EOF.
	2083  // Its Close returns nil but fails the test if called more than once.
	2084  type issue40382Body struct {
	2085  	t								*testing.T
	2086  	n								int
	2087  	closeCallsAtomic int32
	2088  }
	2089  
	2090  func (b *issue40382Body) Read(p []byte) (int, error) {
	2091  	switch {
	2092  	case b.n == 0:
	2093  		return 0, io.EOF
	2094  	case b.n < len(p):
	2095  		p = p[:b.n]
	2096  		fallthrough
	2097  	default:
	2098  		for i := range p {
	2099  			p[i] = 'x'
	2100  		}
	2101  		b.n -= len(p)
	2102  		return len(p), nil
	2103  	}
	2104  }
	2105  
	2106  func (b *issue40382Body) Close() error {
	2107  	if atomic.AddInt32(&b.closeCallsAtomic, 1) == 2 {
	2108  		b.t.Error("Body closed more than once")
	2109  	}
	2110  	return nil
	2111  }
	2112  

View as plain text