Skip to content

Commit

Permalink
Make http.Response.Status respect RFC 2616, like in Go stdlib.
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
constantoine committed Nov 14, 2024
1 parent 8705bb3 commit f05b022
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 3 deletions.
4 changes: 2 additions & 2 deletions response.go
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,7 @@ func NewNotFoundResponder(fn func(...any)) Responder {
// httpmock.NewStringResponse(200, httpmock.File("body.txt").String())
func NewStringResponse(status int, body string) *http.Response {
return &http.Response{
Status: strconv.Itoa(status),
Status: fmt.Sprintf("%03d %s", status, http.StatusText(status)),
StatusCode: status,
Body: NewRespBodyFromString(body),
Header: http.Header{},
Expand All @@ -551,7 +551,7 @@ func NewStringResponder(status int, body string) Responder {
// httpmock.NewBytesResponse(200, httpmock.File("body.raw").Bytes())
func NewBytesResponse(status int, body []byte) *http.Response {
return &http.Response{
Status: strconv.Itoa(status),
Status: fmt.Sprintf("%03d %s", status, http.StatusText(status)),
StatusCode: status,
Body: NewRespBodyFromBytes(body),
Header: http.Header{},
Expand Down
2 changes: 1 addition & 1 deletion response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ func TestResponder_HeaderAddSet(t *testing.T) {

orig := httpmock.NewStringResponder(200, "body")
origNilHeader := httpmock.ResponderFromResponse(&http.Response{
Status: "200",
Status: "200 OK",
StatusCode: 200,
Body: httpmock.NewRespBodyFromString("body"),
ContentLength: -1,
Expand Down

0 comments on commit f05b022

Please sign in to comment.