gock
gock copied to clipboard
Does not appear to work with `go-retryablehttp` client / http.client retries
trafficstars
It appears that gock doesn't work with the hashicorp retriable http library. This is unfortunate and I thought it would since the aforementioned library returns a standard http.client struct.
It appears this may be related to retries more generally (not that specific lib) and some other info is here https://github.com/h2non/gock/issues/71 , but not sure.
I am happy to take a look at addressing this if anyone has any ideas why this might be happening
Reproduction Example
module gocktest
go 1.23.3
require (
github.com/h2non/gock v1.2.0
github.com/hashicorp/go-retryablehttp v0.7.7
)
require (
github.com/h2non/parth v0.0.0-20190131123155-b4df798d6542 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
)
package main
import (
"fmt"
"io/ioutil"
"net/http"
"time"
"github.com/h2non/gock"
"github.com/hashicorp/go-retryablehttp"
)
// NewRetryableHTTPClient returns an HTTP client with automatic retries.
func NewRetryableHTTPClient() *http.Client {
retryClient := retryablehttp.NewClient()
retryClient.RetryMax = 3
retryClient.RetryWaitMin = 1 * time.Second
retryClient.RetryWaitMax = 5 * time.Second
// don't spam in the logs with DEBUG messages
// we should define logs in the application
// not the library level
retryClient.Logger = nil
return retryClient.StandardClient() // Convert to *http.Client so we can use it in the jsonld loader
}
func main() {
defer gock.Off()
gock.New("http://foo.com").
Get("/bar").
Reply(200).
JSON(map[string]string{"foo": "bar"})
res, err := NewRetryableHTTPClient().Get("http://foo.com/bar")
if err != nil {
panic(err)
}
if res.StatusCode != 200 {
panic(fmt.Sprintf("bad status code, got %d", res.StatusCode))
}
body, _ := ioutil.ReadAll(res.Body)
if string(body)[:13] != `{"foo":"bar"}` {
panic("bad body")
}
}
go run .
panic: bad status code, got 404
goroutine 1 [running]:
main.main()
/Users/cloftus/github/gockTest/main.go:42 +0x2a0
exit status 2
Seems to be related to this issue https://github.com/h2non/gock/issues/27