Skip to content

Commit 59699aa

Browse files
jackxbrittonbradfitz
authored andcommitted
net/http/httptest: guarantee ResponseRecorder.Result returns a non-nil body
The doc for ResponseRecorder.Result guarantees that the body of the returned http.Response will be non-nil, but this only holds true if the caller's body is non-nil. With this change, if the caller's body is nil then the returned response's body will be an empty io.ReadCloser. Fixes #26442 Change-Id: I3b2fe4a2541caf9997dbb8978bbaf1f58cd1f471 GitHub-Last-Rev: d802967 GitHub-Pull-Request: #26453 Reviewed-on: https://go-review.googlesource.com/124875 Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org>
1 parent f4c787b commit 59699aa

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

src/net/http/httptest/recorder.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,8 @@ func (rw *ResponseRecorder) Result() *http.Response {
184184
res.Status = fmt.Sprintf("%03d %s", res.StatusCode, http.StatusText(res.StatusCode))
185185
if rw.Body != nil {
186186
res.Body = ioutil.NopCloser(bytes.NewReader(rw.Body.Bytes()))
187+
} else {
188+
res.Body = http.NoBody
187189
}
188190
res.ContentLength = parseContentLength(res.Header.Get("Content-Length"))
189191

src/net/http/httptest/recorder_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package httptest
77
import (
88
"fmt"
99
"io"
10+
"io/ioutil"
1011
"net/http"
1112
"testing"
1213
)
@@ -39,6 +40,19 @@ func TestRecorder(t *testing.T) {
3940
return nil
4041
}
4142
}
43+
hasResultContents := func(want string) checkFunc {
44+
return func(rec *ResponseRecorder) error {
45+
contentBytes, err := ioutil.ReadAll(rec.Result().Body)
46+
if err != nil {
47+
return err
48+
}
49+
contents := string(contentBytes)
50+
if contents != want {
51+
return fmt.Errorf("Result().Body = %s; want %s", contents, want)
52+
}
53+
return nil
54+
}
55+
}
4256
hasContents := func(want string) checkFunc {
4357
return func(rec *ResponseRecorder) error {
4458
if rec.Body.String() != want {
@@ -273,6 +287,15 @@ func TestRecorder(t *testing.T) {
273287
},
274288
check(hasStatus(200), hasContents("Some body"), hasContentLength(9)),
275289
},
290+
{
291+
"nil ResponseRecorder.Body", // Issue 26642
292+
func(w http.ResponseWriter, r *http.Request) {
293+
w.(*ResponseRecorder).Body = nil
294+
io.WriteString(w, "hi")
295+
},
296+
check(hasResultContents("")), // check we don't crash reading the body
297+
298+
},
276299
} {
277300
t.Run(tt.name, func(t *testing.T) {
278301
r, _ := http.NewRequest("GET", "http://foo.com/", nil)

0 commit comments

Comments
 (0)