v3: router: return status 501 instead of 400 on unknown method (#2220)

* router: return status 501 instead of 400 on unknown method

501 is more applicable here.

* router: ensure to always release context

* ctx: fix tests
pull/2255/head
leonklingele 2022-11-15 12:01:34 +01:00 committed by GitHub
parent 73b43cc93e
commit debdb8c4cd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 4 additions and 7 deletions

View File

@ -1402,8 +1402,8 @@ func Test_Ctx_InvalidMethod(t *testing.T) {
app.Handler()(fctx)
require.Equal(t, 400, fctx.Response.StatusCode())
require.Equal(t, []byte("Invalid http method"), fctx.Response.Body())
require.Equal(t, 501, fctx.Response.StatusCode())
require.Equal(t, []byte("Not Implemented"), fctx.Response.Body())
}
// go test -run Test_Ctx_MultipartForm

View File

@ -199,11 +199,11 @@ func (app *App) handler(rctx *fasthttp.RequestCtx) {
c = app.AcquireCtx().(*DefaultCtx)
}
c.Reset(rctx)
defer app.ReleaseCtx(c)
// handle invalid http method directly
if methodInt(c.Method()) == -1 {
_ = c.Status(StatusBadRequest).SendString("Invalid http method")
app.ReleaseCtx(c)
_ = c.SendStatus(StatusNotImplemented)
return
}
@ -224,9 +224,6 @@ func (app *App) handler(rctx *fasthttp.RequestCtx) {
_ = c.SendStatus(StatusInternalServerError)
}
}
// Release Ctx
app.ReleaseCtx(c)
}
func (app *App) addPrefixToRoute(prefix string, route *Route) *Route {