here is the test that can repro the error I am getting
package main
import (
"fmt"
"reflect"
"testing"
"github.com/go-resty/resty/v2"
"github.com/jarcoal/httpmock"
)
type fetchReq struct {
URL string
Method string
Body string
QueryParams map[string]string
Headers map[string]string
}
func fetch(req *fetchReq, client *resty.Client) (*resty.Response, error) {
method := req.Method
if method == "" {
method = "get"
}
request := client.R().
SetHeader("content-type", "application/json").
SetAuthToken("asdf").
SetQueryParams(req.QueryParams).
SetHeaders(req.Headers)
if req.Body != "" {
request.SetBody(req.Body)
}
// Make GET request
resp, err := request.Execute(method, req.URL)
return resp, err
}
func Test_fetch(t *testing.T) {
type args struct {
req *fetchReq
}
type mocker struct {
method string
url string
responder httpmock.Responder
}
tests := []struct {
name string
args args
want []byte
want1 *resty.Response
wantErr bool
m []mocker
}{
{
name: "TestSomething",
args: args{
req: &fetchReq{
URL: "https://example.com/events",
Method: "get",
},
},
want: []byte{},
wantErr: false,
m: []mocker{
{
method: "GET",
responder: httpmock.NewStringResponder(500, `{"stat": "500"}`),
url: "https://example.com/events",
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := resty.New()
// Get the underlying HTTP Client and set it to Mock
httpmock.ActivateNonDefault(client.GetClient())
defer httpmock.DeactivateAndReset()
for i := range tt.m {
httpmock.RegisterResponder(tt.m[i].method, tt.m[i].url, tt.m[i].responder)
}
got, err := fetch(tt.args.req, client)
fmt.Println(httpmock.GetCallCountInfo())
if (err != nil) != tt.wantErr {
t.Errorf("fetch() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("fetch() got = %v, want %v", got, tt.want)
}
})
}
}
In the above test I am registering responder for https://example.com/events but when I execute the test I get the following error
error = get "https://example.com/events": no responder found
my guess is that when I use the .Execute method something goes wrong, if I do not use the Execute method and modify fetch function to something like this I do not get any errors
I am using resty v2 along with httpmock
here is the test that can repro the error I am getting
In the above test I am registering responder for
https://example.com/events
but when I execute the test I get the following errormy guess is that when I use the
.Execute
method something goes wrong, if I do not use theExecute
method and modifyfetch
function to something like this I do not get any errorsIt could be an resty issue too, happy to open an issue in the resty repo.