1
0
mirror of https://github.com/golang/go synced 2024-11-17 22:14:43 -07:00

net/http: make Transport fail tests if request body is not closed when using invalid method

This commit is contained in:
Luke Young 2019-10-19 22:53:15 -07:00 committed by GitHub
parent b1898d2033
commit 4bfc56e716
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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")
}
}