Skip to content

Commit 1ebd59a

Browse files
constantoinemaxatome
authored andcommitted
Make http.Response.Status respect RFC 2616, like in Go stdlib.
Although RFC 2616 has been superseded with RFC 9110, some client implementation still rely on certain behavious regarding the Status-Line. Now, as httpmock currently implements it, per [RFC 9110#6.2-1](https://www.rfc-editor.org/rfc/rfc9110.html#section-6.2-1), the "reason phrase" is actually optional, and you need only to include the status code. Back in the days of [RFC 2616](https://www.rfc-editor.org/rfc/rfc2616.html#section-6.1), per #6.1, the Status-Line had to contain both the status code (eg. 200) and its textual representation (eg. OK). Change code is directly stolen from the Go standard library. Signed-off-by: Cléo Rebert <[email protected]>
1 parent 8705bb3 commit 1ebd59a

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

response.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -525,7 +525,7 @@ func NewNotFoundResponder(fn func(...any)) Responder {
525525
// httpmock.NewStringResponse(200, httpmock.File("body.txt").String())
526526
func NewStringResponse(status int, body string) *http.Response {
527527
return &http.Response{
528-
Status: strconv.Itoa(status),
528+
Status: fmt.Sprintf("%03d %s", status, http.StatusText(status)),
529529
StatusCode: status,
530530
Body: NewRespBodyFromString(body),
531531
Header: http.Header{},
@@ -551,7 +551,7 @@ func NewStringResponder(status int, body string) Responder {
551551
// httpmock.NewBytesResponse(200, httpmock.File("body.raw").Bytes())
552552
func NewBytesResponse(status int, body []byte) *http.Response {
553553
return &http.Response{
554-
Status: strconv.Itoa(status),
554+
Status: fmt.Sprintf("%03d %s", status, http.StatusText(status)),
555555
StatusCode: status,
556556
Body: NewRespBodyFromBytes(body),
557557
Header: http.Header{},

response_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ func TestResponder_HeaderAddSet(t *testing.T) {
712712

713713
orig := httpmock.NewStringResponder(200, "body")
714714
origNilHeader := httpmock.ResponderFromResponse(&http.Response{
715-
Status: "200",
715+
Status: "200 OK",
716716
StatusCode: 200,
717717
Body: httpmock.NewRespBodyFromString("body"),
718718
ContentLength: -1,

0 commit comments

Comments
 (0)