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

net/http: checks for nil Response or Response.Body in Client.send causing some internal Google tests to fail #38095

andybons opened this issue Mar 26, 2020 · 4 comments


Copy link

@andybons andybons commented Mar 26, 2020

With, we do explicit checks to see if a Response from a RoundTripper is nil or has a nil Body, returning an error if so. This is causing some test code to fail. A contrived example:

type fakeTransport func(*http.Request) (*http.Response, error)

func (f fakeTransport) RoundTrip(req *http.Request) (*http.Response, error) {
  return f(req)


func TestFoo(t *testing.T) {
  transport := fakeTransport(func(req *http.Request) (*http.Response, error) {
    return &http.Response{StatusCode: http.StatusOK}, nil
  client := &http.Client{Transport: transport}
  // use client, get panic

It’s documented that http.Client.Do will always return a non-nil Body if its error is nil.

Should we be enforcing a non-nil Body in these scenarios? My general feeling is yes, though creating a NopCloser body pulls in ioutil and likely another package like strings or bytes for their NewReader functions in these cases.

Thoughts, @bcmills?


This comment has been minimized.

Copy link

@bcmills bcmills commented Mar 26, 2020

Reading the package docs, perhaps we could relax this check. says (emphasis mine):

  // The http Client and Transport guarantee that Body is always
  // non-nil, even on responses without a body or responses with
  // a zero-length body. It is the caller's responsibility to
  // close Body. The default HTTP client's Transport may not
  // reuse HTTP/1.x "keep-alive" TCP connections if the Body is
  // not read to completion and closed.

But says:

 // … Callers
 // should not mutate or reuse the request until the Response's
 // Body has been closed.

which to me implies a non-nil Body. But perhaps it could be construed to mean “until the Response's body has been closed if non-nil”.


This comment has been minimized.

Copy link
Member Author

@andybons andybons commented Mar 26, 2020

Whatever you think is best.


This comment has been minimized.

Copy link

@ianlancetaylor ianlancetaylor commented Mar 26, 2020

Also CC @bradfitz

@bcmills bcmills self-assigned this Mar 27, 2020

This comment has been minimized.

Copy link

@gopherbot gopherbot commented Mar 27, 2020

Change mentions this issue: net/http: treat a nil Body from a custom RoundTripper as an empty one

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.