Coder Social home page Coder Social logo

Comments (3)

maxatome avatar maxatome commented on August 23, 2024

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.

daviyang35 avatar daviyang35 commented on August 23, 2024

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.

daviyang35 avatar daviyang35 commented on August 23, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.