mirror of
https://github.com/golang/go
synced 2024-11-23 14:40:02 -07:00
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 <iant@golang.org> Run-TryBot: Brad Fitzpatrick <bradfitz@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org>
This commit is contained in:
parent
a28ce75daa
commit
6374a6607b
@ -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()))
|
||||
}
|
||||
|
@ -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),
|
||||
),
|
||||
},
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user