Skip to content

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

@dunglas

Description

@dunglas

There is currently no way to test 1XX responses with the httptest package.

Support for 1XX responses has been added to the built-in HTTP server and reverse proxy in Go 1.19 (#42597, #53164).

I propose to add support for inspecting 1XX responses with the test package by integrating the httptrace package with it (as we've done for the reverse proxy).

Regarding the API, I suggest implementing the same API as when using the non-test client:

func Test1xx(t *testing.T) {
	trace := &httptrace.ClientTrace{
		Got100Continue: func() {
			// do something
		},
		Got1xxResponse: func(code int, header textproto.MIMEHeader) error {
			// do something

			return nil
		},
	}

	r, _ := http.NewRequest("GET", "http://example.com", nil)
	rw := httptest.NewRecorder()
	rw.ClientTrace = trace
	func(rw http.ResponseWriter, _ *http.Request) {
		rw.Header().Add("Link", "</image.png>; rel=preload; as=image")
		rw.WriteHeader(http.StatusEarlyHints)

		// ...
	}(rw, r)

       // ...
}

Proposal implementation: #56151

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    Incoming

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions