github.com/guyezi/gofrontend@v0.0.0-20200228202240-7a62a49e62c0/libgo/go/net/http/clientserver_test.go (about)

     1  // Copyright 2015 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 that use both the client & server, in both HTTP/1 and HTTP/2 mode.
     6  
     7  package http_test
     8  
     9  import (
    10  	"bytes"
    11  	"compress/gzip"
    12  	"crypto/rand"
    13  	"crypto/sha1"
    14  	"crypto/tls"
    15  	"fmt"
    16  	"hash"
    17  	"io"
    18  	"io/ioutil"
    19  	"log"
    20  	"net"
    21  	. "net/http"
    22  	"net/http/httptest"
    23  	"net/http/httputil"
    24  	"net/url"
    25  	"os"
    26  	"reflect"
    27  	"runtime"
    28  	"sort"
    29  	"strings"
    30  	"sync"
    31  	"sync/atomic"
    32  	"testing"
    33  	"time"
    34  )
    35  
    36  type clientServerTest struct {
    37  	t  *testing.T
    38  	h2 bool
    39  	h  Handler
    40  	ts *httptest.Server
    41  	tr *Transport
    42  	c  *Client
    43  }
    44  
    45  func (t *clientServerTest) close() {
    46  	t.tr.CloseIdleConnections()
    47  	t.ts.Close()
    48  }
    49  
    50  func (t *clientServerTest) getURL(u string) string {
    51  	res, err := t.c.Get(u)
    52  	if err != nil {
    53  		t.t.Fatal(err)
    54  	}
    55  	defer res.Body.Close()
    56  	slurp, err := ioutil.ReadAll(res.Body)
    57  	if err != nil {
    58  		t.t.Fatal(err)
    59  	}
    60  	return string(slurp)
    61  }
    62  
    63  func (t *clientServerTest) scheme() string {
    64  	if t.h2 {
    65  		return "https"
    66  	}
    67  	return "http"
    68  }
    69  
    70  const (
    71  	h1Mode = false
    72  	h2Mode = true
    73  )
    74  
    75  var optQuietLog = func(ts *httptest.Server) {
    76  	ts.Config.ErrorLog = quietLog
    77  }
    78  
    79  func optWithServerLog(lg *log.Logger) func(*httptest.Server) {
    80  	return func(ts *httptest.Server) {
    81  		ts.Config.ErrorLog = lg
    82  	}
    83  }
    84  
    85  func newClientServerTest(t *testing.T, h2 bool, h Handler, opts ...interface{}) *clientServerTest {
    86  	if h2 {
    87  		CondSkipHTTP2(t)
    88  	}
    89  	cst := &clientServerTest{
    90  		t:  t,
    91  		h2: h2,
    92  		h:  h,
    93  		tr: &Transport{},
    94  	}
    95  	cst.c = &Client{Transport: cst.tr}
    96  	cst.ts = httptest.NewUnstartedServer(h)
    97  
    98  	for _, opt := range opts {
    99  		switch opt := opt.(type) {
   100  		case func(*Transport):
   101  			opt(cst.tr)
   102  		case func(*httptest.Server):
   103  			opt(cst.ts)
   104  		default:
   105  			t.Fatalf("unhandled option type %T", opt)
   106  		}
   107  	}
   108  
   109  	if !h2 {
   110  		cst.ts.Start()
   111  		return cst
   112  	}
   113  	ExportHttp2ConfigureServer(cst.ts.Config, nil)
   114  	cst.ts.TLS = cst.ts.Config.TLSConfig
   115  	cst.ts.StartTLS()
   116  
   117  	cst.tr.TLSClientConfig = &tls.Config{
   118  		InsecureSkipVerify: true,
   119  	}
   120  	if err := ExportHttp2ConfigureTransport(cst.tr); err != nil {
   121  		t.Fatal(err)
   122  	}
   123  	return cst
   124  }
   125  
   126  // Testing the newClientServerTest helper itself.
   127  func TestNewClientServerTest(t *testing.T) {
   128  	var got struct {
   129  		sync.Mutex
   130  		log []string
   131  	}
   132  	h := HandlerFunc(func(w ResponseWriter, r *Request) {
   133  		got.Lock()
   134  		defer got.Unlock()
   135  		got.log = append(got.log, r.Proto)
   136  	})
   137  	for _, v := range [2]bool{false, true} {
   138  		cst := newClientServerTest(t, v, h)
   139  		if _, err := cst.c.Head(cst.ts.URL); err != nil {
   140  			t.Fatal(err)
   141  		}
   142  		cst.close()
   143  	}
   144  	got.Lock() // no need to unlock
   145  	if want := []string{"HTTP/1.1", "HTTP/2.0"}; !reflect.DeepEqual(got.log, want) {
   146  		t.Errorf("got %q; want %q", got.log, want)
   147  	}
   148  }
   149  
   150  func TestChunkedResponseHeaders_h1(t *testing.T) { testChunkedResponseHeaders(t, h1Mode) }
   151  func TestChunkedResponseHeaders_h2(t *testing.T) { testChunkedResponseHeaders(t, h2Mode) }
   152  
   153  func testChunkedResponseHeaders(t *testing.T, h2 bool) {
   154  	defer afterTest(t)
   155  	log.SetOutput(ioutil.Discard) // is noisy otherwise
   156  	defer log.SetOutput(os.Stderr)
   157  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   158  		w.Header().Set("Content-Length", "intentional gibberish") // we check that this is deleted
   159  		w.(Flusher).Flush()
   160  		fmt.Fprintf(w, "I am a chunked response.")
   161  	}))
   162  	defer cst.close()
   163  
   164  	res, err := cst.c.Get(cst.ts.URL)
   165  	if err != nil {
   166  		t.Fatalf("Get error: %v", err)
   167  	}
   168  	defer res.Body.Close()
   169  	if g, e := res.ContentLength, int64(-1); g != e {
   170  		t.Errorf("expected ContentLength of %d; got %d", e, g)
   171  	}
   172  	wantTE := []string{"chunked"}
   173  	if h2 {
   174  		wantTE = nil
   175  	}
   176  	if !reflect.DeepEqual(res.TransferEncoding, wantTE) {
   177  		t.Errorf("TransferEncoding = %v; want %v", res.TransferEncoding, wantTE)
   178  	}
   179  	if got, haveCL := res.Header["Content-Length"]; haveCL {
   180  		t.Errorf("Unexpected Content-Length: %q", got)
   181  	}
   182  }
   183  
   184  type reqFunc func(c *Client, url string) (*Response, error)
   185  
   186  // h12Compare is a test that compares HTTP/1 and HTTP/2 behavior
   187  // against each other.
   188  type h12Compare struct {
   189  	Handler            func(ResponseWriter, *Request)    // required
   190  	ReqFunc            reqFunc                           // optional
   191  	CheckResponse      func(proto string, res *Response) // optional
   192  	EarlyCheckResponse func(proto string, res *Response) // optional; pre-normalize
   193  	Opts               []interface{}
   194  }
   195  
   196  func (tt h12Compare) reqFunc() reqFunc {
   197  	if tt.ReqFunc == nil {
   198  		return (*Client).Get
   199  	}
   200  	return tt.ReqFunc
   201  }
   202  
   203  func (tt h12Compare) run(t *testing.T) {
   204  	setParallel(t)
   205  	cst1 := newClientServerTest(t, false, HandlerFunc(tt.Handler), tt.Opts...)
   206  	defer cst1.close()
   207  	cst2 := newClientServerTest(t, true, HandlerFunc(tt.Handler), tt.Opts...)
   208  	defer cst2.close()
   209  
   210  	res1, err := tt.reqFunc()(cst1.c, cst1.ts.URL)
   211  	if err != nil {
   212  		t.Errorf("HTTP/1 request: %v", err)
   213  		return
   214  	}
   215  	res2, err := tt.reqFunc()(cst2.c, cst2.ts.URL)
   216  	if err != nil {
   217  		t.Errorf("HTTP/2 request: %v", err)
   218  		return
   219  	}
   220  
   221  	if fn := tt.EarlyCheckResponse; fn != nil {
   222  		fn("HTTP/1.1", res1)
   223  		fn("HTTP/2.0", res2)
   224  	}
   225  
   226  	tt.normalizeRes(t, res1, "HTTP/1.1")
   227  	tt.normalizeRes(t, res2, "HTTP/2.0")
   228  	res1body, res2body := res1.Body, res2.Body
   229  
   230  	eres1 := mostlyCopy(res1)
   231  	eres2 := mostlyCopy(res2)
   232  	if !reflect.DeepEqual(eres1, eres2) {
   233  		t.Errorf("Response headers to handler differed:\nhttp/1 (%v):\n\t%#v\nhttp/2 (%v):\n\t%#v",
   234  			cst1.ts.URL, eres1, cst2.ts.URL, eres2)
   235  	}
   236  	if !reflect.DeepEqual(res1body, res2body) {
   237  		t.Errorf("Response bodies to handler differed.\nhttp1: %v\nhttp2: %v\n", res1body, res2body)
   238  	}
   239  	if fn := tt.CheckResponse; fn != nil {
   240  		res1.Body, res2.Body = res1body, res2body
   241  		fn("HTTP/1.1", res1)
   242  		fn("HTTP/2.0", res2)
   243  	}
   244  }
   245  
   246  func mostlyCopy(r *Response) *Response {
   247  	c := *r
   248  	c.Body = nil
   249  	c.TransferEncoding = nil
   250  	c.TLS = nil
   251  	c.Request = nil
   252  	return &c
   253  }
   254  
   255  type slurpResult struct {
   256  	io.ReadCloser
   257  	body []byte
   258  	err  error
   259  }
   260  
   261  func (sr slurpResult) String() string { return fmt.Sprintf("body %q; err %v", sr.body, sr.err) }
   262  
   263  func (tt h12Compare) normalizeRes(t *testing.T, res *Response, wantProto string) {
   264  	if res.Proto == wantProto || res.Proto == "HTTP/IGNORE" {
   265  		res.Proto, res.ProtoMajor, res.ProtoMinor = "", 0, 0
   266  	} else {
   267  		t.Errorf("got %q response; want %q", res.Proto, wantProto)
   268  	}
   269  	slurp, err := ioutil.ReadAll(res.Body)
   270  
   271  	res.Body.Close()
   272  	res.Body = slurpResult{
   273  		ReadCloser: ioutil.NopCloser(bytes.NewReader(slurp)),
   274  		body:       slurp,
   275  		err:        err,
   276  	}
   277  	for i, v := range res.Header["Date"] {
   278  		res.Header["Date"][i] = strings.Repeat("x", len(v))
   279  	}
   280  	if res.Request == nil {
   281  		t.Errorf("for %s, no request", wantProto)
   282  	}
   283  	if (res.TLS != nil) != (wantProto == "HTTP/2.0") {
   284  		t.Errorf("TLS set = %v; want %v", res.TLS != nil, res.TLS == nil)
   285  	}
   286  }
   287  
   288  // Issue 13532
   289  func TestH12_HeadContentLengthNoBody(t *testing.T) {
   290  	h12Compare{
   291  		ReqFunc: (*Client).Head,
   292  		Handler: func(w ResponseWriter, r *Request) {
   293  		},
   294  	}.run(t)
   295  }
   296  
   297  func TestH12_HeadContentLengthSmallBody(t *testing.T) {
   298  	h12Compare{
   299  		ReqFunc: (*Client).Head,
   300  		Handler: func(w ResponseWriter, r *Request) {
   301  			io.WriteString(w, "small")
   302  		},
   303  	}.run(t)
   304  }
   305  
   306  func TestH12_HeadContentLengthLargeBody(t *testing.T) {
   307  	h12Compare{
   308  		ReqFunc: (*Client).Head,
   309  		Handler: func(w ResponseWriter, r *Request) {
   310  			chunk := strings.Repeat("x", 512<<10)
   311  			for i := 0; i < 10; i++ {
   312  				io.WriteString(w, chunk)
   313  			}
   314  		},
   315  	}.run(t)
   316  }
   317  
   318  func TestH12_200NoBody(t *testing.T) {
   319  	h12Compare{Handler: func(w ResponseWriter, r *Request) {}}.run(t)
   320  }
   321  
   322  func TestH2_204NoBody(t *testing.T) { testH12_noBody(t, 204) }
   323  func TestH2_304NoBody(t *testing.T) { testH12_noBody(t, 304) }
   324  func TestH2_404NoBody(t *testing.T) { testH12_noBody(t, 404) }
   325  
   326  func testH12_noBody(t *testing.T, status int) {
   327  	h12Compare{Handler: func(w ResponseWriter, r *Request) {
   328  		w.WriteHeader(status)
   329  	}}.run(t)
   330  }
   331  
   332  func TestH12_SmallBody(t *testing.T) {
   333  	h12Compare{Handler: func(w ResponseWriter, r *Request) {
   334  		io.WriteString(w, "small body")
   335  	}}.run(t)
   336  }
   337  
   338  func TestH12_ExplicitContentLength(t *testing.T) {
   339  	h12Compare{Handler: func(w ResponseWriter, r *Request) {
   340  		w.Header().Set("Content-Length", "3")
   341  		io.WriteString(w, "foo")
   342  	}}.run(t)
   343  }
   344  
   345  func TestH12_FlushBeforeBody(t *testing.T) {
   346  	h12Compare{Handler: func(w ResponseWriter, r *Request) {
   347  		w.(Flusher).Flush()
   348  		io.WriteString(w, "foo")
   349  	}}.run(t)
   350  }
   351  
   352  func TestH12_FlushMidBody(t *testing.T) {
   353  	h12Compare{Handler: func(w ResponseWriter, r *Request) {
   354  		io.WriteString(w, "foo")
   355  		w.(Flusher).Flush()
   356  		io.WriteString(w, "bar")
   357  	}}.run(t)
   358  }
   359  
   360  func TestH12_Head_ExplicitLen(t *testing.T) {
   361  	h12Compare{
   362  		ReqFunc: (*Client).Head,
   363  		Handler: func(w ResponseWriter, r *Request) {
   364  			if r.Method != "HEAD" {
   365  				t.Errorf("unexpected method %q", r.Method)
   366  			}
   367  			w.Header().Set("Content-Length", "1235")
   368  		},
   369  	}.run(t)
   370  }
   371  
   372  func TestH12_Head_ImplicitLen(t *testing.T) {
   373  	h12Compare{
   374  		ReqFunc: (*Client).Head,
   375  		Handler: func(w ResponseWriter, r *Request) {
   376  			if r.Method != "HEAD" {
   377  				t.Errorf("unexpected method %q", r.Method)
   378  			}
   379  			io.WriteString(w, "foo")
   380  		},
   381  	}.run(t)
   382  }
   383  
   384  func TestH12_HandlerWritesTooLittle(t *testing.T) {
   385  	h12Compare{
   386  		Handler: func(w ResponseWriter, r *Request) {
   387  			w.Header().Set("Content-Length", "3")
   388  			io.WriteString(w, "12") // one byte short
   389  		},
   390  		CheckResponse: func(proto string, res *Response) {
   391  			sr, ok := res.Body.(slurpResult)
   392  			if !ok {
   393  				t.Errorf("%s body is %T; want slurpResult", proto, res.Body)
   394  				return
   395  			}
   396  			if sr.err != io.ErrUnexpectedEOF {
   397  				t.Errorf("%s read error = %v; want io.ErrUnexpectedEOF", proto, sr.err)
   398  			}
   399  			if string(sr.body) != "12" {
   400  				t.Errorf("%s body = %q; want %q", proto, sr.body, "12")
   401  			}
   402  		},
   403  	}.run(t)
   404  }
   405  
   406  // Tests that the HTTP/1 and HTTP/2 servers prevent handlers from
   407  // writing more than they declared. This test does not test whether
   408  // the transport deals with too much data, though, since the server
   409  // doesn't make it possible to send bogus data. For those tests, see
   410  // transport_test.go (for HTTP/1) or x/net/http2/transport_test.go
   411  // (for HTTP/2).
   412  func TestH12_HandlerWritesTooMuch(t *testing.T) {
   413  	h12Compare{
   414  		Handler: func(w ResponseWriter, r *Request) {
   415  			w.Header().Set("Content-Length", "3")
   416  			w.(Flusher).Flush()
   417  			io.WriteString(w, "123")
   418  			w.(Flusher).Flush()
   419  			n, err := io.WriteString(w, "x") // too many
   420  			if n > 0 || err == nil {
   421  				t.Errorf("for proto %q, final write = %v, %v; want 0, some error", r.Proto, n, err)
   422  			}
   423  		},
   424  	}.run(t)
   425  }
   426  
   427  // Verify that both our HTTP/1 and HTTP/2 request and auto-decompress gzip.
   428  // Some hosts send gzip even if you don't ask for it; see golang.org/issue/13298
   429  func TestH12_AutoGzip(t *testing.T) {
   430  	h12Compare{
   431  		Handler: func(w ResponseWriter, r *Request) {
   432  			if ae := r.Header.Get("Accept-Encoding"); ae != "gzip" {
   433  				t.Errorf("%s Accept-Encoding = %q; want gzip", r.Proto, ae)
   434  			}
   435  			w.Header().Set("Content-Encoding", "gzip")
   436  			gz := gzip.NewWriter(w)
   437  			io.WriteString(gz, "I am some gzipped content. Go go go go go go go go go go go go should compress well.")
   438  			gz.Close()
   439  		},
   440  	}.run(t)
   441  }
   442  
   443  func TestH12_AutoGzip_Disabled(t *testing.T) {
   444  	h12Compare{
   445  		Opts: []interface{}{
   446  			func(tr *Transport) { tr.DisableCompression = true },
   447  		},
   448  		Handler: func(w ResponseWriter, r *Request) {
   449  			fmt.Fprintf(w, "%q", r.Header["Accept-Encoding"])
   450  			if ae := r.Header.Get("Accept-Encoding"); ae != "" {
   451  				t.Errorf("%s Accept-Encoding = %q; want empty", r.Proto, ae)
   452  			}
   453  		},
   454  	}.run(t)
   455  }
   456  
   457  // Test304Responses verifies that 304s don't declare that they're
   458  // chunking in their response headers and aren't allowed to produce
   459  // output.
   460  func Test304Responses_h1(t *testing.T) { test304Responses(t, h1Mode) }
   461  func Test304Responses_h2(t *testing.T) { test304Responses(t, h2Mode) }
   462  
   463  func test304Responses(t *testing.T, h2 bool) {
   464  	defer afterTest(t)
   465  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   466  		w.WriteHeader(StatusNotModified)
   467  		_, err := w.Write([]byte("illegal body"))
   468  		if err != ErrBodyNotAllowed {
   469  			t.Errorf("on Write, expected ErrBodyNotAllowed, got %v", err)
   470  		}
   471  	}))
   472  	defer cst.close()
   473  	res, err := cst.c.Get(cst.ts.URL)
   474  	if err != nil {
   475  		t.Fatal(err)
   476  	}
   477  	if len(res.TransferEncoding) > 0 {
   478  		t.Errorf("expected no TransferEncoding; got %v", res.TransferEncoding)
   479  	}
   480  	body, err := ioutil.ReadAll(res.Body)
   481  	if err != nil {
   482  		t.Error(err)
   483  	}
   484  	if len(body) > 0 {
   485  		t.Errorf("got unexpected body %q", string(body))
   486  	}
   487  }
   488  
   489  func TestH12_ServerEmptyContentLength(t *testing.T) {
   490  	h12Compare{
   491  		Handler: func(w ResponseWriter, r *Request) {
   492  			w.Header()["Content-Type"] = []string{""}
   493  			io.WriteString(w, "<html><body>hi</body></html>")
   494  		},
   495  	}.run(t)
   496  }
   497  
   498  func TestH12_RequestContentLength_Known_NonZero(t *testing.T) {
   499  	h12requestContentLength(t, func() io.Reader { return strings.NewReader("FOUR") }, 4)
   500  }
   501  
   502  func TestH12_RequestContentLength_Known_Zero(t *testing.T) {
   503  	h12requestContentLength(t, func() io.Reader { return nil }, 0)
   504  }
   505  
   506  func TestH12_RequestContentLength_Unknown(t *testing.T) {
   507  	h12requestContentLength(t, func() io.Reader { return struct{ io.Reader }{strings.NewReader("Stuff")} }, -1)
   508  }
   509  
   510  func h12requestContentLength(t *testing.T, bodyfn func() io.Reader, wantLen int64) {
   511  	h12Compare{
   512  		Handler: func(w ResponseWriter, r *Request) {
   513  			w.Header().Set("Got-Length", fmt.Sprint(r.ContentLength))
   514  			fmt.Fprintf(w, "Req.ContentLength=%v", r.ContentLength)
   515  		},
   516  		ReqFunc: func(c *Client, url string) (*Response, error) {
   517  			return c.Post(url, "text/plain", bodyfn())
   518  		},
   519  		CheckResponse: func(proto string, res *Response) {
   520  			if got, want := res.Header.Get("Got-Length"), fmt.Sprint(wantLen); got != want {
   521  				t.Errorf("Proto %q got length %q; want %q", proto, got, want)
   522  			}
   523  		},
   524  	}.run(t)
   525  }
   526  
   527  // Tests that closing the Request.Cancel channel also while still
   528  // reading the response body. Issue 13159.
   529  func TestCancelRequestMidBody_h1(t *testing.T) { testCancelRequestMidBody(t, h1Mode) }
   530  func TestCancelRequestMidBody_h2(t *testing.T) { testCancelRequestMidBody(t, h2Mode) }
   531  func testCancelRequestMidBody(t *testing.T, h2 bool) {
   532  	defer afterTest(t)
   533  	unblock := make(chan bool)
   534  	didFlush := make(chan bool, 1)
   535  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   536  		io.WriteString(w, "Hello")
   537  		w.(Flusher).Flush()
   538  		didFlush <- true
   539  		<-unblock
   540  		io.WriteString(w, ", world.")
   541  	}))
   542  	defer cst.close()
   543  	defer close(unblock)
   544  
   545  	req, _ := NewRequest("GET", cst.ts.URL, nil)
   546  	cancel := make(chan struct{})
   547  	req.Cancel = cancel
   548  
   549  	res, err := cst.c.Do(req)
   550  	if err != nil {
   551  		t.Fatal(err)
   552  	}
   553  	defer res.Body.Close()
   554  	<-didFlush
   555  
   556  	// Read a bit before we cancel. (Issue 13626)
   557  	// We should have "Hello" at least sitting there.
   558  	firstRead := make([]byte, 10)
   559  	n, err := res.Body.Read(firstRead)
   560  	if err != nil {
   561  		t.Fatal(err)
   562  	}
   563  	firstRead = firstRead[:n]
   564  
   565  	close(cancel)
   566  
   567  	rest, err := ioutil.ReadAll(res.Body)
   568  	all := string(firstRead) + string(rest)
   569  	if all != "Hello" {
   570  		t.Errorf("Read %q (%q + %q); want Hello", all, firstRead, rest)
   571  	}
   572  	if err != ExportErrRequestCanceled {
   573  		t.Errorf("ReadAll error = %v; want %v", err, ExportErrRequestCanceled)
   574  	}
   575  }
   576  
   577  // Tests that clients can send trailers to a server and that the server can read them.
   578  func TestTrailersClientToServer_h1(t *testing.T) { testTrailersClientToServer(t, h1Mode) }
   579  func TestTrailersClientToServer_h2(t *testing.T) { testTrailersClientToServer(t, h2Mode) }
   580  
   581  func testTrailersClientToServer(t *testing.T, h2 bool) {
   582  	defer afterTest(t)
   583  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   584  		var decl []string
   585  		for k := range r.Trailer {
   586  			decl = append(decl, k)
   587  		}
   588  		sort.Strings(decl)
   589  
   590  		slurp, err := ioutil.ReadAll(r.Body)
   591  		if err != nil {
   592  			t.Errorf("Server reading request body: %v", err)
   593  		}
   594  		if string(slurp) != "foo" {
   595  			t.Errorf("Server read request body %q; want foo", slurp)
   596  		}
   597  		if r.Trailer == nil {
   598  			io.WriteString(w, "nil Trailer")
   599  		} else {
   600  			fmt.Fprintf(w, "decl: %v, vals: %s, %s",
   601  				decl,
   602  				r.Trailer.Get("Client-Trailer-A"),
   603  				r.Trailer.Get("Client-Trailer-B"))
   604  		}
   605  	}))
   606  	defer cst.close()
   607  
   608  	var req *Request
   609  	req, _ = NewRequest("POST", cst.ts.URL, io.MultiReader(
   610  		eofReaderFunc(func() {
   611  			req.Trailer["Client-Trailer-A"] = []string{"valuea"}
   612  		}),
   613  		strings.NewReader("foo"),
   614  		eofReaderFunc(func() {
   615  			req.Trailer["Client-Trailer-B"] = []string{"valueb"}
   616  		}),
   617  	))
   618  	req.Trailer = Header{
   619  		"Client-Trailer-A": nil, //  to be set later
   620  		"Client-Trailer-B": nil, //  to be set later
   621  	}
   622  	req.ContentLength = -1
   623  	res, err := cst.c.Do(req)
   624  	if err != nil {
   625  		t.Fatal(err)
   626  	}
   627  	if err := wantBody(res, err, "decl: [Client-Trailer-A Client-Trailer-B], vals: valuea, valueb"); err != nil {
   628  		t.Error(err)
   629  	}
   630  }
   631  
   632  // Tests that servers send trailers to a client and that the client can read them.
   633  func TestTrailersServerToClient_h1(t *testing.T)       { testTrailersServerToClient(t, h1Mode, false) }
   634  func TestTrailersServerToClient_h2(t *testing.T)       { testTrailersServerToClient(t, h2Mode, false) }
   635  func TestTrailersServerToClient_Flush_h1(t *testing.T) { testTrailersServerToClient(t, h1Mode, true) }
   636  func TestTrailersServerToClient_Flush_h2(t *testing.T) { testTrailersServerToClient(t, h2Mode, true) }
   637  
   638  func testTrailersServerToClient(t *testing.T, h2, flush bool) {
   639  	defer afterTest(t)
   640  	const body = "Some body"
   641  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   642  		w.Header().Set("Trailer", "Server-Trailer-A, Server-Trailer-B")
   643  		w.Header().Add("Trailer", "Server-Trailer-C")
   644  
   645  		io.WriteString(w, body)
   646  		if flush {
   647  			w.(Flusher).Flush()
   648  		}
   649  
   650  		// How handlers set Trailers: declare it ahead of time
   651  		// with the Trailer header, and then mutate the
   652  		// Header() of those values later, after the response
   653  		// has been written (we wrote to w above).
   654  		w.Header().Set("Server-Trailer-A", "valuea")
   655  		w.Header().Set("Server-Trailer-C", "valuec") // skipping B
   656  		w.Header().Set("Server-Trailer-NotDeclared", "should be omitted")
   657  	}))
   658  	defer cst.close()
   659  
   660  	res, err := cst.c.Get(cst.ts.URL)
   661  	if err != nil {
   662  		t.Fatal(err)
   663  	}
   664  
   665  	wantHeader := Header{
   666  		"Content-Type": {"text/plain; charset=utf-8"},
   667  	}
   668  	wantLen := -1
   669  	if h2 && !flush {
   670  		// In HTTP/1.1, any use of trailers forces HTTP/1.1
   671  		// chunking and a flush at the first write. That's
   672  		// unnecessary with HTTP/2's framing, so the server
   673  		// is able to calculate the length while still sending
   674  		// trailers afterwards.
   675  		wantLen = len(body)
   676  		wantHeader["Content-Length"] = []string{fmt.Sprint(wantLen)}
   677  	}
   678  	if res.ContentLength != int64(wantLen) {
   679  		t.Errorf("ContentLength = %v; want %v", res.ContentLength, wantLen)
   680  	}
   681  
   682  	delete(res.Header, "Date") // irrelevant for test
   683  	if !reflect.DeepEqual(res.Header, wantHeader) {
   684  		t.Errorf("Header = %v; want %v", res.Header, wantHeader)
   685  	}
   686  
   687  	if got, want := res.Trailer, (Header{
   688  		"Server-Trailer-A": nil,
   689  		"Server-Trailer-B": nil,
   690  		"Server-Trailer-C": nil,
   691  	}); !reflect.DeepEqual(got, want) {
   692  		t.Errorf("Trailer before body read = %v; want %v", got, want)
   693  	}
   694  
   695  	if err := wantBody(res, nil, body); err != nil {
   696  		t.Fatal(err)
   697  	}
   698  
   699  	if got, want := res.Trailer, (Header{
   700  		"Server-Trailer-A": {"valuea"},
   701  		"Server-Trailer-B": nil,
   702  		"Server-Trailer-C": {"valuec"},
   703  	}); !reflect.DeepEqual(got, want) {
   704  		t.Errorf("Trailer after body read = %v; want %v", got, want)
   705  	}
   706  }
   707  
   708  // Don't allow a Body.Read after Body.Close. Issue 13648.
   709  func TestResponseBodyReadAfterClose_h1(t *testing.T) { testResponseBodyReadAfterClose(t, h1Mode) }
   710  func TestResponseBodyReadAfterClose_h2(t *testing.T) { testResponseBodyReadAfterClose(t, h2Mode) }
   711  
   712  func testResponseBodyReadAfterClose(t *testing.T, h2 bool) {
   713  	defer afterTest(t)
   714  	const body = "Some body"
   715  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   716  		io.WriteString(w, body)
   717  	}))
   718  	defer cst.close()
   719  	res, err := cst.c.Get(cst.ts.URL)
   720  	if err != nil {
   721  		t.Fatal(err)
   722  	}
   723  	res.Body.Close()
   724  	data, err := ioutil.ReadAll(res.Body)
   725  	if len(data) != 0 || err == nil {
   726  		t.Fatalf("ReadAll returned %q, %v; want error", data, err)
   727  	}
   728  }
   729  
   730  func TestConcurrentReadWriteReqBody_h1(t *testing.T) { testConcurrentReadWriteReqBody(t, h1Mode) }
   731  func TestConcurrentReadWriteReqBody_h2(t *testing.T) { testConcurrentReadWriteReqBody(t, h2Mode) }
   732  func testConcurrentReadWriteReqBody(t *testing.T, h2 bool) {
   733  	defer afterTest(t)
   734  	const reqBody = "some request body"
   735  	const resBody = "some response body"
   736  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   737  		var wg sync.WaitGroup
   738  		wg.Add(2)
   739  		didRead := make(chan bool, 1)
   740  		// Read in one goroutine.
   741  		go func() {
   742  			defer wg.Done()
   743  			data, err := ioutil.ReadAll(r.Body)
   744  			if string(data) != reqBody {
   745  				t.Errorf("Handler read %q; want %q", data, reqBody)
   746  			}
   747  			if err != nil {
   748  				t.Errorf("Handler Read: %v", err)
   749  			}
   750  			didRead <- true
   751  		}()
   752  		// Write in another goroutine.
   753  		go func() {
   754  			defer wg.Done()
   755  			if !h2 {
   756  				// our HTTP/1 implementation intentionally
   757  				// doesn't permit writes during read (mostly
   758  				// due to it being undefined); if that is ever
   759  				// relaxed, change this.
   760  				<-didRead
   761  			}
   762  			io.WriteString(w, resBody)
   763  		}()
   764  		wg.Wait()
   765  	}))
   766  	defer cst.close()
   767  	req, _ := NewRequest("POST", cst.ts.URL, strings.NewReader(reqBody))
   768  	req.Header.Add("Expect", "100-continue") // just to complicate things
   769  	res, err := cst.c.Do(req)
   770  	if err != nil {
   771  		t.Fatal(err)
   772  	}
   773  	data, err := ioutil.ReadAll(res.Body)
   774  	defer res.Body.Close()
   775  	if err != nil {
   776  		t.Fatal(err)
   777  	}
   778  	if string(data) != resBody {
   779  		t.Errorf("read %q; want %q", data, resBody)
   780  	}
   781  }
   782  
   783  func TestConnectRequest_h1(t *testing.T) { testConnectRequest(t, h1Mode) }
   784  func TestConnectRequest_h2(t *testing.T) { testConnectRequest(t, h2Mode) }
   785  func testConnectRequest(t *testing.T, h2 bool) {
   786  	defer afterTest(t)
   787  	gotc := make(chan *Request, 1)
   788  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   789  		gotc <- r
   790  	}))
   791  	defer cst.close()
   792  
   793  	u, err := url.Parse(cst.ts.URL)
   794  	if err != nil {
   795  		t.Fatal(err)
   796  	}
   797  
   798  	tests := []struct {
   799  		req  *Request
   800  		want string
   801  	}{
   802  		{
   803  			req: &Request{
   804  				Method: "CONNECT",
   805  				Header: Header{},
   806  				URL:    u,
   807  			},
   808  			want: u.Host,
   809  		},
   810  		{
   811  			req: &Request{
   812  				Method: "CONNECT",
   813  				Header: Header{},
   814  				URL:    u,
   815  				Host:   "example.com:123",
   816  			},
   817  			want: "example.com:123",
   818  		},
   819  	}
   820  
   821  	for i, tt := range tests {
   822  		res, err := cst.c.Do(tt.req)
   823  		if err != nil {
   824  			t.Errorf("%d. RoundTrip = %v", i, err)
   825  			continue
   826  		}
   827  		res.Body.Close()
   828  		req := <-gotc
   829  		if req.Method != "CONNECT" {
   830  			t.Errorf("method = %q; want CONNECT", req.Method)
   831  		}
   832  		if req.Host != tt.want {
   833  			t.Errorf("Host = %q; want %q", req.Host, tt.want)
   834  		}
   835  		if req.URL.Host != tt.want {
   836  			t.Errorf("URL.Host = %q; want %q", req.URL.Host, tt.want)
   837  		}
   838  	}
   839  }
   840  
   841  func TestTransportUserAgent_h1(t *testing.T) { testTransportUserAgent(t, h1Mode) }
   842  func TestTransportUserAgent_h2(t *testing.T) { testTransportUserAgent(t, h2Mode) }
   843  func testTransportUserAgent(t *testing.T, h2 bool) {
   844  	defer afterTest(t)
   845  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   846  		fmt.Fprintf(w, "%q", r.Header["User-Agent"])
   847  	}))
   848  	defer cst.close()
   849  
   850  	either := func(a, b string) string {
   851  		if h2 {
   852  			return b
   853  		}
   854  		return a
   855  	}
   856  
   857  	tests := []struct {
   858  		setup func(*Request)
   859  		want  string
   860  	}{
   861  		{
   862  			func(r *Request) {},
   863  			either(`["Go-http-client/1.1"]`, `["Go-http-client/2.0"]`),
   864  		},
   865  		{
   866  			func(r *Request) { r.Header.Set("User-Agent", "foo/1.2.3") },
   867  			`["foo/1.2.3"]`,
   868  		},
   869  		{
   870  			func(r *Request) { r.Header["User-Agent"] = []string{"single", "or", "multiple"} },
   871  			`["single"]`,
   872  		},
   873  		{
   874  			func(r *Request) { r.Header.Set("User-Agent", "") },
   875  			`[]`,
   876  		},
   877  		{
   878  			func(r *Request) { r.Header["User-Agent"] = nil },
   879  			`[]`,
   880  		},
   881  	}
   882  	for i, tt := range tests {
   883  		req, _ := NewRequest("GET", cst.ts.URL, nil)
   884  		tt.setup(req)
   885  		res, err := cst.c.Do(req)
   886  		if err != nil {
   887  			t.Errorf("%d. RoundTrip = %v", i, err)
   888  			continue
   889  		}
   890  		slurp, err := ioutil.ReadAll(res.Body)
   891  		res.Body.Close()
   892  		if err != nil {
   893  			t.Errorf("%d. read body = %v", i, err)
   894  			continue
   895  		}
   896  		if string(slurp) != tt.want {
   897  			t.Errorf("%d. body mismatch.\n got: %s\nwant: %s\n", i, slurp, tt.want)
   898  		}
   899  	}
   900  }
   901  
   902  func TestStarRequestFoo_h1(t *testing.T)     { testStarRequest(t, "FOO", h1Mode) }
   903  func TestStarRequestFoo_h2(t *testing.T)     { testStarRequest(t, "FOO", h2Mode) }
   904  func TestStarRequestOptions_h1(t *testing.T) { testStarRequest(t, "OPTIONS", h1Mode) }
   905  func TestStarRequestOptions_h2(t *testing.T) { testStarRequest(t, "OPTIONS", h2Mode) }
   906  func testStarRequest(t *testing.T, method string, h2 bool) {
   907  	defer afterTest(t)
   908  	gotc := make(chan *Request, 1)
   909  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
   910  		w.Header().Set("foo", "bar")
   911  		gotc <- r
   912  		w.(Flusher).Flush()
   913  	}))
   914  	defer cst.close()
   915  
   916  	u, err := url.Parse(cst.ts.URL)
   917  	if err != nil {
   918  		t.Fatal(err)
   919  	}
   920  	u.Path = "*"
   921  
   922  	req := &Request{
   923  		Method: method,
   924  		Header: Header{},
   925  		URL:    u,
   926  	}
   927  
   928  	res, err := cst.c.Do(req)
   929  	if err != nil {
   930  		t.Fatalf("RoundTrip = %v", err)
   931  	}
   932  	res.Body.Close()
   933  
   934  	wantFoo := "bar"
   935  	wantLen := int64(-1)
   936  	if method == "OPTIONS" {
   937  		wantFoo = ""
   938  		wantLen = 0
   939  	}
   940  	if res.StatusCode != 200 {
   941  		t.Errorf("status code = %v; want %d", res.Status, 200)
   942  	}
   943  	if res.ContentLength != wantLen {
   944  		t.Errorf("content length = %v; want %d", res.ContentLength, wantLen)
   945  	}
   946  	if got := res.Header.Get("foo"); got != wantFoo {
   947  		t.Errorf("response \"foo\" header = %q; want %q", got, wantFoo)
   948  	}
   949  	select {
   950  	case req = <-gotc:
   951  	default:
   952  		req = nil
   953  	}
   954  	if req == nil {
   955  		if method != "OPTIONS" {
   956  			t.Fatalf("handler never got request")
   957  		}
   958  		return
   959  	}
   960  	if req.Method != method {
   961  		t.Errorf("method = %q; want %q", req.Method, method)
   962  	}
   963  	if req.URL.Path != "*" {
   964  		t.Errorf("URL.Path = %q; want *", req.URL.Path)
   965  	}
   966  	if req.RequestURI != "*" {
   967  		t.Errorf("RequestURI = %q; want *", req.RequestURI)
   968  	}
   969  }
   970  
   971  // Issue 13957
   972  func TestTransportDiscardsUnneededConns(t *testing.T) {
   973  	setParallel(t)
   974  	defer afterTest(t)
   975  	cst := newClientServerTest(t, h2Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
   976  		fmt.Fprintf(w, "Hello, %v", r.RemoteAddr)
   977  	}))
   978  	defer cst.close()
   979  
   980  	var numOpen, numClose int32 // atomic
   981  
   982  	tlsConfig := &tls.Config{InsecureSkipVerify: true}
   983  	tr := &Transport{
   984  		TLSClientConfig: tlsConfig,
   985  		DialTLS: func(_, addr string) (net.Conn, error) {
   986  			time.Sleep(10 * time.Millisecond)
   987  			rc, err := net.Dial("tcp", addr)
   988  			if err != nil {
   989  				return nil, err
   990  			}
   991  			atomic.AddInt32(&numOpen, 1)
   992  			c := noteCloseConn{rc, func() { atomic.AddInt32(&numClose, 1) }}
   993  			return tls.Client(c, tlsConfig), nil
   994  		},
   995  	}
   996  	if err := ExportHttp2ConfigureTransport(tr); err != nil {
   997  		t.Fatal(err)
   998  	}
   999  	defer tr.CloseIdleConnections()
  1000  
  1001  	c := &Client{Transport: tr}
  1002  
  1003  	const N = 10
  1004  	gotBody := make(chan string, N)
  1005  	var wg sync.WaitGroup
  1006  	for i := 0; i < N; i++ {
  1007  		wg.Add(1)
  1008  		go func() {
  1009  			defer wg.Done()
  1010  			resp, err := c.Get(cst.ts.URL)
  1011  			if err != nil {
  1012  				t.Errorf("Get: %v", err)
  1013  				return
  1014  			}
  1015  			defer resp.Body.Close()
  1016  			slurp, err := ioutil.ReadAll(resp.Body)
  1017  			if err != nil {
  1018  				t.Error(err)
  1019  			}
  1020  			gotBody <- string(slurp)
  1021  		}()
  1022  	}
  1023  	wg.Wait()
  1024  	close(gotBody)
  1025  
  1026  	var last string
  1027  	for got := range gotBody {
  1028  		if last == "" {
  1029  			last = got
  1030  			continue
  1031  		}
  1032  		if got != last {
  1033  			t.Errorf("Response body changed: %q -> %q", last, got)
  1034  		}
  1035  	}
  1036  
  1037  	var open, close int32
  1038  	for i := 0; i < 150; i++ {
  1039  		open, close = atomic.LoadInt32(&numOpen), atomic.LoadInt32(&numClose)
  1040  		if open < 1 {
  1041  			t.Fatalf("open = %d; want at least", open)
  1042  		}
  1043  		if close == open-1 {
  1044  			// Success
  1045  			return
  1046  		}
  1047  		time.Sleep(10 * time.Millisecond)
  1048  	}
  1049  	t.Errorf("%d connections opened, %d closed; want %d to close", open, close, open-1)
  1050  }
  1051  
  1052  // tests that Transport doesn't retain a pointer to the provided request.
  1053  func TestTransportGCRequest_Body_h1(t *testing.T)   { testTransportGCRequest(t, h1Mode, true) }
  1054  func TestTransportGCRequest_Body_h2(t *testing.T)   { testTransportGCRequest(t, h2Mode, true) }
  1055  func TestTransportGCRequest_NoBody_h1(t *testing.T) { testTransportGCRequest(t, h1Mode, false) }
  1056  func TestTransportGCRequest_NoBody_h2(t *testing.T) { testTransportGCRequest(t, h2Mode, false) }
  1057  func testTransportGCRequest(t *testing.T, h2, body bool) {
  1058  	if runtime.Compiler == "gccgo" {
  1059  		t.Skip("skipping on gccgo because conservative GC means that finalizer may never run")
  1060  	}
  1061  
  1062  	setParallel(t)
  1063  	defer afterTest(t)
  1064  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
  1065  		ioutil.ReadAll(r.Body)
  1066  		if body {
  1067  			io.WriteString(w, "Hello.")
  1068  		}
  1069  	}))
  1070  	defer cst.close()
  1071  
  1072  	didGC := make(chan struct{})
  1073  	(func() {
  1074  		body := strings.NewReader("some body")
  1075  		req, _ := NewRequest("POST", cst.ts.URL, body)
  1076  		runtime.SetFinalizer(req, func(*Request) { close(didGC) })
  1077  		res, err := cst.c.Do(req)
  1078  		if err != nil {
  1079  			t.Fatal(err)
  1080  		}
  1081  		if _, err := ioutil.ReadAll(res.Body); err != nil {
  1082  			t.Fatal(err)
  1083  		}
  1084  		if err := res.Body.Close(); err != nil {
  1085  			t.Fatal(err)
  1086  		}
  1087  	})()
  1088  	timeout := time.NewTimer(5 * time.Second)
  1089  	defer timeout.Stop()
  1090  	for {
  1091  		select {
  1092  		case <-didGC:
  1093  			return
  1094  		case <-time.After(100 * time.Millisecond):
  1095  			runtime.GC()
  1096  		case <-timeout.C:
  1097  			t.Fatal("never saw GC of request")
  1098  		}
  1099  	}
  1100  }
  1101  
  1102  func TestTransportRejectsInvalidHeaders_h1(t *testing.T) {
  1103  	testTransportRejectsInvalidHeaders(t, h1Mode)
  1104  }
  1105  func TestTransportRejectsInvalidHeaders_h2(t *testing.T) {
  1106  	testTransportRejectsInvalidHeaders(t, h2Mode)
  1107  }
  1108  func testTransportRejectsInvalidHeaders(t *testing.T, h2 bool) {
  1109  	setParallel(t)
  1110  	defer afterTest(t)
  1111  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
  1112  		fmt.Fprintf(w, "Handler saw headers: %q", r.Header)
  1113  	}), optQuietLog)
  1114  	defer cst.close()
  1115  	cst.tr.DisableKeepAlives = true
  1116  
  1117  	tests := []struct {
  1118  		key, val string
  1119  		ok       bool
  1120  	}{
  1121  		{"Foo", "capital-key", true}, // verify h2 allows capital keys
  1122  		{"Foo", "foo\x00bar", false}, // \x00 byte in value not allowed
  1123  		{"Foo", "two\nlines", false}, // \n byte in value not allowed
  1124  		{"bogus\nkey", "v", false},   // \n byte also not allowed in key
  1125  		{"A space", "v", false},      // spaces in keys not allowed
  1126  		{"имя", "v", false},          // key must be ascii
  1127  		{"name", "валю", true},       // value may be non-ascii
  1128  		{"", "v", false},             // key must be non-empty
  1129  		{"k", "", true},              // value may be empty
  1130  	}
  1131  	for _, tt := range tests {
  1132  		dialedc := make(chan bool, 1)
  1133  		cst.tr.Dial = func(netw, addr string) (net.Conn, error) {
  1134  			dialedc <- true
  1135  			return net.Dial(netw, addr)
  1136  		}
  1137  		req, _ := NewRequest("GET", cst.ts.URL, nil)
  1138  		req.Header[tt.key] = []string{tt.val}
  1139  		res, err := cst.c.Do(req)
  1140  		var body []byte
  1141  		if err == nil {
  1142  			body, _ = ioutil.ReadAll(res.Body)
  1143  			res.Body.Close()
  1144  		}
  1145  		var dialed bool
  1146  		select {
  1147  		case <-dialedc:
  1148  			dialed = true
  1149  		default:
  1150  		}
  1151  
  1152  		if !tt.ok && dialed {
  1153  			t.Errorf("For key %q, value %q, transport dialed. Expected local failure. Response was: (%v, %v)\nServer replied with: %s", tt.key, tt.val, res, err, body)
  1154  		} else if (err == nil) != tt.ok {
  1155  			t.Errorf("For key %q, value %q; got err = %v; want ok=%v", tt.key, tt.val, err, tt.ok)
  1156  		}
  1157  	}
  1158  }
  1159  
  1160  func TestInterruptWithPanic_h1(t *testing.T)     { testInterruptWithPanic(t, h1Mode, "boom") }
  1161  func TestInterruptWithPanic_h2(t *testing.T)     { testInterruptWithPanic(t, h2Mode, "boom") }
  1162  func TestInterruptWithPanic_nil_h1(t *testing.T) { testInterruptWithPanic(t, h1Mode, nil) }
  1163  func TestInterruptWithPanic_nil_h2(t *testing.T) { testInterruptWithPanic(t, h2Mode, nil) }
  1164  func TestInterruptWithPanic_ErrAbortHandler_h1(t *testing.T) {
  1165  	testInterruptWithPanic(t, h1Mode, ErrAbortHandler)
  1166  }
  1167  func TestInterruptWithPanic_ErrAbortHandler_h2(t *testing.T) {
  1168  	testInterruptWithPanic(t, h2Mode, ErrAbortHandler)
  1169  }
  1170  func testInterruptWithPanic(t *testing.T, h2 bool, panicValue interface{}) {
  1171  	setParallel(t)
  1172  	const msg = "hello"
  1173  	defer afterTest(t)
  1174  
  1175  	testDone := make(chan struct{})
  1176  	defer close(testDone)
  1177  
  1178  	var errorLog lockedBytesBuffer
  1179  	gotHeaders := make(chan bool, 1)
  1180  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
  1181  		io.WriteString(w, msg)
  1182  		w.(Flusher).Flush()
  1183  
  1184  		select {
  1185  		case <-gotHeaders:
  1186  		case <-testDone:
  1187  		}
  1188  		panic(panicValue)
  1189  	}), func(ts *httptest.Server) {
  1190  		ts.Config.ErrorLog = log.New(&errorLog, "", 0)
  1191  	})
  1192  	defer cst.close()
  1193  	res, err := cst.c.Get(cst.ts.URL)
  1194  	if err != nil {
  1195  		t.Fatal(err)
  1196  	}
  1197  	gotHeaders <- true
  1198  	defer res.Body.Close()
  1199  	slurp, err := ioutil.ReadAll(res.Body)
  1200  	if string(slurp) != msg {
  1201  		t.Errorf("client read %q; want %q", slurp, msg)
  1202  	}
  1203  	if err == nil {
  1204  		t.Errorf("client read all successfully; want some error")
  1205  	}
  1206  	logOutput := func() string {
  1207  		errorLog.Lock()
  1208  		defer errorLog.Unlock()
  1209  		return errorLog.String()
  1210  	}
  1211  	wantStackLogged := panicValue != nil && panicValue != ErrAbortHandler
  1212  
  1213  	if err := waitErrCondition(5*time.Second, 10*time.Millisecond, func() error {
  1214  		gotLog := logOutput()
  1215  		if !wantStackLogged {
  1216  			if gotLog == "" {
  1217  				return nil
  1218  			}
  1219  			return fmt.Errorf("want no log output; got: %s", gotLog)
  1220  		}
  1221  		if gotLog == "" {
  1222  			return fmt.Errorf("wanted a stack trace logged; got nothing")
  1223  		}
  1224  		if !strings.Contains(gotLog, "created by ") && strings.Count(gotLog, "\n") < 6 {
  1225  			return fmt.Errorf("output doesn't look like a panic stack trace. Got: %s", gotLog)
  1226  		}
  1227  		return nil
  1228  	}); err != nil {
  1229  		t.Fatal(err)
  1230  	}
  1231  }
  1232  
  1233  type lockedBytesBuffer struct {
  1234  	sync.Mutex
  1235  	bytes.Buffer
  1236  }
  1237  
  1238  func (b *lockedBytesBuffer) Write(p []byte) (int, error) {
  1239  	b.Lock()
  1240  	defer b.Unlock()
  1241  	return b.Buffer.Write(p)
  1242  }
  1243  
  1244  // Issue 15366
  1245  func TestH12_AutoGzipWithDumpResponse(t *testing.T) {
  1246  	h12Compare{
  1247  		Handler: func(w ResponseWriter, r *Request) {
  1248  			h := w.Header()
  1249  			h.Set("Content-Encoding", "gzip")
  1250  			h.Set("Content-Length", "23")
  1251  			io.WriteString(w, "\x1f\x8b\b\x00\x00\x00\x00\x00\x00\x00s\xf3\xf7\a\x00\xab'\xd4\x1a\x03\x00\x00\x00")
  1252  		},
  1253  		EarlyCheckResponse: func(proto string, res *Response) {
  1254  			if !res.Uncompressed {
  1255  				t.Errorf("%s: expected Uncompressed to be set", proto)
  1256  			}
  1257  			dump, err := httputil.DumpResponse(res, true)
  1258  			if err != nil {
  1259  				t.Errorf("%s: DumpResponse: %v", proto, err)
  1260  				return
  1261  			}
  1262  			if strings.Contains(string(dump), "Connection: close") {
  1263  				t.Errorf("%s: should not see \"Connection: close\" in dump; got:\n%s", proto, dump)
  1264  			}
  1265  			if !strings.Contains(string(dump), "FOO") {
  1266  				t.Errorf("%s: should see \"FOO\" in response; got:\n%s", proto, dump)
  1267  			}
  1268  		},
  1269  	}.run(t)
  1270  }
  1271  
  1272  // Issue 14607
  1273  func TestCloseIdleConnections_h1(t *testing.T) { testCloseIdleConnections(t, h1Mode) }
  1274  func TestCloseIdleConnections_h2(t *testing.T) { testCloseIdleConnections(t, h2Mode) }
  1275  func testCloseIdleConnections(t *testing.T, h2 bool) {
  1276  	setParallel(t)
  1277  	defer afterTest(t)
  1278  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
  1279  		w.Header().Set("X-Addr", r.RemoteAddr)
  1280  	}))
  1281  	defer cst.close()
  1282  	get := func() string {
  1283  		res, err := cst.c.Get(cst.ts.URL)
  1284  		if err != nil {
  1285  			t.Fatal(err)
  1286  		}
  1287  		res.Body.Close()
  1288  		v := res.Header.Get("X-Addr")
  1289  		if v == "" {
  1290  			t.Fatal("didn't get X-Addr")
  1291  		}
  1292  		return v
  1293  	}
  1294  	a1 := get()
  1295  	cst.tr.CloseIdleConnections()
  1296  	a2 := get()
  1297  	if a1 == a2 {
  1298  		t.Errorf("didn't close connection")
  1299  	}
  1300  }
  1301  
  1302  type noteCloseConn struct {
  1303  	net.Conn
  1304  	closeFunc func()
  1305  }
  1306  
  1307  func (x noteCloseConn) Close() error {
  1308  	x.closeFunc()
  1309  	return x.Conn.Close()
  1310  }
  1311  
  1312  type testErrorReader struct{ t *testing.T }
  1313  
  1314  func (r testErrorReader) Read(p []byte) (n int, err error) {
  1315  	r.t.Error("unexpected Read call")
  1316  	return 0, io.EOF
  1317  }
  1318  
  1319  func TestNoSniffExpectRequestBody_h1(t *testing.T) { testNoSniffExpectRequestBody(t, h1Mode) }
  1320  func TestNoSniffExpectRequestBody_h2(t *testing.T) { testNoSniffExpectRequestBody(t, h2Mode) }
  1321  
  1322  func testNoSniffExpectRequestBody(t *testing.T, h2 bool) {
  1323  	defer afterTest(t)
  1324  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
  1325  		w.WriteHeader(StatusUnauthorized)
  1326  	}))
  1327  	defer cst.close()
  1328  
  1329  	// Set ExpectContinueTimeout non-zero so RoundTrip won't try to write it.
  1330  	cst.tr.ExpectContinueTimeout = 10 * time.Second
  1331  
  1332  	req, err := NewRequest("POST", cst.ts.URL, testErrorReader{t})
  1333  	if err != nil {
  1334  		t.Fatal(err)
  1335  	}
  1336  	req.ContentLength = 0 // so transport is tempted to sniff it
  1337  	req.Header.Set("Expect", "100-continue")
  1338  	res, err := cst.tr.RoundTrip(req)
  1339  	if err != nil {
  1340  		t.Fatal(err)
  1341  	}
  1342  	defer res.Body.Close()
  1343  	if res.StatusCode != StatusUnauthorized {
  1344  		t.Errorf("status code = %v; want %v", res.StatusCode, StatusUnauthorized)
  1345  	}
  1346  }
  1347  
  1348  func TestServerUndeclaredTrailers_h1(t *testing.T) { testServerUndeclaredTrailers(t, h1Mode) }
  1349  func TestServerUndeclaredTrailers_h2(t *testing.T) { testServerUndeclaredTrailers(t, h2Mode) }
  1350  func testServerUndeclaredTrailers(t *testing.T, h2 bool) {
  1351  	defer afterTest(t)
  1352  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
  1353  		w.Header().Set("Foo", "Bar")
  1354  		w.Header().Set("Trailer:Foo", "Baz")
  1355  		w.(Flusher).Flush()
  1356  		w.Header().Add("Trailer:Foo", "Baz2")
  1357  		w.Header().Set("Trailer:Bar", "Quux")
  1358  	}))
  1359  	defer cst.close()
  1360  	res, err := cst.c.Get(cst.ts.URL)
  1361  	if err != nil {
  1362  		t.Fatal(err)
  1363  	}
  1364  	if _, err := io.Copy(ioutil.Discard, res.Body); err != nil {
  1365  		t.Fatal(err)
  1366  	}
  1367  	res.Body.Close()
  1368  	delete(res.Header, "Date")
  1369  	delete(res.Header, "Content-Type")
  1370  
  1371  	if want := (Header{"Foo": {"Bar"}}); !reflect.DeepEqual(res.Header, want) {
  1372  		t.Errorf("Header = %#v; want %#v", res.Header, want)
  1373  	}
  1374  	if want := (Header{"Foo": {"Baz", "Baz2"}, "Bar": {"Quux"}}); !reflect.DeepEqual(res.Trailer, want) {
  1375  		t.Errorf("Trailer = %#v; want %#v", res.Trailer, want)
  1376  	}
  1377  }
  1378  
  1379  func TestBadResponseAfterReadingBody(t *testing.T) {
  1380  	defer afterTest(t)
  1381  	cst := newClientServerTest(t, false, HandlerFunc(func(w ResponseWriter, r *Request) {
  1382  		_, err := io.Copy(ioutil.Discard, r.Body)
  1383  		if err != nil {
  1384  			t.Fatal(err)
  1385  		}
  1386  		c, _, err := w.(Hijacker).Hijack()
  1387  		if err != nil {
  1388  			t.Fatal(err)
  1389  		}
  1390  		defer c.Close()
  1391  		fmt.Fprintln(c, "some bogus crap")
  1392  	}))
  1393  	defer cst.close()
  1394  
  1395  	closes := 0
  1396  	res, err := cst.c.Post(cst.ts.URL, "text/plain", countCloseReader{&closes, strings.NewReader("hello")})
  1397  	if err == nil {
  1398  		res.Body.Close()
  1399  		t.Fatal("expected an error to be returned from Post")
  1400  	}
  1401  	if closes != 1 {
  1402  		t.Errorf("closes = %d; want 1", closes)
  1403  	}
  1404  }
  1405  
  1406  func TestWriteHeader0_h1(t *testing.T) { testWriteHeader0(t, h1Mode) }
  1407  func TestWriteHeader0_h2(t *testing.T) { testWriteHeader0(t, h2Mode) }
  1408  func testWriteHeader0(t *testing.T, h2 bool) {
  1409  	defer afterTest(t)
  1410  	gotpanic := make(chan bool, 1)
  1411  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
  1412  		defer close(gotpanic)
  1413  		defer func() {
  1414  			if e := recover(); e != nil {
  1415  				got := fmt.Sprintf("%T, %v", e, e)
  1416  				want := "string, invalid WriteHeader code 0"
  1417  				if got != want {
  1418  					t.Errorf("unexpected panic value:\n got: %v\nwant: %v\n", got, want)
  1419  				}
  1420  				gotpanic <- true
  1421  
  1422  				// Set an explicit 503. This also tests that the WriteHeader call panics
  1423  				// before it recorded that an explicit value was set and that bogus
  1424  				// value wasn't stuck.
  1425  				w.WriteHeader(503)
  1426  			}
  1427  		}()
  1428  		w.WriteHeader(0)
  1429  	}))
  1430  	defer cst.close()
  1431  	res, err := cst.c.Get(cst.ts.URL)
  1432  	if err != nil {
  1433  		t.Fatal(err)
  1434  	}
  1435  	if res.StatusCode != 503 {
  1436  		t.Errorf("Response: %v %q; want 503", res.StatusCode, res.Status)
  1437  	}
  1438  	if !<-gotpanic {
  1439  		t.Error("expected panic in handler")
  1440  	}
  1441  }
  1442  
  1443  // Issue 23010: don't be super strict checking WriteHeader's code if
  1444  // it's not even valid to call WriteHeader then anyway.
  1445  func TestWriteHeaderNoCodeCheck_h1(t *testing.T)       { testWriteHeaderAfterWrite(t, h1Mode, false) }
  1446  func TestWriteHeaderNoCodeCheck_h1hijack(t *testing.T) { testWriteHeaderAfterWrite(t, h1Mode, true) }
  1447  func TestWriteHeaderNoCodeCheck_h2(t *testing.T)       { testWriteHeaderAfterWrite(t, h2Mode, false) }
  1448  func testWriteHeaderAfterWrite(t *testing.T, h2, hijack bool) {
  1449  	setParallel(t)
  1450  	defer afterTest(t)
  1451  
  1452  	var errorLog lockedBytesBuffer
  1453  	cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) {
  1454  		if hijack {
  1455  			conn, _, _ := w.(Hijacker).Hijack()
  1456  			defer conn.Close()
  1457  			conn.Write([]byte("HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nfoo"))
  1458  			w.WriteHeader(0) // verify this doesn't panic if there's already output; Issue 23010
  1459  			conn.Write([]byte("bar"))
  1460  			return
  1461  		}
  1462  		io.WriteString(w, "foo")
  1463  		w.(Flusher).Flush()
  1464  		w.WriteHeader(0) // verify this doesn't panic if there's already output; Issue 23010
  1465  		io.WriteString(w, "bar")
  1466  	}), func(ts *httptest.Server) {
  1467  		ts.Config.ErrorLog = log.New(&errorLog, "", 0)
  1468  	})
  1469  	defer cst.close()
  1470  	res, err := cst.c.Get(cst.ts.URL)
  1471  	if err != nil {
  1472  		t.Fatal(err)
  1473  	}
  1474  	defer res.Body.Close()
  1475  	body, err := ioutil.ReadAll(res.Body)
  1476  	if err != nil {
  1477  		t.Fatal(err)
  1478  	}
  1479  	if got, want := string(body), "foobar"; got != want {
  1480  		t.Errorf("got = %q; want %q", got, want)
  1481  	}
  1482  
  1483  	// Also check the stderr output:
  1484  	if h2 {
  1485  		// TODO: also emit this log message for HTTP/2?
  1486  		// We historically haven't, so don't check.
  1487  		return
  1488  	}
  1489  	gotLog := strings.TrimSpace(errorLog.String())
  1490  	wantLog := "http: superfluous response.WriteHeader call from http_test.testWriteHeaderAfterWrite..func1 (clientserver_test.go:"
  1491  	if hijack {
  1492  		wantLog = "http: response.WriteHeader on hijacked connection from http_test.testWriteHeaderAfterWrite..func1 (clientserver_test.go:"
  1493  	}
  1494  	if !strings.HasPrefix(gotLog, wantLog) {
  1495  		t.Errorf("stderr output = %q; want %q", gotLog, wantLog)
  1496  	}
  1497  }
  1498  
  1499  func TestBidiStreamReverseProxy(t *testing.T) {
  1500  	setParallel(t)
  1501  	defer afterTest(t)
  1502  	backend := newClientServerTest(t, h2Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
  1503  		if _, err := io.Copy(w, r.Body); err != nil {
  1504  			log.Printf("bidi backend copy: %v", err)
  1505  		}
  1506  	}))
  1507  	defer backend.close()
  1508  
  1509  	backURL, err := url.Parse(backend.ts.URL)
  1510  	if err != nil {
  1511  		t.Fatal(err)
  1512  	}
  1513  	rp := httputil.NewSingleHostReverseProxy(backURL)
  1514  	rp.Transport = backend.tr
  1515  	proxy := newClientServerTest(t, h2Mode, HandlerFunc(func(w ResponseWriter, r *Request) {
  1516  		rp.ServeHTTP(w, r)
  1517  	}))
  1518  	defer proxy.close()
  1519  
  1520  	bodyRes := make(chan interface{}, 1) // error or hash.Hash
  1521  	pr, pw := io.Pipe()
  1522  	req, _ := NewRequest("PUT", proxy.ts.URL, pr)
  1523  	const size = 4 << 20
  1524  	go func() {
  1525  		h := sha1.New()
  1526  		_, err := io.CopyN(io.MultiWriter(h, pw), rand.Reader, size)
  1527  		go pw.Close()
  1528  		if err != nil {
  1529  			bodyRes <- err
  1530  		} else {
  1531  			bodyRes <- h
  1532  		}
  1533  	}()
  1534  	res, err := backend.c.Do(req)
  1535  	if err != nil {
  1536  		t.Fatal(err)
  1537  	}
  1538  	defer res.Body.Close()
  1539  	hgot := sha1.New()
  1540  	n, err := io.Copy(hgot, res.Body)
  1541  	if err != nil {
  1542  		t.Fatal(err)
  1543  	}
  1544  	if n != size {
  1545  		t.Fatalf("got %d bytes; want %d", n, size)
  1546  	}
  1547  	select {
  1548  	case v := <-bodyRes:
  1549  		switch v := v.(type) {
  1550  		default:
  1551  			t.Fatalf("body copy: %v", err)
  1552  		case hash.Hash:
  1553  			if !bytes.Equal(v.Sum(nil), hgot.Sum(nil)) {
  1554  				t.Errorf("written bytes didn't match received bytes")
  1555  			}
  1556  		}
  1557  	case <-time.After(10 * time.Second):
  1558  		t.Fatal("timeout")
  1559  	}
  1560  
  1561  }
  1562  
  1563  // Always use HTTP/1.1 for WebSocket upgrades.
  1564  func TestH12_WebSocketUpgrade(t *testing.T) {
  1565  	h12Compare{
  1566  		Handler: func(w ResponseWriter, r *Request) {
  1567  			h := w.Header()
  1568  			h.Set("Foo", "bar")
  1569  		},
  1570  		ReqFunc: func(c *Client, url string) (*Response, error) {
  1571  			req, _ := NewRequest("GET", url, nil)
  1572  			req.Header.Set("Connection", "Upgrade")
  1573  			req.Header.Set("Upgrade", "WebSocket")
  1574  			return c.Do(req)
  1575  		},
  1576  		EarlyCheckResponse: func(proto string, res *Response) {
  1577  			if res.Proto != "HTTP/1.1" {
  1578  				t.Errorf("%s: expected HTTP/1.1, got %q", proto, res.Proto)
  1579  			}
  1580  			res.Proto = "HTTP/IGNORE" // skip later checks that Proto must be 1.1 vs 2.0
  1581  		},
  1582  	}.run(t)
  1583  }