Skip to content

Commit b68cebd

Browse files
neildgopherbot
authored andcommitted
net/http/httptest: record failed ResponseWriter writes
CL 709335 changed ResponseWriter.Write to return an error when trying to write to a response with a status code which doesn't permit a body, such as 304. Continue to return an error, but still record the write in ResponseWriter.Body. This maintains the documented property that "the data in buf is written to rw.Body". For #75471 Change-Id: I69139797559fe09d6580c5d25b4458f04263c60e Reviewed-on: https://go-review.googlesource.com/c/go/+/711940 Reviewed-by: Sean Liao <sean@liao.dev> TryBot-Bypass: Damien Neil <dneil@google.com> Auto-Submit: Damien Neil <dneil@google.com> Reviewed-by: Nicholas Husin <nsh@golang.org> Reviewed-by: Nicholas Husin <husin@google.com>
1 parent f1fed74 commit b68cebd

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
lines changed

src/net/http/httptest/recorder.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -105,28 +105,28 @@ func (rw *ResponseRecorder) writeHeader(b []byte, str string) {
105105
// Write implements http.ResponseWriter. The data in buf is written to
106106
// rw.Body, if not nil.
107107
func (rw *ResponseRecorder) Write(buf []byte) (int, error) {
108-
code := rw.Code
109-
if !bodyAllowedForStatus(code) {
110-
return 0, http.ErrBodyNotAllowed
111-
}
108+
// Record the write, even if we're going to return an error.
112109
rw.writeHeader(buf, "")
113110
if rw.Body != nil {
114111
rw.Body.Write(buf)
115112
}
113+
if !bodyAllowedForStatus(rw.Code) {
114+
return 0, http.ErrBodyNotAllowed
115+
}
116116
return len(buf), nil
117117
}
118118

119119
// WriteString implements [io.StringWriter]. The data in str is written
120120
// to rw.Body, if not nil.
121121
func (rw *ResponseRecorder) WriteString(str string) (int, error) {
122-
code := rw.Code
123-
if !bodyAllowedForStatus(code) {
124-
return 0, http.ErrBodyNotAllowed
125-
}
122+
// Record the write, even if we're going to return an error.
126123
rw.writeHeader(nil, str)
127124
if rw.Body != nil {
128125
rw.Body.WriteString(str)
129126
}
127+
if !bodyAllowedForStatus(rw.Code) {
128+
return 0, http.ErrBodyNotAllowed
129+
}
130130
return len(str), nil
131131
}
132132

src/net/http/httptest/recorder_test.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
package httptest
66

77
import (
8+
"bytes"
89
"errors"
910
"fmt"
1011
"io"
@@ -312,17 +313,22 @@ func TestRecorder(t *testing.T) {
312313

313314
func TestBodyNotAllowed(t *testing.T) {
314315
rw := NewRecorder()
316+
rw.Body = new(bytes.Buffer)
315317
rw.WriteHeader(204)
316318

317-
_, err := rw.Write([]byte("hello world"))
319+
_, err := rw.Write([]byte("hello "))
318320
if !errors.Is(err, http.ErrBodyNotAllowed) {
319321
t.Errorf("expected BodyNotAllowed for Write after 204, got: %v", err)
320322
}
321323

322-
_, err = rw.WriteString("hello world")
324+
_, err = rw.WriteString("world")
323325
if !errors.Is(err, http.ErrBodyNotAllowed) {
324326
t.Errorf("expected BodyNotAllowed for WriteString after 204, got: %v", err)
325327
}
328+
329+
if got, want := rw.Body.String(), "hello world"; got != want {
330+
t.Errorf("got Body=%q, want %q", got, want)
331+
}
326332
}
327333

328334
// issue 39017 - disallow Content-Length values such as "+3"

0 commit comments

Comments
 (0)