Skip to content

Commit

Permalink
refactor: use http.Redirect for redirect function
Browse files Browse the repository at this point in the history
  • Loading branch information
vicanso committed Sep 17, 2019
1 parent adaa585 commit 3e417a8
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 2 deletions.
4 changes: 3 additions & 1 deletion context.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,10 @@ func (c *Context) Redirect(code int, url string) (err error) {
err = ErrInvalidRedirect
return
}

c.StatusCode = code
c.SetHeader(HeaderLocation, url)
c.Committed = true
http.Redirect(c.Response, c.Request, url, code)
return
}

Expand Down
4 changes: 3 additions & 1 deletion context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -365,15 +365,17 @@ func TestSignedCookie(t *testing.T) {

func TestRedirect(t *testing.T) {
assert := assert.New(t)
req := httptest.NewRequest("GET", "/", nil)
resp := httptest.NewRecorder()
c := NewContext(resp, nil)
c := NewContext(resp, req)
err := c.Redirect(299, "")
assert.Equal(err, ErrInvalidRedirect)

url := "https://aslant.site/"
err = c.Redirect(302, url)
assert.Nil(err)
assert.Equal(url, c.GetHeader(HeaderLocation))
assert.Equal(302, c.StatusCode)
}

func TestCreate(t *testing.T) {
Expand Down

0 comments on commit 3e417a8

Please sign in to comment.