Skip to content

net/http/httptest: add support for http.ResponseController to ResponseRecorder #60231

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

dunglas
Copy link
Contributor

@dunglas dunglas commented May 16, 2023

Proposal #54136 (implemented in CL 436890 which is part of Go 1.20) added the "http".ResponseController type, which allows manipulating per-request timeouts. This is especially useful for programs managing long-running HTTP connections such as Mercure.

However, testing HTTP handlers leveraging per-request timeouts is currently cumbersome (even if doable) because
"net/http/httptest".ResponseRecorder isn't compatible yet with "http".ResponseController.

This patch makes ResponseRecorder compatible with
"http".ResponseController.

All new methods are part of the contract that response types must honor to be usable with "http".ResponseController.

Deadlines are recorded but not enforced to prevent flaky tests.

Fixes #60229.

…rder

CL golang#54136 (implemented in Go 1.20) added the "http".ResponseController
type, which allows manipulating per-request timeouts.
This is especially useful for programs managing long-running
HTTP connections such as Mercure.

However, testing HTTP handlers leveraging per-request timeouts
is currently cumbersome (even if doable) because
"net/http/httptest".ResponseRecorder isn't compatible yet with
"http".ResponseController.

This patch makes ResponseRecorder compatible with
"http".ResponseController.

All new methods are part of the contract that response types
must honor to be usable with "http".ResponseController.

NewRecorderWithDeadlineAwareRequest() is necessary to test
read deadlines, as calling rw.SetReadDeadline() must change
the deadline on the request body.

Fixes golang#60229.
@gopherbot
Copy link
Contributor

This PR (HEAD: 8e5d8d7) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/495295 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot
Copy link
Contributor

This PR (HEAD: 577f6cf) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/495295 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

@gopherbot
Copy link
Contributor

This PR (HEAD: 82674e6) has been imported to Gerrit for code review.

Please visit https://go-review.googlesource.com/c/go/+/495295 to see it.

Tip: You can toggle comments from me using the comments slash command (e.g. /comments off)
See the Wiki page for more info

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

Successfully merging this pull request may close these issues.

proposal: net/http/httptest: add support for http.ResponseController to ResponseRecorder
3 participants