Skip to content

Commit c3fcfb3

Browse files
committed
test: fix not found by method
1 parent 2d790ee commit c3fcfb3

File tree

2 files changed

+40
-30
lines changed

2 files changed

+40
-30
lines changed

router.go

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func (r *Router) methodIndexOf(method string) int {
8686

8787
// Mutable allows updating the route handler
8888
//
89-
// It's disabled by default
89+
// # It's disabled by default
9090
//
9191
// WARNING: Use with care. It could generate unexpected behaviours
9292
func (r *Router) Mutable(v bool) {
@@ -165,7 +165,8 @@ func (r *Router) ANY(path string, handler fasthttp.RequestHandler) {
165165
// "/etc/passwd" would be served.
166166
// Internally a fasthttp.FSHandler is used, therefore fasthttp.NotFound is used instead
167167
// Use:
168-
// router.ServeFiles("/src/{filepath:*}", "./")
168+
//
169+
// router.ServeFiles("/src/{filepath:*}", "./")
169170
func (r *Router) ServeFiles(path string, rootPath string) {
170171
r.ServeFilesCustom(path, &fasthttp.FS{
171172
Root: rootPath,
@@ -183,7 +184,8 @@ func (r *Router) ServeFiles(path string, rootPath string) {
183184
// Internally a fasthttp.FSHandler is used, therefore http.NotFound is used instead
184185
// of the Router's NotFound handler.
185186
// Use:
186-
// router.ServeFilesCustom("/src/{filepath:*}", *customFS)
187+
//
188+
// router.ServeFilesCustom("/src/{filepath:*}", *customFS)
187189
func (r *Router) ServeFilesCustom(path string, fs *fasthttp.FS) {
188190
suffix := "/{filepath:*}"
189191

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

359-
queryBuf := ctx.URI().QueryString()
360-
if len(queryBuf) > 0 {
361+
if queryBuf := ctx.URI().QueryString(); len(queryBuf) > 0 {
361362
uri.WriteByte(questionMark)
362363
uri.Write(queryBuf)
363364
}
364365

365366
ctx.Redirect(uri.String(), code)
366-
367367
bytebufferpool.Put(uri)
368368

369369
return true
370370
}
371371

372372
// Try to fix the request path
373373
if r.RedirectFixedPath {
374-
path := strconv.B2S(ctx.Request.URI().Path())
374+
path2 := strconv.B2S(ctx.Request.URI().Path())
375375

376376
uri := bytebufferpool.Get()
377377
found := tree.FindCaseInsensitivePath(
378-
cleanPath(path),
378+
cleanPath(path2),
379379
r.RedirectTrailingSlash,
380380
uri,
381381
)
382382

383383
if found {
384-
queryBuf := ctx.URI().QueryString()
385-
if len(queryBuf) > 0 {
384+
if queryBuf := ctx.URI().QueryString(); len(queryBuf) > 0 {
386385
uri.WriteByte(questionMark)
387386
uri.Write(queryBuf)
388387
}
389388

390-
ctx.RedirectBytes(uri.Bytes(), code)
391-
389+
ctx.Redirect(uri.String(), code)
392390
bytebufferpool.Put(uri)
393391

394392
return true
395393
}
394+
395+
bytebufferpool.Put(uri)
396396
}
397397

398398
return false

router_test.go

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -578,12 +578,18 @@ func testRouterNotFoundByMethod(t *testing.T, method string) {
578578
router.Handle(method, "/USERS/{name}/enTRies/", handlerFunc)
579579
router.Handle(method, "/static/{filepath:*}", handlerFunc)
580580

581+
reqMethod := method
582+
if method == MethodWild {
583+
reqMethod = randomHTTPMethod()
584+
}
585+
581586
// Moved Permanently, request with GET method
582587
expectedCode := fasthttp.StatusMovedPermanently
583-
if method == fasthttp.MethodConnect {
588+
switch {
589+
case reqMethod == fasthttp.MethodConnect:
584590
// CONNECT method does not allow redirects, so Not Found (404)
585591
expectedCode = fasthttp.StatusNotFound
586-
} else if method != fasthttp.MethodGet {
592+
case reqMethod != fasthttp.MethodGet:
587593
// Permanent Redirect, request with same method
588594
expectedCode = fasthttp.StatusPermanentRedirect
589595
}
@@ -616,30 +622,29 @@ func testRouterNotFoundByMethod(t *testing.T, method string) {
616622
}...)
617623
}
618624

619-
reqMethod := method
620-
if method == MethodWild {
621-
reqMethod = randomHTTPMethod()
622-
}
623-
624625
for _, tr := range testRoutes {
625-
if runtime.GOOS == "windows" && strings.HasPrefix(tr.route, "/../") {
626-
// See: https://github.com/valyala/fasthttp/issues/1226
627-
t.Logf("skipping route '%s %s' on %s, unsupported yet", reqMethod, tr.route, runtime.GOOS)
628-
629-
continue
630-
}
631-
632626
ctx := new(fasthttp.RequestCtx)
633-
634627
ctx.Request.Header.SetMethod(reqMethod)
635628
ctx.Request.SetRequestURI(tr.route)
636629
ctx.Request.SetHost(host)
630+
637631
router.Handler(ctx)
638632

639633
statusCode := ctx.Response.StatusCode()
640634
location := string(ctx.Response.Header.Peek("Location"))
635+
641636
if !(statusCode == tr.code && (statusCode == fasthttp.StatusNotFound || location == tr.location)) {
642-
t.Errorf("NotFound handling route %s failed: ReqMethod=%s, Code=%d, Header=%v", method, tr.route, statusCode, location)
637+
fn := t.Errorf
638+
msg := "NotFound handling route '%s' failed: Method=%s, ReqMethod=%s, Code=%d, ExpectedCode=%d, Header=%v"
639+
640+
if runtime.GOOS == "windows" && strings.HasPrefix(tr.route, "/../") {
641+
// See: https://github.com/valyala/fasthttp/issues/1226
642+
// Not fail, because it is a known issue.
643+
fn = t.Logf
644+
msg = "ERROR: " + msg
645+
}
646+
647+
fn(msg, tr.route, method, reqMethod, statusCode, tr.code, location)
643648
}
644649
}
645650

@@ -655,9 +660,14 @@ func testRouterNotFoundByMethod(t *testing.T, method string) {
655660
ctx.Request.Header.SetMethod(reqMethod)
656661
ctx.Request.SetRequestURI("/nope")
657662
router.Handler(ctx)
663+
658664
if !(ctx.Response.StatusCode() == fasthttp.StatusNotFound && notFound == true) {
659-
t.Errorf("Custom NotFound handler failed: Code=%d, Header=%v", ctx.Response.StatusCode(), ctx.Response.Header.String())
665+
t.Errorf(
666+
"Custom NotFound handling failed: Method=%s, ReqMethod=%s, Code=%d, Header=%v",
667+
method, reqMethod, ctx.Response.StatusCode(), ctx.Response.Header.String(),
668+
)
660669
}
670+
661671
ctx.Response.Reset()
662672
}
663673

0 commit comments

Comments
 (0)