Skip to content

Commit 4bfc56e

Browse files
net/http: make Transport fail tests if request body is not closed when using invalid method
1 parent b1898d2 commit 4bfc56e

File tree

1 file changed

+29
-0
lines changed

1 file changed

+29
-0
lines changed

src/net/http/transport_test.go

+29
Original file line numberDiff line numberDiff line change
@@ -5719,3 +5719,32 @@ func TestInvalidHeaderResponse(t *testing.T) {
57195719
t.Errorf(`bad "Foo " header value: %q, want %q`, v, "bar")
57205720
}
57215721
}
5722+
5723+
type bodyCloser bool
5724+
5725+
func (bc *bodyCloser) Close() error {
5726+
*bc = true
5727+
return nil
5728+
}
5729+
func (bc *bodyCloser) Read(b []byte) (n int, err error) {
5730+
return 0, io.EOF
5731+
}
5732+
5733+
func TestInvalidMethodClosesBody(t *testing.T) {
5734+
cst := httptest.NewServer(HandlerFunc(func(w ResponseWriter, r *Request) {}))
5735+
defer cst.Close()
5736+
var bc bodyCloser
5737+
u, _ := url.Parse(cst.URL)
5738+
req := &Request{
5739+
Method: " ",
5740+
URL: u,
5741+
Body: &bc,
5742+
}
5743+
_, err := DefaultClient.Do(req)
5744+
if err == nil {
5745+
t.Fatal("Expected an error")
5746+
}
5747+
if !bc {
5748+
t.Fatal("Expected body to have been closed")
5749+
}
5750+
}

0 commit comments

Comments
 (0)