c.Redirect() allows 201 status code

This commit is contained in:
Manu Mtz.-Almeida 2016-01-28 00:34:05 +01:00
parent b4963aa7de
commit 97cd894279
2 changed files with 24 additions and 3 deletions

View File

@ -394,9 +394,9 @@ func TestContextRenderRedirectWithRelativePath(t *testing.T) {
assert.Panics(t, func() { c.Redirect(299, "/new_path") }) assert.Panics(t, func() { c.Redirect(299, "/new_path") })
assert.Panics(t, func() { c.Redirect(309, "/new_path") }) assert.Panics(t, func() { c.Redirect(309, "/new_path") })
c.Redirect(302, "/path") c.Redirect(301, "/path")
c.Writer.WriteHeaderNow() c.Writer.WriteHeaderNow()
assert.Equal(t, w.Code, 302) assert.Equal(t, w.Code, 301)
assert.Equal(t, w.Header().Get("Location"), "/path") 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") 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) { func TestContextNegotiationFormat(t *testing.T) {
c, _, _ := CreateTestContext() c, _, _ := CreateTestContext()
c.Request, _ = http.NewRequest("POST", "", nil) c.Request, _ = http.NewRequest("POST", "", nil)

View File

@ -16,7 +16,7 @@ type Redirect struct {
} }
func (r Redirect) Render(w http.ResponseWriter) error { 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)) panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code))
} }
http.Redirect(w, r.Request, r.Location, r.Code) http.Redirect(w, r.Request, r.Location, r.Code)