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

Testing http.Client.Timeout value with retries #71

Open danielbh opened 3 years ago

danielbh commented 3 years ago

Hello,

Great lib!

I'm trying to test retries with a specific http.Client.Timeout value. Response.Delay does not seem to work as expected. I set it to 60 and I set my http.Client.Timeout to 5. My retries are also set to retry after Client.Timeout. When I do this and run the following test it does not seem to timeout as expected. What I expect to happen is to retry twice after a maximum of 5 seconds per request. What happens instead is the test takes much longer to run. Any idea of a workaround or area of the gock code that might change to make this work? Happy to submit a PR for this with your guidance. Thanks.


         gock.New(url).
        Post("/endpoint").
        Response.
        Delay(60 * time.Second)

          gock.New(url).
        Post("/endpoint").
        Response.
        Delay(60 * time.Second)

    gock.New(url).
        Post("/endpoint").
        MatchHeaders(expectedRequestHeaders).
        Reply(http.StatusCreated)

    client := CreateHTTClientAdapter()
    gock.InterceptClient(client.GetHTTPClient())
        resp,err := client.Post(generateBody(), test.CreateStandardHeaders())

    assert.Nil(suite.T(), err, "err should be nil and should handle error through response")
    assert.Equal(suite.T(), http.StatusCreated, resp.Status, "status should be 201/Created")