(t *testing.T)
| 566 | } |
| 567 | |
| 568 | func TestRouterNotFound(t *testing.T) { |
| 569 | router := New() |
| 570 | router.RedirectFixedPath = true |
| 571 | router.GET("/path", func(c *Context) {}) |
| 572 | router.GET("/dir/", func(c *Context) {}) |
| 573 | router.GET("/", func(c *Context) {}) |
| 574 | |
| 575 | testRoutes := []struct { |
| 576 | route string |
| 577 | code int |
| 578 | location string |
| 579 | }{ |
| 580 | {"/path/", http.StatusMovedPermanently, "/path"}, // TSR -/ |
| 581 | {"/dir", http.StatusMovedPermanently, "/dir/"}, // TSR +/ |
| 582 | {"/PATH", http.StatusMovedPermanently, "/path"}, // Fixed Case |
| 583 | {"/DIR/", http.StatusMovedPermanently, "/dir/"}, // Fixed Case |
| 584 | {"/PATH/", http.StatusMovedPermanently, "/path"}, // Fixed Case -/ |
| 585 | {"/DIR", http.StatusMovedPermanently, "/dir/"}, // Fixed Case +/ |
| 586 | {"/../path", http.StatusMovedPermanently, "/path"}, // Without CleanPath |
| 587 | {"/nope", http.StatusNotFound, ""}, // NotFound |
| 588 | } |
| 589 | for _, tr := range testRoutes { |
| 590 | w := PerformRequest(router, http.MethodGet, tr.route) |
| 591 | assert.Equal(t, tr.code, w.Code) |
| 592 | if w.Code != http.StatusNotFound { |
| 593 | assert.Equal(t, tr.location, w.Header().Get("Location")) |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | // Test custom not found handler |
| 598 | var notFound bool |
| 599 | router.NoRoute(func(c *Context) { |
| 600 | c.AbortWithStatus(http.StatusNotFound) |
| 601 | notFound = true |
| 602 | }) |
| 603 | w := PerformRequest(router, http.MethodGet, "/nope") |
| 604 | assert.Equal(t, http.StatusNotFound, w.Code) |
| 605 | assert.True(t, notFound) |
| 606 | |
| 607 | // Test other method than GET (want 307 instead of 301) |
| 608 | router.PATCH("/path", func(c *Context) {}) |
| 609 | w = PerformRequest(router, http.MethodPatch, "/path/") |
| 610 | assert.Equal(t, http.StatusTemporaryRedirect, w.Code) |
| 611 | assert.Equal(t, "map[Location:[/path]]", fmt.Sprint(w.Header())) |
| 612 | |
| 613 | // Test special case where no node for the prefix "/" exists |
| 614 | router = New() |
| 615 | router.GET("/a", func(c *Context) {}) |
| 616 | w = PerformRequest(router, http.MethodGet, "/") |
| 617 | assert.Equal(t, http.StatusNotFound, w.Code) |
| 618 | |
| 619 | // Reproduction test for the bug of issue #2843 |
| 620 | router = New() |
| 621 | router.NoRoute(func(c *Context) { |
| 622 | if c.Request.RequestURI == "/login" { |
| 623 | c.String(http.StatusOK, "login") |
| 624 | } |
| 625 | }) |
nothing calls this directly
no test coverage detected