Skip to content

Commit

Permalink
test: fix not found by method
Browse files Browse the repository at this point in the history
  • Loading branch information
savsgio committed Feb 28, 2023
1 parent 2d790ee commit c3fcfb3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 30 deletions.
26 changes: 13 additions & 13 deletions router.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func (r *Router) methodIndexOf(method string) int {

// Mutable allows updating the route handler
//
// It's disabled by default
// # It's disabled by default
//
// WARNING: Use with care. It could generate unexpected behaviours
func (r *Router) Mutable(v bool) {
Expand Down Expand Up @@ -165,7 +165,8 @@ func (r *Router) ANY(path string, handler fasthttp.RequestHandler) {
// "/etc/passwd" would be served.
// Internally a fasthttp.FSHandler is used, therefore fasthttp.NotFound is used instead
// Use:
// router.ServeFiles("/src/{filepath:*}", "./")
//
// router.ServeFiles("/src/{filepath:*}", "./")
func (r *Router) ServeFiles(path string, rootPath string) {
r.ServeFilesCustom(path, &fasthttp.FS{
Root: rootPath,
Expand All @@ -183,7 +184,8 @@ func (r *Router) ServeFiles(path string, rootPath string) {
// Internally a fasthttp.FSHandler is used, therefore http.NotFound is used instead
// of the Router's NotFound handler.
// Use:
// router.ServeFilesCustom("/src/{filepath:*}", *customFS)
//
// router.ServeFilesCustom("/src/{filepath:*}", *customFS)
func (r *Router) ServeFilesCustom(path string, fs *fasthttp.FS) {
suffix := "/{filepath:*}"

Expand Down Expand Up @@ -353,46 +355,44 @@ func (r *Router) tryRedirect(ctx *fasthttp.RequestCtx, tree *radix.Tree, tsr boo
uri.SetString(path[:len(path)-1])
} else {
uri.SetString(path)
uri.WriteString("/")
uri.WriteByte('/')
}

queryBuf := ctx.URI().QueryString()
if len(queryBuf) > 0 {
if queryBuf := ctx.URI().QueryString(); len(queryBuf) > 0 {
uri.WriteByte(questionMark)
uri.Write(queryBuf)
}

ctx.Redirect(uri.String(), code)

bytebufferpool.Put(uri)

return true
}

// Try to fix the request path
if r.RedirectFixedPath {
path := strconv.B2S(ctx.Request.URI().Path())
path2 := strconv.B2S(ctx.Request.URI().Path())

uri := bytebufferpool.Get()
found := tree.FindCaseInsensitivePath(
cleanPath(path),
cleanPath(path2),
r.RedirectTrailingSlash,
uri,
)

if found {
queryBuf := ctx.URI().QueryString()
if len(queryBuf) > 0 {
if queryBuf := ctx.URI().QueryString(); len(queryBuf) > 0 {
uri.WriteByte(questionMark)
uri.Write(queryBuf)
}

ctx.RedirectBytes(uri.Bytes(), code)

ctx.Redirect(uri.String(), code)
bytebufferpool.Put(uri)

return true
}

bytebufferpool.Put(uri)
}

return false
Expand Down
44 changes: 27 additions & 17 deletions router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,12 +578,18 @@ func testRouterNotFoundByMethod(t *testing.T, method string) {
router.Handle(method, "/USERS/{name}/enTRies/", handlerFunc)
router.Handle(method, "/static/{filepath:*}", handlerFunc)

reqMethod := method
if method == MethodWild {
reqMethod = randomHTTPMethod()
}

// Moved Permanently, request with GET method
expectedCode := fasthttp.StatusMovedPermanently
if method == fasthttp.MethodConnect {
switch {
case reqMethod == fasthttp.MethodConnect:
// CONNECT method does not allow redirects, so Not Found (404)
expectedCode = fasthttp.StatusNotFound
} else if method != fasthttp.MethodGet {
case reqMethod != fasthttp.MethodGet:
// Permanent Redirect, request with same method
expectedCode = fasthttp.StatusPermanentRedirect
}
Expand Down Expand Up @@ -616,30 +622,29 @@ func testRouterNotFoundByMethod(t *testing.T, method string) {
}...)
}

reqMethod := method
if method == MethodWild {
reqMethod = randomHTTPMethod()
}

for _, tr := range testRoutes {
if runtime.GOOS == "windows" && strings.HasPrefix(tr.route, "/../") {
// See: https://github.com/valyala/fasthttp/issues/1226
t.Logf("skipping route '%s %s' on %s, unsupported yet", reqMethod, tr.route, runtime.GOOS)

continue
}

ctx := new(fasthttp.RequestCtx)

ctx.Request.Header.SetMethod(reqMethod)
ctx.Request.SetRequestURI(tr.route)
ctx.Request.SetHost(host)

router.Handler(ctx)

statusCode := ctx.Response.StatusCode()
location := string(ctx.Response.Header.Peek("Location"))

if !(statusCode == tr.code && (statusCode == fasthttp.StatusNotFound || location == tr.location)) {
t.Errorf("NotFound handling route %s failed: ReqMethod=%s, Code=%d, Header=%v", method, tr.route, statusCode, location)
fn := t.Errorf
msg := "NotFound handling route '%s' failed: Method=%s, ReqMethod=%s, Code=%d, ExpectedCode=%d, Header=%v"

if runtime.GOOS == "windows" && strings.HasPrefix(tr.route, "/../") {
// See: https://github.com/valyala/fasthttp/issues/1226
// Not fail, because it is a known issue.
fn = t.Logf
msg = "ERROR: " + msg
}

fn(msg, tr.route, method, reqMethod, statusCode, tr.code, location)
}
}

Expand All @@ -655,9 +660,14 @@ func testRouterNotFoundByMethod(t *testing.T, method string) {
ctx.Request.Header.SetMethod(reqMethod)
ctx.Request.SetRequestURI("/nope")
router.Handler(ctx)

if !(ctx.Response.StatusCode() == fasthttp.StatusNotFound && notFound == true) {
t.Errorf("Custom NotFound handler failed: Code=%d, Header=%v", ctx.Response.StatusCode(), ctx.Response.Header.String())
t.Errorf(
"Custom NotFound handling failed: Method=%s, ReqMethod=%s, Code=%d, Header=%v",
method, reqMethod, ctx.Response.StatusCode(), ctx.Response.Header.String(),
)
}

ctx.Response.Reset()
}

Expand Down

0 comments on commit c3fcfb3

Please sign in to comment.