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

Mocking InsecureSkipVerify requests #101

Open retr0h opened 1 year ago

retr0h commented 1 year ago

If i attempt to mock the following request it does not work. However, if I change client to the following it passes. Any idea what I'm doing wrong?

client := &http.Client{}
gock.New("https://0.0.0.0").
        Post("/api/auth/token/login/").
        MatchHeader("Content-Type", "application/json; chartset=UTF-8").
        JSON(map[string]string{"foo": "bar"}).
        Reply(200).
        BodyString(`{"foo": "foo"}`)

body := bytes.NewBuffer([]byte(`{"foo":"bar"}`))
tr := &http.Transport{
        TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
}

client := &http.Client{Transport: tr}
req, err := http.NewRequest("POST", "https://0.0.0.0/api/auth/token/login/", body)
req.Header.Set("Content-Type", "application/json; chartset=UTF-8")
resp, err := client.Do(req)
if err != nil {
        fmt.Printf("error building request: %v", err)
}

Error is:

error building request: Post "https://0.0.0.0/api/auth/token/login/": dial tcp 0.0.0.0:443: connect: connection refused
retr0h commented 1 year ago

Update, if I explicitly set the intercept client, after defining the client containing the InsecureSkipVerify transport, it seems to work. This doesn't seem ideal, any other suggestions?

gock.InterceptClient(client)