Can't intercept 3rd party initiated request
hawarir opened this issue · comments
Hi, I was wondering how does gock intercepts an outgoing HTTP.
We use API client generated by swagger-codegen to access our other services, so any HTTP traffic are handled inside the said library, not in our packages. And it seems that gock were unable to intercepts any outgoing HTTP request originating from this API client. There's no issue if the HTTP request coming from the code residing in the same package as when gock is invoked.
gock.New(baseUrl).Get(path).Reply(404)
// this will produces the expected result
resp, _ := http.Get(url)
// this will not, the handler type is from another package (the API Client)
result, _ := handler.GetData(param1, param2, param3)
You simply need to instrument gock
in your custom http.Client
instance, in this case the one defined by swagger-codegen
.
See this example: https://github.com/h2non/gock#mocking-a-custom-httpclient-and-httproundtripper
@tomas-fp yeah, that was one of my attempts as well. Unfortunately, I couldn't find any exported function to retrieve the http clients it uses, it's all well wrapped up inside.
I'm using httptest now instead, but thanks for your insight on this.