From c8725ae244614f161cb02545fdcc0f1346883937 Mon Sep 17 00:00:00 2001 From: Vishal Rana Date: Sat, 20 Feb 2016 12:54:43 -0800 Subject: [PATCH] Context#Redirect Signed-off-by: Vishal Rana --- context.go | 6 +++--- context_test.go | 8 +++++--- glide.lock | 12 ++++++------ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/context.go b/context.go index e2db9d33..d882485d 100644 --- a/context.go +++ b/context.go @@ -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 } diff --git a/context_test.go b/context_test.go index 6622ad8b..c6d3bb6a 100644 --- a/context_test.go +++ b/context_test.go @@ -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()) diff --git a/glide.lock b/glide.lock index 253ab23e..0e817a2b 100644 --- a/glide.lock +++ b/glide.lock @@ -1,27 +1,27 @@ hash: f220137e9ccd9aaf3051be33c3c23ea8abd2c40df6cf96175c3994d482b5e007 -updated: 2016-02-18T22:26:06.777493855-08:00 +updated: 2016-02-20T12:46:29.282847891-08:00 imports: - name: github.com/klauspost/compress - version: 3dcfad3351d86b2c07406ac93e92f03edd98c707 + version: f9625351863b5e94c1da72862187b8fe9a91af90 subpackages: - flate - gzip - zlib - name: github.com/klauspost/cpuid - version: 349c675778172472f5e8f3a3e0fe187e302e5a10 + version: 2c698c6aef5976c7860074cc7040e8af7866aa21 - name: github.com/klauspost/crc32 - version: 81ac41837f877bc6a7f81321c2c29d88d69c108a + version: 19b0b332c9e4516a6370a0456e6182c3b5036720 - name: github.com/labstack/gommon version: bfff5bf04688a4048a5cb4dd3b3f0697caaad19c subpackages: - color - log - name: github.com/mattn/go-colorable - version: 9fdad7c47650b7d2e1da50644c1f4ba7f172f252 + version: 9cbef7c35391cca05f15f8181dc0b18bc9736dbb - name: github.com/mattn/go-isatty version: 56b76bdf51f7708750eac80fa38b952bb9f32639 - name: github.com/valyala/fasthttp - version: 14b2ff3d2b38a596b037541c94f43718d9da215f + version: 58e4dea85db1b01e95b02e105b2a0e953c52694a - name: golang.org/x/net version: b6d7b1396ec874c3b00f6c84cd4301a17c56c8ed subpackages: