From debdb8c4cd09380fb60f20178cdc155ceaef9a93 Mon Sep 17 00:00:00 2001 From: leonklingele Date: Tue, 15 Nov 2022 12:01:34 +0100 Subject: [PATCH] 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 --- ctx_test.go | 4 ++-- router.go | 7 ++----- 2 files changed, 4 insertions(+), 7 deletions(-) diff --git a/ctx_test.go b/ctx_test.go index 353690da78..b4c0c55d38 100644 --- a/ctx_test.go +++ b/ctx_test.go @@ -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 diff --git a/router.go b/router.go index baab4682ec..8738497629 100644 --- a/router.go +++ b/router.go @@ -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 {