h2non / gock

HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽
https://pkg.go.dev/github.com/h2non/gock
MIT License
2.04k stars 106 forks source link

[Question] How to mock two urls and corresponding responses. #90

Closed ziranl16 closed 2 years ago

ziranl16 commented 2 years ago

Hi guys, gock is a great project! However, in my current work, I have a method that will call two different APIs. In our current work, this is currently inevitable.

Thus, I hope to mock two urls in one test and return two corresponding responses. I wonder if there is a way for me to achieve this?

h2non commented 2 years ago

You can simply define the two URLs to mock before the actual method call in your test.

Assuming both HTTP calls work use http.DefaultTransport instance within the same Go process, there is not further action required. However, if the HTTP calls uses a custom instance of http.Client, then you will have to explicitly intercept those instances by using gock.InterceptClient(*http.Client).

ziranl16 commented 2 years ago

You can simply define the two URLs to mock before the actual method call in your test.

Assuming both HTTP calls work use http.DefaultTransport instance within the same Go process, there is not further action required. However, if the HTTP calls uses a custom instance of http.Client, then you will have to explicitly intercept those instances by using gock.InterceptClient(*http.Client).

Thanks!!