mirror of https://github.com/gofiber/fiber.git
improve not found error
parent
8ec7cec435
commit
1512997235
|
@ -6,6 +6,7 @@ package fiber
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"html"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
@ -147,7 +148,7 @@ func (app *App) next(c *Ctx) (bool, error) {
|
|||
}
|
||||
|
||||
// If c.Next() does not match, return 404
|
||||
err := NewError(StatusNotFound, "Cannot "+c.method+" "+c.pathOriginal)
|
||||
err := NewError(StatusNotFound, "Cannot "+c.method+" "+html.EscapeString(c.pathOriginal))
|
||||
if !c.matched && app.methodExist(c) {
|
||||
// If no match, scan stack again if other methods match the request
|
||||
// Moved from app.handler because middleware may break the route chain
|
||||
|
|
|
@ -473,6 +473,40 @@ func Test_Route_Static_HasPrefix(t *testing.T) {
|
|||
utils.AssertEqual(t, true, strings.Contains(app.getString(body), "color"))
|
||||
}
|
||||
|
||||
func Test_Router_NotFound(t *testing.T) {
|
||||
app := New()
|
||||
app.Use(func(c *Ctx) error {
|
||||
return c.Next()
|
||||
})
|
||||
appHandler := app.Handler()
|
||||
c := &fasthttp.RequestCtx{}
|
||||
|
||||
c.Request.Header.SetMethod("DELETE")
|
||||
c.URI().SetPath("/this/route/does/not/exist")
|
||||
|
||||
appHandler(c)
|
||||
|
||||
utils.AssertEqual(t, 404, c.Response.StatusCode())
|
||||
utils.AssertEqual(t, "Cannot DELETE /this/route/does/not/exist", string(c.Response.Body()))
|
||||
}
|
||||
|
||||
func Test_Router_NotFound_HTML_Inject(t *testing.T) {
|
||||
app := New()
|
||||
app.Use(func(c *Ctx) error {
|
||||
return c.Next()
|
||||
})
|
||||
appHandler := app.Handler()
|
||||
c := &fasthttp.RequestCtx{}
|
||||
|
||||
c.Request.Header.SetMethod("DELETE")
|
||||
c.URI().SetPath("/does/not/exist<script>alert('foo');</script>")
|
||||
|
||||
appHandler(c)
|
||||
|
||||
utils.AssertEqual(t, 404, c.Response.StatusCode())
|
||||
utils.AssertEqual(t, "Cannot DELETE /does/not/exist<script>alert('foo');</script>", string(c.Response.Body()))
|
||||
}
|
||||
|
||||
//////////////////////////////////////////////
|
||||
///////////////// BENCHMARKS /////////////////
|
||||
//////////////////////////////////////////////
|
||||
|
|
Loading…
Reference in New Issue