Mocking Http requests in go

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: ...

April 10, 2023 · 1 min · Nolan