Skip to content

Commit

Permalink
v3: router: return status 501 instead of 400 on unknown method (#2220)
Browse files Browse the repository at this point in the history
* router: return status 501 instead of 400 on unknown method

501 is more applicable here.

* router: ensure to always release context

* ctx: fix tests
  • Loading branch information
leonklingele committed Nov 15, 2022
1 parent 73b43cc commit debdb8c
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 7 deletions.
4 changes: 2 additions & 2 deletions ctx_test.go
Expand Up @@ -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
Expand Down
7 changes: 2 additions & 5 deletions router.go
Expand Up @@ -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
}

Expand All @@ -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 {
Expand Down

1 comment on commit debdb8c

@ReneWerner87
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: debdb8c Previous: 235cd9d Ratio
Benchmark_AcquireCtx 1874 ns/op 1568 B/op 5 allocs/op 755 ns/op 1568 B/op 5 allocs/op 2.48

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.