From 4bfc56e71660ad9624ac5eb594b3afd0d221c99d Mon Sep 17 00:00:00 2001 From: Luke Young Date: Sat, 19 Oct 2019 22:53:15 -0700 Subject: [PATCH] net/http: make Transport fail tests if request body is not closed when using invalid method --- src/net/http/transport_test.go | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/src/net/http/transport_test.go b/src/net/http/transport_test.go index d7eef0d94c..0fe1283d97 100644 --- a/src/net/http/transport_test.go +++ b/src/net/http/transport_test.go @@ -5719,3 +5719,32 @@ func TestInvalidHeaderResponse(t *testing.T) { t.Errorf(`bad "Foo " header value: %q, want %q`, v, "bar") } } + +type bodyCloser bool + +func (bc *bodyCloser) Close() error { + *bc = true + return nil +} +func (bc *bodyCloser) Read(b []byte) (n int, err error) { + return 0, io.EOF +} + +func TestInvalidMethodClosesBody(t *testing.T) { + cst := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {})) + defer cst.Close() + var bc bodyCloser + u, _ := url.Parse(cst.URL) + req := &Request{ + Method: " ", + URL: u, + Body: &bc, + } + _, err := DefaultClient.Do(req) + if err == nil { + t.Fatal("Expected an error") + } + if !bc { + t.Fatal("Expected body to have been closed") + } +}