iris icon indicating copy to clipboard operation
iris copied to clipboard

How to disable auto redirect in httptest?

Open huyinghuan opened this issue 4 years ago • 1 comments

I have api router need ctx.Redirect(targetURL, statusCode), I want to check the targetURL is ok,

app.get("/a", func(ctx iris.Context){
  ctx.Redirect("https://google.com", 302)
})

i write test like :

func TestCreate(t *testing.T) {
	t.Run("301 check", func(t *testing.T) {
		e := httptest.New(t, TestServer)
		tt := e.GET("/a").
		response := tt.Expect()
		response.Status(301)
		fmt.Println(response.Header("Location").Raw())
	})
}

the http request will request google.com but it's not my expect result, i just want to get 301 and the text in http header location how to disable request auto redirect and let test pass?

huyinghuan avatar Jun 07 '21 08:06 huyinghuan

in httptest.go line 103change to

testConfiguration := httpexpect.Config{
		BaseURL: conf.URL,
		Client: &http.Client{
			Transport: httpexpect.NewBinder(app),
			Jar:       httpexpect.NewJar(),
			CheckRedirect: func(req *http.Request, via []*http.Request) error {
				return http.ErrUseLastResponse
			},
		},
		Reporter: httpexpect.NewAssertReporter(t),
	}

httptest api can provide params to disable autoredirct?

huyinghuan avatar Jun 08 '21 09:06 huyinghuan