Skip to content

Commit bf35244

Browse files
committed
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 is only 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.
1 parent e161b1e commit bf35244

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-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 = ioutil.NopCloser(strings.NewReader(""))
187189
}
188190
res.ContentLength = parseContentLength(res.Header.Get("Content-Length"))
189191

0 commit comments

Comments
 (0)