Skip to content
Myr

c.Redirect() allows 201 status code

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Manu Mtz.-Almeida · committed by Manu Mtz.-Almeida · parent b4963aa · Browse files · 2 files+24 added−3 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+24 added−3 removed

  • other:+24 added−3 removed

2 files+24 added−3 removed

modified:render/redirect.go+1 added-1 removed
14 unmodified lines
15 Location string16}1718func (r Redirect) Render(w http.ResponseWriter) error {19 if r.Code < 300 || r.Code > 308 {19 if (r.Code < 300 || r.Code > 308) && r.Code != 201 {20 panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code))21 }22 http.Redirect(w, r.Request, r.Location, r.Code)23 return nil
1 unmodified line
modified:context_test.go+23 added-2 removed
392 unmodified lines
393 c.Request, _ = http.NewRequest("POST", "http://example.com", nil)394 assert.Panics(t, func() { c.Redirect(299, "/new_path") })395 assert.Panics(t, func() { c.Redirect(309, "/new_path") })396397 c.Redirect(302, "/path")397 c.Redirect(301, "/path")398 c.Writer.WriteHeaderNow()399 assert.Equal(t, w.Code, 302)399 assert.Equal(t, w.Code, 301)400 assert.Equal(t, w.Header().Get("Location"), "/path")401}402403func TestContextRenderRedirectWithAbsolutePath(t *testing.T) {
5 unmodified lines
409 assert.Equal(t, w.Code, 302)410 assert.Equal(t, w.Header().Get("Location"), "http://google.com")411}412413func TestContextRenderRedirectWith201(t *testing.T) {414 c, w, _ := CreateTestContext()415 c.Request, _ = http.NewRequest("POST", "http://example.com", nil)416 c.Redirect(201, "/resource")417 c.Writer.WriteHeaderNow()418419 assert.Equal(t, w.Code, 201)420 assert.Equal(t, w.Header().Get("Location"), "/resource")421}422423func TestContextRenderRedirectAll(t *testing.T) {424 c, _, _ := CreateTestContext()425 c.Request, _ = http.NewRequest("POST", "http://example.com", nil)426 assert.Panics(t, func() { c.Redirect(200, "/resource") })427 assert.Panics(t, func() { c.Redirect(202, "/resource") })428 assert.Panics(t, func() { c.Redirect(299, "/resource") })429 assert.Panics(t, func() { c.Redirect(309, "/resource") })430 assert.NotPanics(t, func() { c.Redirect(300, "/resource") })431 assert.NotPanics(t, func() { c.Redirect(308, "/resource") })432}433434func TestContextNegotiationFormat(t *testing.T) {435 c, _, _ := CreateTestContext()436 c.Request, _ = http.NewRequest("POST", "", nil)437
198 unmodified lines