Skip to content

Commit 6374a66

Browse files
committed
net/http/httptest: make ResponseRecorder.Result.Status match http.Transport
Fixes #18438 Change-Id: I9599c1536d5e8bad7662b8ffa19e9b0746e27e60 Reviewed-on: https://go-review.googlesource.com/44000 Reviewed-by: Ian Lance Taylor <[email protected]> Run-TryBot: Brad Fitzpatrick <[email protected]> TryBot-Result: Gobot Gobot <[email protected]>
1 parent a28ce75 commit 6374a66

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/net/http/httptest/recorder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package httptest
66

77
import (
88
"bytes"
9+
"fmt"
910
"io/ioutil"
1011
"net/http"
1112
"strconv"
@@ -176,7 +177,7 @@ func (rw *ResponseRecorder) Result() *http.Response {
176177
if res.StatusCode == 0 {
177178
res.StatusCode = 200
178179
}
179-
res.Status = http.StatusText(res.StatusCode)
180+
res.Status = fmt.Sprintf("%03d %s", res.StatusCode, http.StatusText(res.StatusCode))
180181
if rw.Body != nil {
181182
res.Body = ioutil.NopCloser(bytes.NewReader(rw.Body.Bytes()))
182183
}

src/net/http/httptest/recorder_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@ func TestRecorder(t *testing.T) {
2323
return nil
2424
}
2525
}
26-
hasResultStatus := func(wantCode int) checkFunc {
26+
hasResultStatus := func(want string) checkFunc {
27+
return func(rec *ResponseRecorder) error {
28+
if rec.Result().Status != want {
29+
return fmt.Errorf("Result().Status = %q; want %q", rec.Result().Status, want)
30+
}
31+
return nil
32+
}
33+
}
34+
hasResultStatusCode := func(wantCode int) checkFunc {
2735
return func(rec *ResponseRecorder) error {
2836
if rec.Result().StatusCode != wantCode {
2937
return fmt.Errorf("Result().StatusCode = %d; want %d", rec.Result().StatusCode, wantCode)
@@ -235,7 +243,8 @@ func TestRecorder(t *testing.T) {
235243
hasOldHeader("X-Foo", "1"),
236244
hasStatus(0),
237245
hasHeader("X-Foo", "1"),
238-
hasResultStatus(200),
246+
hasResultStatus("200 OK"),
247+
hasResultStatusCode(200),
239248
),
240249
},
241250
{

0 commit comments

Comments
 (0)