github.com/x04/go/src@v0.0.0-20200202162449-3d481ceb3525/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 "github.com/x04/go/src/bytes" 11 "github.com/x04/go/src/compress/gzip" 12 "github.com/x04/go/src/crypto/rand" 13 "github.com/x04/go/src/crypto/sha1" 14 "github.com/x04/go/src/crypto/tls" 15 "github.com/x04/go/src/fmt" 16 "github.com/x04/go/src/hash" 17 "github.com/x04/go/src/io" 18 "github.com/x04/go/src/io/ioutil" 19 "github.com/x04/go/src/log" 20 "github.com/x04/go/src/net" 21 . "github.com/x04/go/src/net/http" 22 "github.com/x04/go/src/net/http/httptest" 23 "github.com/x04/go/src/net/http/httputil" 24 "github.com/x04/go/src/net/url" 25 "github.com/x04/go/src/os" 26 "github.com/x04/go/src/reflect" 27 "github.com/x04/go/src/runtime" 28 "github.com/x04/go/src/sort" 29 "github.com/x04/go/src/strings" 30 "github.com/x04/go/src/sync" 31 "github.com/x04/go/src/sync/atomic" 32 "github.com/x04/go/src/testing" 33 "github.com/x04/go/src/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 setParallel(t) 1059 defer afterTest(t) 1060 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 1061 ioutil.ReadAll(r.Body) 1062 if body { 1063 io.WriteString(w, "Hello.") 1064 } 1065 })) 1066 defer cst.close() 1067 1068 didGC := make(chan struct{}) 1069 (func() { 1070 body := strings.NewReader("some body") 1071 req, _ := NewRequest("POST", cst.ts.URL, body) 1072 runtime.SetFinalizer(req, func(*Request) { close(didGC) }) 1073 res, err := cst.c.Do(req) 1074 if err != nil { 1075 t.Fatal(err) 1076 } 1077 if _, err := ioutil.ReadAll(res.Body); err != nil { 1078 t.Fatal(err) 1079 } 1080 if err := res.Body.Close(); err != nil { 1081 t.Fatal(err) 1082 } 1083 })() 1084 timeout := time.NewTimer(5 * time.Second) 1085 defer timeout.Stop() 1086 for { 1087 select { 1088 case <-didGC: 1089 return 1090 case <-time.After(100 * time.Millisecond): 1091 runtime.GC() 1092 case <-timeout.C: 1093 t.Fatal("never saw GC of request") 1094 } 1095 } 1096 } 1097 1098 func TestTransportRejectsInvalidHeaders_h1(t *testing.T) { 1099 testTransportRejectsInvalidHeaders(t, h1Mode) 1100 } 1101 func TestTransportRejectsInvalidHeaders_h2(t *testing.T) { 1102 testTransportRejectsInvalidHeaders(t, h2Mode) 1103 } 1104 func testTransportRejectsInvalidHeaders(t *testing.T, h2 bool) { 1105 setParallel(t) 1106 defer afterTest(t) 1107 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 1108 fmt.Fprintf(w, "Handler saw headers: %q", r.Header) 1109 }), optQuietLog) 1110 defer cst.close() 1111 cst.tr.DisableKeepAlives = true 1112 1113 tests := []struct { 1114 key, val string 1115 ok bool 1116 }{ 1117 {"Foo", "capital-key", true}, // verify h2 allows capital keys 1118 {"Foo", "foo\x00bar", false}, // \x00 byte in value not allowed 1119 {"Foo", "two\nlines", false}, // \n byte in value not allowed 1120 {"bogus\nkey", "v", false}, // \n byte also not allowed in key 1121 {"A space", "v", false}, // spaces in keys not allowed 1122 {"имя", "v", false}, // key must be ascii 1123 {"name", "валю", true}, // value may be non-ascii 1124 {"", "v", false}, // key must be non-empty 1125 {"k", "", true}, // value may be empty 1126 } 1127 for _, tt := range tests { 1128 dialedc := make(chan bool, 1) 1129 cst.tr.Dial = func(netw, addr string) (net.Conn, error) { 1130 dialedc <- true 1131 return net.Dial(netw, addr) 1132 } 1133 req, _ := NewRequest("GET", cst.ts.URL, nil) 1134 req.Header[tt.key] = []string{tt.val} 1135 res, err := cst.c.Do(req) 1136 var body []byte 1137 if err == nil { 1138 body, _ = ioutil.ReadAll(res.Body) 1139 res.Body.Close() 1140 } 1141 var dialed bool 1142 select { 1143 case <-dialedc: 1144 dialed = true 1145 default: 1146 } 1147 1148 if !tt.ok && dialed { 1149 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) 1150 } else if (err == nil) != tt.ok { 1151 t.Errorf("For key %q, value %q; got err = %v; want ok=%v", tt.key, tt.val, err, tt.ok) 1152 } 1153 } 1154 } 1155 1156 func TestInterruptWithPanic_h1(t *testing.T) { testInterruptWithPanic(t, h1Mode, "boom") } 1157 func TestInterruptWithPanic_h2(t *testing.T) { testInterruptWithPanic(t, h2Mode, "boom") } 1158 func TestInterruptWithPanic_nil_h1(t *testing.T) { testInterruptWithPanic(t, h1Mode, nil) } 1159 func TestInterruptWithPanic_nil_h2(t *testing.T) { testInterruptWithPanic(t, h2Mode, nil) } 1160 func TestInterruptWithPanic_ErrAbortHandler_h1(t *testing.T) { 1161 testInterruptWithPanic(t, h1Mode, ErrAbortHandler) 1162 } 1163 func TestInterruptWithPanic_ErrAbortHandler_h2(t *testing.T) { 1164 testInterruptWithPanic(t, h2Mode, ErrAbortHandler) 1165 } 1166 func testInterruptWithPanic(t *testing.T, h2 bool, panicValue interface{}) { 1167 setParallel(t) 1168 const msg = "hello" 1169 defer afterTest(t) 1170 1171 testDone := make(chan struct{}) 1172 defer close(testDone) 1173 1174 var errorLog lockedBytesBuffer 1175 gotHeaders := make(chan bool, 1) 1176 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 1177 io.WriteString(w, msg) 1178 w.(Flusher).Flush() 1179 1180 select { 1181 case <-gotHeaders: 1182 case <-testDone: 1183 } 1184 panic(panicValue) 1185 }), func(ts *httptest.Server) { 1186 ts.Config.ErrorLog = log.New(&errorLog, "", 0) 1187 }) 1188 defer cst.close() 1189 res, err := cst.c.Get(cst.ts.URL) 1190 if err != nil { 1191 t.Fatal(err) 1192 } 1193 gotHeaders <- true 1194 defer res.Body.Close() 1195 slurp, err := ioutil.ReadAll(res.Body) 1196 if string(slurp) != msg { 1197 t.Errorf("client read %q; want %q", slurp, msg) 1198 } 1199 if err == nil { 1200 t.Errorf("client read all successfully; want some error") 1201 } 1202 logOutput := func() string { 1203 errorLog.Lock() 1204 defer errorLog.Unlock() 1205 return errorLog.String() 1206 } 1207 wantStackLogged := panicValue != nil && panicValue != ErrAbortHandler 1208 1209 if err := waitErrCondition(5*time.Second, 10*time.Millisecond, func() error { 1210 gotLog := logOutput() 1211 if !wantStackLogged { 1212 if gotLog == "" { 1213 return nil 1214 } 1215 return fmt.Errorf("want no log output; got: %s", gotLog) 1216 } 1217 if gotLog == "" { 1218 return fmt.Errorf("wanted a stack trace logged; got nothing") 1219 } 1220 if !strings.Contains(gotLog, "created by ") && strings.Count(gotLog, "\n") < 6 { 1221 return fmt.Errorf("output doesn't look like a panic stack trace. Got: %s", gotLog) 1222 } 1223 return nil 1224 }); err != nil { 1225 t.Fatal(err) 1226 } 1227 } 1228 1229 type lockedBytesBuffer struct { 1230 sync.Mutex 1231 bytes.Buffer 1232 } 1233 1234 func (b *lockedBytesBuffer) Write(p []byte) (int, error) { 1235 b.Lock() 1236 defer b.Unlock() 1237 return b.Buffer.Write(p) 1238 } 1239 1240 // Issue 15366 1241 func TestH12_AutoGzipWithDumpResponse(t *testing.T) { 1242 h12Compare{ 1243 Handler: func(w ResponseWriter, r *Request) { 1244 h := w.Header() 1245 h.Set("Content-Encoding", "gzip") 1246 h.Set("Content-Length", "23") 1247 io.WriteString(w, "\x1f\x8b\b\x00\x00\x00\x00\x00\x00\x00s\xf3\xf7\a\x00\xab'\xd4\x1a\x03\x00\x00\x00") 1248 }, 1249 EarlyCheckResponse: func(proto string, res *Response) { 1250 if !res.Uncompressed { 1251 t.Errorf("%s: expected Uncompressed to be set", proto) 1252 } 1253 dump, err := httputil.DumpResponse(res, true) 1254 if err != nil { 1255 t.Errorf("%s: DumpResponse: %v", proto, err) 1256 return 1257 } 1258 if strings.Contains(string(dump), "Connection: close") { 1259 t.Errorf("%s: should not see \"Connection: close\" in dump; got:\n%s", proto, dump) 1260 } 1261 if !strings.Contains(string(dump), "FOO") { 1262 t.Errorf("%s: should see \"FOO\" in response; got:\n%s", proto, dump) 1263 } 1264 }, 1265 }.run(t) 1266 } 1267 1268 // Issue 14607 1269 func TestCloseIdleConnections_h1(t *testing.T) { testCloseIdleConnections(t, h1Mode) } 1270 func TestCloseIdleConnections_h2(t *testing.T) { testCloseIdleConnections(t, h2Mode) } 1271 func testCloseIdleConnections(t *testing.T, h2 bool) { 1272 setParallel(t) 1273 defer afterTest(t) 1274 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 1275 w.Header().Set("X-Addr", r.RemoteAddr) 1276 })) 1277 defer cst.close() 1278 get := func() string { 1279 res, err := cst.c.Get(cst.ts.URL) 1280 if err != nil { 1281 t.Fatal(err) 1282 } 1283 res.Body.Close() 1284 v := res.Header.Get("X-Addr") 1285 if v == "" { 1286 t.Fatal("didn't get X-Addr") 1287 } 1288 return v 1289 } 1290 a1 := get() 1291 cst.tr.CloseIdleConnections() 1292 a2 := get() 1293 if a1 == a2 { 1294 t.Errorf("didn't close connection") 1295 } 1296 } 1297 1298 type noteCloseConn struct { 1299 net.Conn 1300 closeFunc func() 1301 } 1302 1303 func (x noteCloseConn) Close() error { 1304 x.closeFunc() 1305 return x.Conn.Close() 1306 } 1307 1308 type testErrorReader struct{ t *testing.T } 1309 1310 func (r testErrorReader) Read(p []byte) (n int, err error) { 1311 r.t.Error("unexpected Read call") 1312 return 0, io.EOF 1313 } 1314 1315 func TestNoSniffExpectRequestBody_h1(t *testing.T) { testNoSniffExpectRequestBody(t, h1Mode) } 1316 func TestNoSniffExpectRequestBody_h2(t *testing.T) { testNoSniffExpectRequestBody(t, h2Mode) } 1317 1318 func testNoSniffExpectRequestBody(t *testing.T, h2 bool) { 1319 defer afterTest(t) 1320 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 1321 w.WriteHeader(StatusUnauthorized) 1322 })) 1323 defer cst.close() 1324 1325 // Set ExpectContinueTimeout non-zero so RoundTrip won't try to write it. 1326 cst.tr.ExpectContinueTimeout = 10 * time.Second 1327 1328 req, err := NewRequest("POST", cst.ts.URL, testErrorReader{t}) 1329 if err != nil { 1330 t.Fatal(err) 1331 } 1332 req.ContentLength = 0 // so transport is tempted to sniff it 1333 req.Header.Set("Expect", "100-continue") 1334 res, err := cst.tr.RoundTrip(req) 1335 if err != nil { 1336 t.Fatal(err) 1337 } 1338 defer res.Body.Close() 1339 if res.StatusCode != StatusUnauthorized { 1340 t.Errorf("status code = %v; want %v", res.StatusCode, StatusUnauthorized) 1341 } 1342 } 1343 1344 func TestServerUndeclaredTrailers_h1(t *testing.T) { testServerUndeclaredTrailers(t, h1Mode) } 1345 func TestServerUndeclaredTrailers_h2(t *testing.T) { testServerUndeclaredTrailers(t, h2Mode) } 1346 func testServerUndeclaredTrailers(t *testing.T, h2 bool) { 1347 defer afterTest(t) 1348 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 1349 w.Header().Set("Foo", "Bar") 1350 w.Header().Set("Trailer:Foo", "Baz") 1351 w.(Flusher).Flush() 1352 w.Header().Add("Trailer:Foo", "Baz2") 1353 w.Header().Set("Trailer:Bar", "Quux") 1354 })) 1355 defer cst.close() 1356 res, err := cst.c.Get(cst.ts.URL) 1357 if err != nil { 1358 t.Fatal(err) 1359 } 1360 if _, err := io.Copy(ioutil.Discard, res.Body); err != nil { 1361 t.Fatal(err) 1362 } 1363 res.Body.Close() 1364 delete(res.Header, "Date") 1365 delete(res.Header, "Content-Type") 1366 1367 if want := (Header{"Foo": {"Bar"}}); !reflect.DeepEqual(res.Header, want) { 1368 t.Errorf("Header = %#v; want %#v", res.Header, want) 1369 } 1370 if want := (Header{"Foo": {"Baz", "Baz2"}, "Bar": {"Quux"}}); !reflect.DeepEqual(res.Trailer, want) { 1371 t.Errorf("Trailer = %#v; want %#v", res.Trailer, want) 1372 } 1373 } 1374 1375 func TestBadResponseAfterReadingBody(t *testing.T) { 1376 defer afterTest(t) 1377 cst := newClientServerTest(t, false, HandlerFunc(func(w ResponseWriter, r *Request) { 1378 _, err := io.Copy(ioutil.Discard, r.Body) 1379 if err != nil { 1380 t.Fatal(err) 1381 } 1382 c, _, err := w.(Hijacker).Hijack() 1383 if err != nil { 1384 t.Fatal(err) 1385 } 1386 defer c.Close() 1387 fmt.Fprintln(c, "some bogus crap") 1388 })) 1389 defer cst.close() 1390 1391 closes := 0 1392 res, err := cst.c.Post(cst.ts.URL, "text/plain", countCloseReader{&closes, strings.NewReader("hello")}) 1393 if err == nil { 1394 res.Body.Close() 1395 t.Fatal("expected an error to be returned from Post") 1396 } 1397 if closes != 1 { 1398 t.Errorf("closes = %d; want 1", closes) 1399 } 1400 } 1401 1402 func TestWriteHeader0_h1(t *testing.T) { testWriteHeader0(t, h1Mode) } 1403 func TestWriteHeader0_h2(t *testing.T) { testWriteHeader0(t, h2Mode) } 1404 func testWriteHeader0(t *testing.T, h2 bool) { 1405 defer afterTest(t) 1406 gotpanic := make(chan bool, 1) 1407 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 1408 defer close(gotpanic) 1409 defer func() { 1410 if e := recover(); e != nil { 1411 got := fmt.Sprintf("%T, %v", e, e) 1412 want := "string, invalid WriteHeader code 0" 1413 if got != want { 1414 t.Errorf("unexpected panic value:\n got: %v\nwant: %v\n", got, want) 1415 } 1416 gotpanic <- true 1417 1418 // Set an explicit 503. This also tests that the WriteHeader call panics 1419 // before it recorded that an explicit value was set and that bogus 1420 // value wasn't stuck. 1421 w.WriteHeader(503) 1422 } 1423 }() 1424 w.WriteHeader(0) 1425 })) 1426 defer cst.close() 1427 res, err := cst.c.Get(cst.ts.URL) 1428 if err != nil { 1429 t.Fatal(err) 1430 } 1431 if res.StatusCode != 503 { 1432 t.Errorf("Response: %v %q; want 503", res.StatusCode, res.Status) 1433 } 1434 if !<-gotpanic { 1435 t.Error("expected panic in handler") 1436 } 1437 } 1438 1439 // Issue 23010: don't be super strict checking WriteHeader's code if 1440 // it's not even valid to call WriteHeader then anyway. 1441 func TestWriteHeaderNoCodeCheck_h1(t *testing.T) { testWriteHeaderAfterWrite(t, h1Mode, false) } 1442 func TestWriteHeaderNoCodeCheck_h1hijack(t *testing.T) { testWriteHeaderAfterWrite(t, h1Mode, true) } 1443 func TestWriteHeaderNoCodeCheck_h2(t *testing.T) { testWriteHeaderAfterWrite(t, h2Mode, false) } 1444 func testWriteHeaderAfterWrite(t *testing.T, h2, hijack bool) { 1445 setParallel(t) 1446 defer afterTest(t) 1447 1448 var errorLog lockedBytesBuffer 1449 cst := newClientServerTest(t, h2, HandlerFunc(func(w ResponseWriter, r *Request) { 1450 if hijack { 1451 conn, _, _ := w.(Hijacker).Hijack() 1452 defer conn.Close() 1453 conn.Write([]byte("HTTP/1.1 200 OK\r\nContent-Length: 6\r\n\r\nfoo")) 1454 w.WriteHeader(0) // verify this doesn't panic if there's already output; Issue 23010 1455 conn.Write([]byte("bar")) 1456 return 1457 } 1458 io.WriteString(w, "foo") 1459 w.(Flusher).Flush() 1460 w.WriteHeader(0) // verify this doesn't panic if there's already output; Issue 23010 1461 io.WriteString(w, "bar") 1462 }), func(ts *httptest.Server) { 1463 ts.Config.ErrorLog = log.New(&errorLog, "", 0) 1464 }) 1465 defer cst.close() 1466 res, err := cst.c.Get(cst.ts.URL) 1467 if err != nil { 1468 t.Fatal(err) 1469 } 1470 defer res.Body.Close() 1471 body, err := ioutil.ReadAll(res.Body) 1472 if err != nil { 1473 t.Fatal(err) 1474 } 1475 if got, want := string(body), "foobar"; got != want { 1476 t.Errorf("got = %q; want %q", got, want) 1477 } 1478 1479 // Also check the stderr output: 1480 if h2 { 1481 // TODO: also emit this log message for HTTP/2? 1482 // We historically haven't, so don't check. 1483 return 1484 } 1485 gotLog := strings.TrimSpace(errorLog.String()) 1486 wantLog := "http: superfluous response.WriteHeader call from net/http_test.testWriteHeaderAfterWrite.func1 (clientserver_test.go:" 1487 if hijack { 1488 wantLog = "http: response.WriteHeader on hijacked connection from net/http_test.testWriteHeaderAfterWrite.func1 (clientserver_test.go:" 1489 } 1490 if !strings.HasPrefix(gotLog, wantLog) { 1491 t.Errorf("stderr output = %q; want %q", gotLog, wantLog) 1492 } 1493 } 1494 1495 func TestBidiStreamReverseProxy(t *testing.T) { 1496 setParallel(t) 1497 defer afterTest(t) 1498 backend := newClientServerTest(t, h2Mode, HandlerFunc(func(w ResponseWriter, r *Request) { 1499 if _, err := io.Copy(w, r.Body); err != nil { 1500 log.Printf("bidi backend copy: %v", err) 1501 } 1502 })) 1503 defer backend.close() 1504 1505 backURL, err := url.Parse(backend.ts.URL) 1506 if err != nil { 1507 t.Fatal(err) 1508 } 1509 rp := httputil.NewSingleHostReverseProxy(backURL) 1510 rp.Transport = backend.tr 1511 proxy := newClientServerTest(t, h2Mode, HandlerFunc(func(w ResponseWriter, r *Request) { 1512 rp.ServeHTTP(w, r) 1513 })) 1514 defer proxy.close() 1515 1516 bodyRes := make(chan interface{}, 1) // error or hash.Hash 1517 pr, pw := io.Pipe() 1518 req, _ := NewRequest("PUT", proxy.ts.URL, pr) 1519 const size = 4 << 20 1520 go func() { 1521 h := sha1.New() 1522 _, err := io.CopyN(io.MultiWriter(h, pw), rand.Reader, size) 1523 go pw.Close() 1524 if err != nil { 1525 bodyRes <- err 1526 } else { 1527 bodyRes <- h 1528 } 1529 }() 1530 res, err := backend.c.Do(req) 1531 if err != nil { 1532 t.Fatal(err) 1533 } 1534 defer res.Body.Close() 1535 hgot := sha1.New() 1536 n, err := io.Copy(hgot, res.Body) 1537 if err != nil { 1538 t.Fatal(err) 1539 } 1540 if n != size { 1541 t.Fatalf("got %d bytes; want %d", n, size) 1542 } 1543 select { 1544 case v := <-bodyRes: 1545 switch v := v.(type) { 1546 default: 1547 t.Fatalf("body copy: %v", err) 1548 case hash.Hash: 1549 if !bytes.Equal(v.Sum(nil), hgot.Sum(nil)) { 1550 t.Errorf("written bytes didn't match received bytes") 1551 } 1552 } 1553 case <-time.After(10 * time.Second): 1554 t.Fatal("timeout") 1555 } 1556 1557 } 1558 1559 // Always use HTTP/1.1 for WebSocket upgrades. 1560 func TestH12_WebSocketUpgrade(t *testing.T) { 1561 h12Compare{ 1562 Handler: func(w ResponseWriter, r *Request) { 1563 h := w.Header() 1564 h.Set("Foo", "bar") 1565 }, 1566 ReqFunc: func(c *Client, url string) (*Response, error) { 1567 req, _ := NewRequest("GET", url, nil) 1568 req.Header.Set("Connection", "Upgrade") 1569 req.Header.Set("Upgrade", "WebSocket") 1570 return c.Do(req) 1571 }, 1572 EarlyCheckResponse: func(proto string, res *Response) { 1573 if res.Proto != "HTTP/1.1" { 1574 t.Errorf("%s: expected HTTP/1.1, got %q", proto, res.Proto) 1575 } 1576 res.Proto = "HTTP/IGNORE" // skip later checks that Proto must be 1.1 vs 2.0 1577 }, 1578 }.run(t) 1579 }