From f05b02263d6e32851e4db0afc49bb5d671d6287c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9o=20Rebert?= Date: Thu, 14 Nov 2024 14:09:08 +0100 Subject: [PATCH] Make http.Response.Status respect RFC 2616, like in Go stdlib. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- response.go | 4 ++-- response_test.go | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/response.go b/response.go index 75378c4..4e2c666 100644 --- a/response.go +++ b/response.go @@ -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{}, @@ -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{}, diff --git a/response_test.go b/response_test.go index a34915b..9c828d5 100644 --- a/response_test.go +++ b/response_test.go @@ -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,