mirror of https://github.com/gofiber/fiber.git
Add 405 Support
parent
4102262dd7
commit
578e566475
|
@ -199,12 +199,14 @@ func registerDummyRoutes(app *App) {
|
|||
}
|
||||
}
|
||||
|
||||
// go test -v -run=^$ -bench=Benchmark_App_Benchmark_App_MethodNotAllowed -benchmem -count=4
|
||||
// go test -v -run=^$ -bench=Benchmark_App_MethodNotAllowed -benchmem -count=4
|
||||
func Benchmark_App_MethodNotAllowed(b *testing.B) {
|
||||
app := New()
|
||||
app.Get("/this/is/a/dummy/route/oke", func(c *Ctx) {
|
||||
h := func(c *Ctx) {
|
||||
c.Send("Hello World!")
|
||||
})
|
||||
}
|
||||
app.All("/this/is/a/", h)
|
||||
app.Get("/this/is/a/dummy/route/oke", h)
|
||||
c := &fasthttp.RequestCtx{}
|
||||
|
||||
c.Request.Header.SetMethod("DELETE")
|
||||
|
@ -215,6 +217,7 @@ func Benchmark_App_MethodNotAllowed(b *testing.B) {
|
|||
}
|
||||
utils.AssertEqual(b, 405, c.Response.StatusCode())
|
||||
utils.AssertEqual(b, "GET, HEAD", string(c.Response.Header.Peek("Allow")))
|
||||
utils.AssertEqual(b, "Cannot DELETE /this/is/a/dummy/route/oke", string(c.Response.Body()))
|
||||
}
|
||||
|
||||
// go test -v ./... -run=^$ -bench=Benchmark_Router_NotFound -benchmem -count=4
|
||||
|
|
11
utils.go
11
utils.go
|
@ -17,7 +17,8 @@ import (
|
|||
|
||||
// Scan stack if other methods match
|
||||
func setMethodNotAllowed(ctx *Ctx) {
|
||||
original := methodINT[utils.GetString(ctx.Fasthttp.Request.Header.Method())]
|
||||
var allow bool
|
||||
original := methodINT[ctx.method]
|
||||
for i := 0; i < len(intMethod); i++ {
|
||||
// Skip original method
|
||||
if original == i {
|
||||
|
@ -39,13 +40,19 @@ func setMethodNotAllowed(ctx *Ctx) {
|
|||
match, _ := route.match(ctx.path, ctx.pathOriginal)
|
||||
// No match, next route
|
||||
if match {
|
||||
ctx.SendStatus(StatusMethodNotAllowed)
|
||||
// Update allow bool
|
||||
allow = true
|
||||
// Add method to Allow header
|
||||
ctx.Append(HeaderAllow, intMethod[i])
|
||||
// Break stack loop
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
// Update response status
|
||||
if allow {
|
||||
ctx.Status(StatusMethodNotAllowed)
|
||||
}
|
||||
}
|
||||
|
||||
// Generate and set ETag header to response
|
||||
|
|
Loading…
Reference in New Issue