From 97cd89427998b28491443b09649dc89aca1255bb Mon Sep 17 00:00:00 2001 From: "Manu Mtz.-Almeida" Date: Thu, 28 Jan 2016 00:34:05 +0100 Subject: [PATCH] c.Redirect() allows 201 status code --- context_test.go | 25 +++++++++++++++++++++++-- render/redirect.go | 2 +- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/context_test.go b/context_test.go index 1eeabf3a..1b774ff1 100644 --- a/context_test.go +++ b/context_test.go @@ -394,9 +394,9 @@ func TestContextRenderRedirectWithRelativePath(t *testing.T) { assert.Panics(t, func() { c.Redirect(299, "/new_path") }) assert.Panics(t, func() { c.Redirect(309, "/new_path") }) - c.Redirect(302, "/path") + c.Redirect(301, "/path") c.Writer.WriteHeaderNow() - assert.Equal(t, w.Code, 302) + assert.Equal(t, w.Code, 301) assert.Equal(t, w.Header().Get("Location"), "/path") } @@ -410,6 +410,27 @@ func TestContextRenderRedirectWithAbsolutePath(t *testing.T) { assert.Equal(t, w.Header().Get("Location"), "http://google.com") } +func TestContextRenderRedirectWith201(t *testing.T) { + c, w, _ := CreateTestContext() + c.Request, _ = http.NewRequest("POST", "http://example.com", nil) + c.Redirect(201, "/resource") + c.Writer.WriteHeaderNow() + + assert.Equal(t, w.Code, 201) + assert.Equal(t, w.Header().Get("Location"), "/resource") +} + +func TestContextRenderRedirectAll(t *testing.T) { + c, _, _ := CreateTestContext() + c.Request, _ = http.NewRequest("POST", "http://example.com", nil) + assert.Panics(t, func() { c.Redirect(200, "/resource") }) + assert.Panics(t, func() { c.Redirect(202, "/resource") }) + assert.Panics(t, func() { c.Redirect(299, "/resource") }) + assert.Panics(t, func() { c.Redirect(309, "/resource") }) + assert.NotPanics(t, func() { c.Redirect(300, "/resource") }) + assert.NotPanics(t, func() { c.Redirect(308, "/resource") }) +} + func TestContextNegotiationFormat(t *testing.T) { c, _, _ := CreateTestContext() c.Request, _ = http.NewRequest("POST", "", nil) diff --git a/render/redirect.go b/render/redirect.go index d64e4d75..bd48d7d8 100644 --- a/render/redirect.go +++ b/render/redirect.go @@ -16,7 +16,7 @@ type Redirect struct { } func (r Redirect) Render(w http.ResponseWriter) error { - if r.Code < 300 || r.Code > 308 { + if (r.Code < 300 || r.Code > 308) && r.Code != 201 { panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code)) } http.Redirect(w, r.Request, r.Location, r.Code)