Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mocked response with 301 status code and location header returns an error #97

Open
wxm112 opened this issue Jan 18, 2022 · 1 comment
Open

Comments

@wxm112
Copy link

wxm112 commented Jan 18, 2022

When I mocked a response just with 301 status code, I got an error with a message: 301 response missing Location header. After adding the location header, there was another error with a message: gock: cannot match any request.

The code is shown as below:


import (
	"net/http"
	"testing"

	"github.com/stretchr/testify/assert"
	"gopkg.in/h2non/gock.v1"
)

func TestWithLocationHeader(t *testing.T) {
	defer gock.Off()

	gock.New("https://domain.com").
		Get("/path").
		Reply(301).
		SetHeader("Location", "https://domain.com/newpath")

	_, err := http.Get("https://domain.com/path")

	assert.Equal(t, "Get \"https://domain.com/newpath\": gock: cannot match any request", err.Error())
}

func TestWithoutLocationHeader(t *testing.T) {
	defer gock.Off()

	gock.New("https://domain.com").
		Get("/path").
		Reply(301)

	_, err := http.Get("https://domain.com/path")

	assert.Equal(t, "Get \"https://domain.com/path\": 301 response missing Location header", err.Error())
}
@westy92
Copy link

westy92 commented Dec 6, 2022

I was able to do the following:

defer gock.Off()

gock.New("https://domain.com/").
	Get("/initial").
	Reply(302).
	SetHeader("Location", "https://domain.com/redirected")

gock.New("https://domain.com/").
	Get("/redirected").
	Reply(200).
	File("test.json")

// call API domain.com/initial and assert you get a 200 with the redirect body

assert.True(t, gock.IsDone())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants