Comments (3)
Hello,
You set a "Location" header in your mocked response of POST /api/v1/some_action
, so you have to provide a responder for this new URL...
from httpmock.
I found a way to skip this problem.
import (
"errors"
"fmt"
"github.com/go-resty/resty/v2"
"github.com/jarcoal/httpmock"
"github.com/stretchr/testify/assert"
"net/http"
"strings"
"testing"
"time"
)
var ErrNoAuth = errors.New("no auth")
func createClient() *resty.Client {
client := resty.New()
client.EnableTrace()
client.SetTimeout(time.Second * 60)
client.SetRetryCount(0)
client.SetBaseURL("http://mock.server")
client.SetRedirectPolicy(resty.RedirectPolicyFunc(func(request *http.Request, requests []*http.Request) error {
if strings.Contains(request.URL.Path, "/account/login") {
fmt.Printf("RedirectPolicy matched 302 location. return ErrNoAuth\n")
return ErrNoAuth
}
return nil
}))
//client.OnAfterResponse(func(client *resty.Client, response *resty.Response) error {
// switch response.StatusCode() {
// case http.StatusFound:
// location := response.Header().Get("Location")
// if strings.Contains(location, "/account/login") {
// fmt.Printf("OnAfterResponse matched 302 location. return ErrNoAuth\n")
// return ErrNoAuth
// }
// }
// return nil
//})
return client
}
func TestForResty302(t *testing.T) {
client := createClient()
httpmock.Activate()
httpmock.ActivateNonDefault(client.GetClient())
defer httpmock.DeactivateAndReset()
httpmock.RegisterResponder("POST", "/api/v1/some_action", func(request *http.Request) (*http.Response, error) {
response := httpmock.NewStringResponse(http.StatusFound, "")
response.Header.Set("Location", "https://new.server/account/login")
return response, nil
})
_, err := client.R().Post("/api/v1/some_action")
// Expect
assert.ErrorIs(t, err, ErrNoAuth)
}
from httpmock.
Hello, You set a "Location" header in your mocked response of
POST /api/v1/some_action
, so you have to provide a responder for this new URL...
Using a new Responder is a bit counterintuitive. This is because I originally intended to test whether OnAfterResponse was in effect.
Only 302 redirects are special and can be intercepted by the SetRedirectPolicy interface provided by resty.
In this case, use httpmock to set the server response message to get the correct result.
Thanks for your work.
from httpmock.
Related Issues (20)
- no matching versions for query "latest" HOT 1
- No responder found when mocking with Cobra.Execute() HOT 2
- Mock not working HOT 5
- Inconsist behavior between http.Response.Body and httpmock.Response.Body HOT 4
- Support for go 1.17 HOT 2
- Httpmock not working with resty HOT 4
- No respond for URL question HOT 5
- how httpmock works with InsecureSkipVerify HOT 2
- t.Parallel() HOT 1
- [Question] How to add custom values in the response header ? HOT 2
- http mocks regardless of domain HOT 2
- Question: how to sponsor this project? HOT 2
- Capture the request body HOT 20
- [Question] How to return binary files in http response body HOT 1
- `Or()` and `And()` don't work correctly HOT 2
- MinTimes and MaxTimes responder invocations HOT 5
- getting no responder found error when using `Execute` method from `github.com/go-resty/resty/v2` HOT 3
- golangcli-lint says no export data for "github.com/jarcoal/httpmock" HOT 4
- Implemented test case with mock. but it's not working HOT 15
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from httpmock.