Mocking http retries behavior
As we know, http requests can fail for a large variety of reasons, client side or server side (unauthorized, timeouts etc).
On the client side a common approach is to retry request by increasing the waiting time between retries.
Is this scenario be easily unit test?
Unit Testing
In golang there is this cool library
Simple mocking of an http endpoint:
func TestMockHttpResponse(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("GET", "http://example.com/articles", httpmock.NewStringResponder(200, "my articles").String()))
resp, err := http.Get("http://example.com/") // return http 200
...
}
But there is more, we can actually easily mock retries like this below:
func TestMockHttpResponse(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
first := httpmock.NewStringResponder(500, "")
second := httpmock.NewStringResponder(200, "good response")
httpmock.RegisterResponder("GET", "http://myserver.com/workafteronecall", first.Then(second))
// First call returns 500
resp, err := http.Get("http://example.com/") // return http 500
// Second call returns 200
}