Skip to content

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

@andybons

Description

@andybons

With https://golang.org/cl/221818, 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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions