在Golang中使ioutil.ReadAll(response.Body)引发错误

For some reason, I cannot seem to get ioutil.ReadAll(res.Body), where res is the *http.Response returned by res, err := hc.Do(redirectRequest) (for hc http.Client, redirectRequest *http.Request).

Testing strategy thus far

Any time I see hc.Do or http.Request in the SUT, my instinct is to spin up a fake server and point the appropriate application states to it. Such a server, for this test, looks like this :

badServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 
    // some stuff
    w.Write([some bad bytes])
}))
defer badServer.Close()

I don't seem to have a way to control res.Body, which is literally the only thing keeping me from 100% test completion against the func this is all in.

I tried, in the errorThrowingServer's handler func, setting r.Body to a stub io.ReadCloser that throws an error when Read() is called, but that doesn't effect res.

As far as I could find perusing the source files for all of the working parts, the only way to get http.Response.Body.Read() to fail is commented here:

https://golang.org/src/net/http/response.go#L53

The response body is streamed on demand as the Body field is read. If the network connection fails or the server terminates the response, Body.Read calls return an error.

Or there is the possibility in ioutil.ReadAll() for it to return bytes.ErrTooLarge here:

https://golang.org/src/io/ioutil/ioutil.go#L20

If the buffer overflows, we will get bytes.ErrTooLarge. Return that as an error. Any other panic remains.

You can mock the body. Basically body is an io.ReadCloser interface so, you can do something like this:

import (
    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/mock"
)

type mockReadCloser struct {
    mock.Mock
}

func (m *mockReadCloser) Read(p []byte) (n int, err error) {
    args := m.Called(p)
    return args.Int(0), args.Error(1)
}

func (m *mockReadCloser) Close() error {
    args := m.Called()
    return args.Error(0)
}

func TestTestingSomethingWithBodyError(t *testing.T) {
    mockReadCloser := mockReadCloser{}
    // if Read is called, it will return error
    mockReadCloser.On("Read", mock.AnythingOfType("[]uint8")).Return(0, fmt.Errorf("error reading"))
    // if Close is called, it will return error
    mockReadCloser.On("Close").Return(fmt.Errorf("error closing"))

    request := &http.Request{
        // pass the mock address
        Body: &mockReadCloser,
    }

    expected := "what you expected"
    result := YourMethod(request)

    assert.Equal(t, expected, result)

    mockReadCloser.AssertExpectations(t)
}

To stop reading you can use:

mockReadCloser.On("Read", mock.AnythingOfType("[]uint8")).Return(0, io.EOF).Once()