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