Can I write my own version of go's http client.Do() function for my test cases?


I have a file, called user-service.go and the corresponding test file, called user-service_test.go. As I try to get complete code coverage, I am struggling to get some of the error conditions to actually happen.

Here is the function: GetOrCreateByAccessToken()

//GetOrCreateByAccessToken gets a user from the database with the given access token
func (s *service) GetOrCreateByAccessToken(aT string, client *Client) (*user.User, fcerr.FCErr) {

var currentUser user.OauthUser

req, err := http.NewRequest("GET", ""+aT, nil)
if err != nil {
    return nil, fcerr.NewInternalServerError("Error when setting up the network request")

response, err := client.httpClient.Do(req)
if err != nil {
    fmt.Println("error when getting the userinfo with the access token")
    return nil, fcerr.NewInternalServerError("Error when trying to verify user identity")

defer response.Body.Close()

contents, err := io.ReadAll(response.Body)
if err != nil {
    return nil, fcerr.NewInternalServerError("Error when trying to read response from Google about user identity")

The main control I have for my tests is that I can pass in a *Client.

Here is the part of the test case where I’d like to have io.ReadAll throw an error:

h := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    //manually return the message google would return on an actual request
//Call the testHTTPClient() function defined in the test file to substitute my own HandlerFunc
httpClient, teardown := testHTTPClient(h)
defer teardown()

//Call the real NewClient() from my user-service.go
client := NewClient()

//Substitute the default httpClient for the one I've just set up.
client.httpClient = httpClient

resultingUser, err := userService.GetOrCreateByAccessToken(nU.AccessToken, client)

assert.Nil(t, resultingUser)
assert.NotNil(t, err)
assert.Equal(t, http.StatusInternalServerError, err.Status())

Is there somewhere I can write my own version of the .Do() method which will put something in the response which will cause io.ReadAll to return an error? Or is there a better way to achieve the error with just the pre-baked response text I’m already using?


There is not a way to replace the Do method, but there is a way to accomplish your goal.

Create a round tripper type that returns an arbitrary response body:

type respondWithReader struct{ body io.Reader }

func (rr respondWithReader) RoundTrip(req *http.Request) (*http.Response, error) {
    return &http.Response{
        Proto:      "HTTP/1.0",
        ProtoMajor: 1,
        Header:     make(http.Header),
        Close:      true,
        Body:       ioutil.NopCloser(rr.body),
    }, nil


Create an io.Reader that fails:

var errReadFail = errors.New("blah!")

type failReader int

func (failReader) Read([]byte) (int, error) {
    return 0, errReadFail

Use the stock client with the transport and reader above:

c := http.Client{Transport: respondWithReader{body: failReader(0)}}
resp, err := c.Get("")
if err != nil {
defer resp.Body.Close()

// ReadAll returns errReadFail
_, err = ioutil.ReadAll(resp.Body)
if err != errReadFail {
    t.Errorf("got err %v, expect %v", err, errReadFail)

Run the test on the Go playground.

Answered By – Zombo

Answer Checked By – Robin (GoLangFix Admin)

Leave a Reply

Your email address will not be published.