Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

net/http/httptest: add support for 1XX responses #56151

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions src/net/http/httptest/recorder.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,17 @@ import (
"golang.org/x/net/http/httpguts"
)

// InformationalResponse is an HTTP response sent with a [1xx status code].
//
// [1xx status code]: https://httpwg.org/specs/rfc9110.html#status.1xx
type InformationalResponse struct {
// Code is the 1xx HTTP response code of this informational response.
Code int

// Header contains the headers of this informational response.
Header http.Header
}

// ResponseRecorder is an implementation of http.ResponseWriter that
// records its mutations for later inspection in tests.
type ResponseRecorder struct {
Expand All @@ -27,6 +38,9 @@ type ResponseRecorder struct {
// method.
Code int

// Informational HTTP responses (1xx status code) sent before the main response.
InformationalResponses []InformationalResponse

// HeaderMap contains the headers explicitly set by the Handler.
// It is an internal detail.
//
Expand Down Expand Up @@ -146,11 +160,20 @@ func (rw *ResponseRecorder) WriteHeader(code int) {
}

checkWriteHeaderCode(code)
rw.Code = code
rw.wroteHeader = true

if rw.HeaderMap == nil {
rw.HeaderMap = make(http.Header)
}

if code >= 100 && code < 200 {
ir := InformationalResponse{code, rw.HeaderMap.Clone()}
rw.InformationalResponses = append(rw.InformationalResponses, ir)

return
}

rw.Code = code
rw.wroteHeader = true
rw.snapHeader = rw.HeaderMap.Clone()
}

Expand Down
30 changes: 30 additions & 0 deletions src/net/http/httptest/recorder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"
"io"
"net/http"
"reflect"
"testing"
)

Expand Down Expand Up @@ -123,6 +124,15 @@ func TestRecorder(t *testing.T) {
return nil
}
}
hasInformationalResponses := func(ir []InformationalResponse) checkFunc {
return func(rec *ResponseRecorder) error {
if !reflect.DeepEqual(ir, rec.InformationalResponses) {
return fmt.Errorf("InformationalResponses = %v; want %v", rec.InformationalResponses, ir)
}

return nil
}
}

for _, tt := range [...]struct {
name string
Expand Down Expand Up @@ -294,6 +304,26 @@ func TestRecorder(t *testing.T) {
check(hasResultContents("")), // check we don't crash reading the body

},
{
"1xx status code",
func(rw http.ResponseWriter, _ *http.Request) {
rw.WriteHeader(http.StatusContinue)
rw.Header().Add("Foo", "bar")

rw.WriteHeader(http.StatusEarlyHints)
rw.Header().Add("Baz", "bat")

rw.Header().Del("Foo")
},
check(
hasInformationalResponses([]InformationalResponse{
InformationalResponse{100, http.Header{}},
InformationalResponse{103, http.Header{"Foo": []string{"bar"}}},
}),
hasHeader("Baz", "bat"),
hasNotHeaders("Foo"),
),
},
} {
t.Run(tt.name, func(t *testing.T) {
r, _ := http.NewRequest("GET", "http://foo.com/", nil)
Expand Down