-
Notifications
You must be signed in to change notification settings - Fork 103
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
Unable to Mock Responses with Custom http.Client and Transport #154
Comments
Hello, httpmock works using its own implementation of By default, when enabling it globally using If you create your own Note that in that case there is no need to call func TestHttpMock(t *testing.T) {
addr := "http://example/foo"
mockTransport := httpmock.NewMockTransport()
mockTransport.RegisterResponder(http.MethodGet, addr,
func(req *http.Request) (*http.Response, error) {
return httpmock.NewStringResponse(http.StatusOK, "bar"), nil
},
)
cli := createHttpClient()
cli.Transport = mockTransport
ret := doHttp(cli, addr)
assert.Equal(t, "bar", ret)
} |
Mmmm, unfortunately this is not viable for libs that assert |
Asserting that Such libraries should just provide a way to override the |
Facing the same issue unfortunately, so I ended up using gock which provides that functionality. https://github.com/h2non/gock?tab=readme-ov-file#mocking-a-custom-httpclient-and-httproundtripper |
Hi @emirot, I didn't understand what gock does that httpmock cannot do, following the link you gave. If you're talking about var client *http.Client
… // set client
client.Transport = httpmock.DefaultTransport
// or
mock := NewMockTransport()
client.Transport = mock Note that an equivalent of |
I forgot |
@maxatome thanks so much ActivateNonDefault is exactly what I need. I missed it |
@emirot feel free to submit a pr to enhance the readme or the godoc 😉 |
Thank you for creating a good package. I am using a custom http client. If I write the test code as below, it is working good.
However, I confirmed that I cannot receive a mock response if I add the transport option to createHttpClient as shown below.
Is there a way to make it work even if I add the transport option?
The text was updated successfully, but these errors were encountered: