mirror of https://github.com/gofiber/fiber.git
🩹 handle invalid http method
parent
c2f552d92e
commit
cf22aa2ae5
16
ctx_test.go
16
ctx_test.go
|
@ -805,6 +805,22 @@ func Test_Ctx_Method(t *testing.T) {
|
|||
utils.AssertEqual(t, MethodPost, ctx.Method())
|
||||
}
|
||||
|
||||
// go test -run Test_Ctx_InvalidMethod
|
||||
func Test_Ctx_InvalidMethod(t *testing.T) {
|
||||
t.Parallel()
|
||||
app := New()
|
||||
app.Get("/", func(c *Ctx) {})
|
||||
|
||||
fctx := &fasthttp.RequestCtx{}
|
||||
fctx.Request.Header.SetMethod("InvalidMethod")
|
||||
fctx.Request.SetRequestURI("/")
|
||||
|
||||
app.handler(fctx)
|
||||
|
||||
utils.AssertEqual(t, 400, fctx.Response.StatusCode())
|
||||
utils.AssertEqual(t, []byte("Invalid http method"), fctx.Response.Body())
|
||||
}
|
||||
|
||||
// go test -run Test_Ctx_MultipartForm
|
||||
func Test_Ctx_MultipartForm(t *testing.T) {
|
||||
t.Parallel()
|
||||
|
|
|
@ -128,6 +128,14 @@ func (app *App) next(ctx *Ctx) bool {
|
|||
func (app *App) handler(rctx *fasthttp.RequestCtx) {
|
||||
// Acquire Ctx with fasthttp request from pool
|
||||
ctx := app.AcquireCtx(rctx)
|
||||
|
||||
// handle invalid http method directly
|
||||
if ctx.methodINT == -1 {
|
||||
ctx.Status(StatusBadRequest).SendString("Invalid http method")
|
||||
app.ReleaseCtx(ctx)
|
||||
return
|
||||
}
|
||||
|
||||
// Prettify path
|
||||
ctx.prettifyPath()
|
||||
// Find match in stack
|
||||
|
|
Loading…
Reference in New Issue