Skip to content

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

@wxm112

Description

@wxm112

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())
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions