From 6374a6607ba601b8d15514b0ce9462ea50fed0a5 Mon Sep 17 00:00:00 2001 From: Brad Fitzpatrick Date: Tue, 23 May 2017 22:35:15 +0000 Subject: [PATCH] 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 Run-TryBot: Brad Fitzpatrick TryBot-Result: Gobot Gobot --- src/net/http/httptest/recorder.go | 3 ++- src/net/http/httptest/recorder_test.go | 13 +++++++++++-- 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/src/net/http/httptest/recorder.go b/src/net/http/httptest/recorder.go index 5f1aa6af479..741f076b363 100644 --- a/src/net/http/httptest/recorder.go +++ b/src/net/http/httptest/recorder.go @@ -6,6 +6,7 @@ package httptest import ( "bytes" + "fmt" "io/ioutil" "net/http" "strconv" @@ -176,7 +177,7 @@ func (rw *ResponseRecorder) Result() *http.Response { if res.StatusCode == 0 { res.StatusCode = 200 } - res.Status = http.StatusText(res.StatusCode) + res.Status = fmt.Sprintf("%03d %s", res.StatusCode, http.StatusText(res.StatusCode)) if rw.Body != nil { res.Body = ioutil.NopCloser(bytes.NewReader(rw.Body.Bytes())) } diff --git a/src/net/http/httptest/recorder_test.go b/src/net/http/httptest/recorder_test.go index 9afba4e556a..a6259ebac71 100644 --- a/src/net/http/httptest/recorder_test.go +++ b/src/net/http/httptest/recorder_test.go @@ -23,7 +23,15 @@ func TestRecorder(t *testing.T) { return nil } } - hasResultStatus := func(wantCode int) checkFunc { + hasResultStatus := func(want string) checkFunc { + return func(rec *ResponseRecorder) error { + if rec.Result().Status != want { + return fmt.Errorf("Result().Status = %q; want %q", rec.Result().Status, want) + } + return nil + } + } + hasResultStatusCode := func(wantCode int) checkFunc { return func(rec *ResponseRecorder) error { if rec.Result().StatusCode != wantCode { return fmt.Errorf("Result().StatusCode = %d; want %d", rec.Result().StatusCode, wantCode) @@ -235,7 +243,8 @@ func TestRecorder(t *testing.T) { hasOldHeader("X-Foo", "1"), hasStatus(0), hasHeader("X-Foo", "1"), - hasResultStatus(200), + hasResultStatus("200 OK"), + hasResultStatusCode(200), ), }, {