Skip to content

Instantly share code, notes, and snippets.

@xcoulon
Last active September 30, 2025 13:24
Show Gist options
  • Save xcoulon/99e9a6afe18669872988e1ef64880981 to your computer and use it in GitHub Desktop.
Save xcoulon/99e9a6afe18669872988e1ef64880981 to your computer and use it in GitHub Desktop.
package gock_test
import (
"crypto/tls"
"io"
"net/http"
"testing"
"github.com/h2non/gock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInterceptClient(t *testing.T) {
// given
cl := http.DefaultClient
cl.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
InsecureSkipVerify: true,
},
}
gock.InterceptClient(cl)
gock.New("https://my-argocd-server").
Get("/api/v1/applications").
Reply(200).
BodyString(`{"items": []}`)
defer gock.Off() // disable HTTP interceptor after test execution
// when
resp, err := cl.Get("https://my-argocd-server/api/v1/applications")
// then
require.NoError(t, err)
data, err := io.ReadAll(resp.Body)
defer resp.Body.Close()
require.NoError(t, err)
assert.JSONEq(t, `{"items": []}`, string(data))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment