jarcoal / httpmock

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

getting no responder found error when using `Execute` method from `github.com/go-resty/resty/v2` #95

Closed shreyaskarnik closed 4 years ago

shreyaskarnik commented 4 years ago

I am using resty v2 along with httpmock

github.com/go-resty/resty/v2 v2.3.0
github.com/jarcoal/httpmock v1.0.6

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

resp, err := client.R().
        SetHeader("content-type", "application/json").
        SetAuthToken("asdf").
        SetQueryParams(req.QueryParams).
        SetHeaders(req.Headers).Get(req.URL)

It could be an resty issue too, happy to open an issue in the resty repo.

maxatome commented 4 years ago

change

Method: "get",

to

Method: "GET",
shreyaskarnik commented 4 years ago

Thanks @maxatome case sensitive args strike again 😞 my bad

maxatome commented 4 years ago

https://tools.ietf.org/html/rfc2616#page-36