Skip to content
This repository has been archived by the owner on May 19, 2023. It is now read-only.

Commit

Permalink
Merge pull request #21 from gofiber/fix-redirect-suffix-slash
Browse files Browse the repository at this point in the history
🐛  fix: remove slash when has a route greater than 1 length and ends with slash
  • Loading branch information
Fenny authored Nov 6, 2020
2 parents 1da386c + 4cf847f commit 868d386
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func New(config ...Config) fiber.Handler {

// https://github.com/labstack/echo/blob/master/middleware/rewrite.go
func captureTokens(pattern *regexp.Regexp, input string) *strings.Replacer {
if len(input) > 1 {
input = strings.TrimSuffix(input, "/")
}
groups := pattern.FindAllStringSubmatch(input, -1)
if groups == nil {
return nil
Expand Down
27 changes: 27 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,17 @@ func Test_Redirect(t *testing.T) {
StatusCode: 302,
}))

app.Use(New(Config{
Rules: map[string]string{
"/": "/swagger",
},
StatusCode: 301,
}))

app.Get("/api/*", func(c *fiber.Ctx) error {
return c.SendString("API")
})

app.Get("/new", func(c *fiber.Ctx) error {
return c.SendString("Hello, World!")
})
Expand Down Expand Up @@ -78,6 +89,22 @@ func Test_Redirect(t *testing.T) {
url: "/new",
statusCode: 200,
},
{
name: "redirect to swagger route",
url: "/",
redirectTo: "/swagger",
statusCode: 301,
},
{
name: "no redirect to swagger route",
url: "/api/",
statusCode: 200,
},
{
name: "no redirect to swagger route #2",
url: "/api/test",
statusCode: 200,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit 868d386

Please sign in to comment.