How to mock http.Client.Do by setting the cookie in the response

I have a method whose signature is as follows

func Login(client pkg.HTTPClient, userName, password string) ([]*http.Cookie, error) {}```

Here pkg.HTTPClient is an interface as follows:

    package pkg

    type HTTPClient interface {
        Do(req *http.Request) (*http.Response, error)
    }

My intention is to mock the client.Do method and also set cookies in the response of client.Do

I am not sure how to do this because when I inspect the http.Response object I don’t see a way to set cookies

Assuming you are creating a *ResponseRecorder in the mock, like recorder := httptest.NewRecorder(), cookies can be set using http.SetCookie(recorder, &http.Cookie{Name: "test", Value: "expected"}).

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.