-
Notifications
You must be signed in to change notification settings - Fork 18k
net/http/httptest: Explicitly discourage using ResponseRecorder.HeaderMap #25763
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
Comments
What do you propose? |
I'd like to just deprecate any usage of that field and say that it shouldn't be accessed. But the previous discussions make me think that there's some still-reasonable usage of HeaderMap that I don't understand? |
I also filed #25764 for changing the API for Go 2. |
Okay, sounds like this isn't concrete enough for Go 1.11 at least. |
The way I see it, ResponseRecorder has a footgun first reported 4 years ago via #8857, and although an alternative to the footgun was provided 2 years ago (Result) there is still nothing in the documentation explicitly stating that it's a footgun. (It's obliquely alluded to in the Result doc comment but why would you even look at that once you've found the headers you need in HeaderMap?) To be concrete, I propose replacing the HeaderMap field documentation with:
|
I'd be fine with: // HeaderMap is the map returned by the Header method. It is an internal detail.
//
// Deprecated: HeaderMap exists for historical compatibility and should not be used.
// To access the headers returned by a handler, use the Response.Header map as returned
// by the Result method.
HeaderMap http.Header Want to send a change? |
Yeah, I'll do that. Thanks. |
Change https://golang.org/cl/117675 mentions this issue: |
(This is basically #16717 again.)
Quick background: if you write a header after the body has been written and status has been set in an HTTP handler, that's a bug because it has no effect. If you're testing using an httptest.ResponseRecorder, its HeaderMap will record the header and might make you think that your code is working when it's not. This is #8857. In response to that bug the behavior was changed to snapshot the headers instead, but that caused another problem (#15560) so it was reverted.
Instead, you're now supposed to use Result to get an http.Response and then look at resp.Header.
The documentation is not clear enough about this. I previously filed #16717 and 8e4ea2f added the following documentation to ResponseRecorder.HeaderMap:
I think we need to be clearer and explicitly discourage looking at HeaderMap.
I've recently fixed a production bug where headers weren't being set and the tests specifically looked for them using HeaderMap. Then I found a whole bunch more places across many code bases where tests looked for headers to be set in HeaderMap. None of those would have caught headers written after Write.
/cc @bradfitz
The text was updated successfully, but these errors were encountered: