-
Notifications
You must be signed in to change notification settings - Fork 17.7k
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: Recorder.Result() should always set Response.Body to http.NoBody when response is empty #39290
Comments
I’m not sure we make any explicit guarantees that an empty response’s body will be equal to The documentation seems to imply that this is used when constructing requests, not interpreting responses. Is the desire that any response with an empty body be equal to |
Just to pile on an additional suggestion along the same lines. We could consider implementing json.Marshaler so that http.NoBody doesnt marshal as |
@MaerF0x0 to be clear, my comments weren’t suggestions, but questions for you to gain a better understanding of your report. |
My apologies, i thought those tags for brad/damien meant the q's were for them :). I do not see any documentation indicating http.NoBody to be returned. func (rw *ResponseRecorder) Result() *http.Response {
//... code ...
if rw.Body != nil {
res.Body = ioutil.NopCloser(bytes.NewReader(rw.Body.Bytes()))
} else {
res.Body = http.NoBody
} https://github.com/golang/go/blob/master/src/net/http/httptest/recorder.go#L181-L185 This guard seems to imply if the returned body == nil, actually make it |
This sounds like a proposal, as the behavior is not explicitly defined nor documented. If we decided to set a response’s I’ll mark as a proposal for now and see what the committee thinks. Since Brad is on proposal review he’ll be able to chime in as well (if he doesn’t get a chance in this thread). |
NoBody was originally added out of necessity to make an ambiguous case not ambiguous, to know when a request was replayable (as an io.ReadCloser isn't in general comparable usefully). I'd rather not expand its meaning to guarantee when it's set in Response.Body. I'd worry that if we did this, people would start testing |
This proposal has been added to the active column of the proposals project |
Based on the discussion above, this proposal seems like a likely decline. |
No change in consensus, so declined. |
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
I looked at the code in master and it looks the same
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
https://play.golang.org/p/r3ypSy1vAIb
Attempting to use testify/assert to assert that the response is an empty body like
What did you expect to see?
Return
http.NoBody
What did you see instead?
ioutil.nopCloser(ioutil.nopCloser{Reader:(*bytes.Reader)(0xc000566ba0)})
The text was updated successfully, but these errors were encountered: