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

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

Open
dunglas opened this issue Oct 20, 2022 · 4 comments · May be fixed by #56151
Open

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

dunglas opened this issue Oct 20, 2022 · 4 comments · May be fixed by #56151
Labels
Milestone

Comments

@dunglas
Copy link
Contributor

dunglas commented Oct 20, 2022

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

@gopherbot gopherbot added this to the Proposal milestone Oct 20, 2022
@ianlancetaylor

This comment has been hidden.

@dunglas

This comment has been hidden.

kisunji added a commit to hashicorp/consul that referenced this issue Oct 24, 2022
These tests started failing in go1.19, presumably due to
support for valid 1xx responses being added.

golang/go#56346
kisunji added a commit to hashicorp/consul that referenced this issue Oct 24, 2022
These tests started failing in go1.19, presumably due to
support for valid 1xx responses being added.

golang/go#56346
kisunji added a commit to hashicorp/consul that referenced this issue Oct 24, 2022
These tests started failing in go1.19, presumably due to
support for valid 1xx responses being added.

golang/go#56346
kisunji added a commit to hashicorp/consul that referenced this issue Oct 24, 2022
These tests started failing in go1.19, presumably due to
support for valid 1xx responses being added.

golang/go#56346
kisunji added a commit to hashicorp/consul that referenced this issue Oct 24, 2022
These tests started failing in go1.19, presumably due to
support for valid 1xx responses being added.

golang/go#56346
kisunji added a commit to hashicorp/consul that referenced this issue Oct 24, 2022
These tests started failing in go1.19, presumably due to
support for valid 1xx responses being added.

golang/go#56346
@dunglas dunglas linked a pull request Nov 23, 2022 that will close this issue
@dunglas
Copy link
Contributor Author

dunglas commented Nov 23, 2022

Updated API proposal following https://go-review.googlesource.com/c/go/+/442215/1#message-b8b8e699ed0237031dbfec5e34b8adf1bf8a31d8:

// 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
}

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

	// ...
}

@gopherbot
Copy link

Change https://go.dev/cl/442215 mentions this issue: net/http/httptest: add support for 1XX responses

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Status: Incoming
Development

Successfully merging a pull request may close this issue.

3 participants