go-resty / resty

Simple HTTP and REST client library for Go

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

SetResult failed with httpmock

vruge opened this issue · comments

Example

	url := "https://test.org"
	httpClient := resty.New()
	httpmock.ActivateNonDefault(httpClient.GetClient())
	defer httpmock.DeactivateAndReset()
	httpmock.RegisterResponder("GET", url,
		httpmock.NewStringResponder(200, `{"data": true}`),
	)

	testResult := struct {
		Data bool `json:"data"`
	}{Data: false}

	resp, err := httpClient.R().SetResult(&testResult).Get(url)

       fmt.Println(err) // <nil>
	fmt.Println(resp) // {"data": true}
	fmt.Println(testResult) // {false}

workaround

	json.Unmarshal(resp.Body(), &testResult)
	fmt.Println(testResult) // {true}

go.sum

github.com/go-resty/resty/v2 v2.7.0 h1:me+K9p3uhSmXtrBZ4k9jcEAfJmuC8IivWHwaLZwPrFY=
github.com/go-resty/resty/v2 v2.7.0/go.mod h1:9PWDzw47qPphMRFfhsyk0NnSgvluHcljSMVIq3w7q0I= 
github.com/jarcoal/httpmock v1.3.1 h1:iUx3whfZWVf3jT01hQTO/Eo5sAYtB2/rqaUuOtpInww=
github.com/jarcoal/httpmock v1.3.1/go.mod h1:3yb8rc4BI7TCBhFY8ng0gjuLKJNquuDNiPaZjnENuYg=

The reason is that httpmock uses http.DefaultTransport to mock http, but resty does not use http.DefaultTransport.

Thanks @ahuigo for responding to the issue.