jarcoal / httpmock

HTTP mocking for Golang
http://godoc.org/github.com/jarcoal/httpmock
MIT License
1.93k stars 103 forks source link

httmock not working with resty #89

Closed heronrs closed 4 years ago

heronrs commented 4 years ago

I have this small code block to test resty + httpmock but the requests is not being intercepted. I'm getting the google page html as a response. Am I missing anything?

package main

import (
    "fmt"

    "github.com/go-resty/resty/v2"
    "github.com/jarcoal/httpmock"
)

func main() {
    client := resty.New()
    httpmock.ActivateNonDefault(resty.New().GetClient())
    defer httpmock.DeactivateAndReset()

    httpmock.RegisterResponder("GET", "https://google.com",
        httpmock.NewStringResponder(200, `[{"id": 1, "name": "My Great Article"}]`))

    resp, _ := client.R().
        Get("https://google.com")

    fmt.Println(resp)

}
maxatome commented 4 years ago

Hi That is because you don't enable httpmock on the right client. Do this instead:

httpmock.ActivateNonDefault(client.GetClient())
heronrs commented 4 years ago

Hey sorry for the delay. It worked. Thanks!