Skip to content

Commit

Permalink
Context#Redirect
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Feb 20, 2016
1 parent 4f6fd97 commit c8725ae
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
6 changes: 3 additions & 3 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,13 +285,13 @@ func (c *context) NoContent(code int) error {
return nil
}

// Redirect redirects the request using http.Redirect with status code.
// Redirect redirects the request with status code.
func (c *context) Redirect(code int, url string) error {
if code < http.StatusMultipleChoices || code > http.StatusTemporaryRedirect {
return ErrInvalidRedirectCode
}
// TODO: v2
// http.Redirect(c.response, c.request, url, code)
c.response.Header().Set(Location, url)
c.response.WriteHeader(code)
return nil
}

Expand Down
8 changes: 5 additions & 3 deletions context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,20 @@ func TestContext(t *testing.T) {
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
c.NoContent(http.StatusOK)
assert.Equal(t, http.StatusOK, c.Response().Status())
assert.Equal(t, http.StatusOK, rec.Status())

// Redirect
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e)
assert.Equal(t, nil, c.Redirect(http.StatusMovedPermanently, "http://labstack.github.io/echo"))
assert.Equal(t, "http://labstack.github.io/echo", rec.Header().Get(Location))
assert.Equal(t, http.StatusMovedPermanently, rec.Status())

// Error
rec = test.NewResponseRecorder()
c = NewContext(req, rec, e).Object()
c = NewContext(req, rec, e)
c.Error(errors.New("error"))
assert.Equal(t, http.StatusInternalServerError, c.Response().Status())
assert.Equal(t, http.StatusInternalServerError, rec.Status())

// reset
c.Object().reset(req, test.NewResponseRecorder())
Expand Down
12 changes: 6 additions & 6 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit c8725ae

Please sign in to comment.