Skip to content

Commit

Permalink
🩹🚨 - fix for redirect with query params (#2748)
Browse files Browse the repository at this point in the history
* redirect with query params did not work, fix it and add test for it

* redirect middleware - fix test typo
  • Loading branch information
gilwo committed Dec 22, 2023
1 parent c49faf9 commit d6c8876
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
6 changes: 5 additions & 1 deletion middleware/redirect/redirect.go
Expand Up @@ -30,7 +30,11 @@ func New(config ...Config) fiber.Handler {
for k, v := range cfg.rulesRegex {
replacer := captureTokens(k, c.Path())
if replacer != nil {
return c.Redirect(replacer.Replace(v), cfg.StatusCode)
queryString := string(c.Context().QueryArgs().QueryString())
if queryString != "" {
queryString = "?" + queryString
}
return c.Redirect(replacer.Replace(v)+queryString, cfg.StatusCode)
}
}
return c.Next()
Expand Down
12 changes: 12 additions & 0 deletions middleware/redirect/redirect_test.go
Expand Up @@ -44,6 +44,12 @@ func Test_Redirect(t *testing.T) {
},
StatusCode: fiber.StatusMovedPermanently,
}))
app.Use(New(Config{
Rules: map[string]string{
"/params": "/with_params",
},
StatusCode: fiber.StatusMovedPermanently,
}))

app.Get("/api/*", func(c *fiber.Ctx) error {
return c.SendString("API")
Expand Down Expand Up @@ -104,6 +110,12 @@ func Test_Redirect(t *testing.T) {
url: "/api/test",
statusCode: fiber.StatusOK,
},
{
name: "redirect with query params",
url: "/params?query=abc",
redirectTo: "/with_params?query=abc",
statusCode: fiber.StatusMovedPermanently,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
Expand Down

0 comments on commit d6c8876

Please sign in to comment.