Adding upstream version 2.52.6.
Signed-off-by: Daniel Baumann <daniel@debian.org>
This commit is contained in:
parent
a960158181
commit
6d002e9543
441 changed files with 95392 additions and 0 deletions
38
middleware/rewrite/config.go
Normal file
38
middleware/rewrite/config.go
Normal file
|
@ -0,0 +1,38 @@
|
|||
package rewrite
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// Config defines the config for middleware.
|
||||
type Config struct {
|
||||
// Next defines a function to skip middleware.
|
||||
// Optional. Default: nil
|
||||
Next func(*fiber.Ctx) bool
|
||||
|
||||
// Rules defines the URL path rewrite rules. The values captured in asterisk can be
|
||||
// retrieved by index e.g. $1, $2 and so on.
|
||||
// Required. Example:
|
||||
// "/old": "/new",
|
||||
// "/api/*": "/$1",
|
||||
// "/js/*": "/public/javascripts/$1",
|
||||
// "/users/*/orders/*": "/user/$1/order/$2",
|
||||
Rules map[string]string
|
||||
|
||||
rulesRegex map[*regexp.Regexp]string
|
||||
}
|
||||
|
||||
// Helper function to set default values
|
||||
func configDefault(config ...Config) Config {
|
||||
// Return default config if nothing provided
|
||||
if len(config) < 1 {
|
||||
return Config{}
|
||||
}
|
||||
|
||||
// Override default config
|
||||
cfg := config[0]
|
||||
|
||||
return cfg
|
||||
}
|
54
middleware/rewrite/rewrite.go
Normal file
54
middleware/rewrite/rewrite.go
Normal file
|
@ -0,0 +1,54 @@
|
|||
package rewrite
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
)
|
||||
|
||||
// New creates a new middleware handler
|
||||
func New(config ...Config) fiber.Handler {
|
||||
cfg := configDefault(config...)
|
||||
|
||||
// Initialize
|
||||
cfg.rulesRegex = map[*regexp.Regexp]string{}
|
||||
for k, v := range cfg.Rules {
|
||||
k = strings.ReplaceAll(k, "*", "(.*)")
|
||||
k += "$"
|
||||
cfg.rulesRegex[regexp.MustCompile(k)] = v
|
||||
}
|
||||
// Middleware function
|
||||
return func(c *fiber.Ctx) error {
|
||||
// Next request to skip middleware
|
||||
if cfg.Next != nil && cfg.Next(c) {
|
||||
return c.Next()
|
||||
}
|
||||
// Rewrite
|
||||
for k, v := range cfg.rulesRegex {
|
||||
replacer := captureTokens(k, c.Path())
|
||||
if replacer != nil {
|
||||
c.Path(replacer.Replace(v))
|
||||
break
|
||||
}
|
||||
}
|
||||
return c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// https://github.com/labstack/echo/blob/master/middleware/rewrite.go
|
||||
func captureTokens(pattern *regexp.Regexp, input string) *strings.Replacer {
|
||||
groups := pattern.FindAllStringSubmatch(input, -1)
|
||||
if groups == nil {
|
||||
return nil
|
||||
}
|
||||
values := groups[0][1:]
|
||||
replace := make([]string, 2*len(values))
|
||||
for i, v := range values {
|
||||
j := 2 * i
|
||||
replace[j] = "$" + strconv.Itoa(i+1)
|
||||
replace[j+1] = v
|
||||
}
|
||||
return strings.NewReplacer(replace...)
|
||||
}
|
173
middleware/rewrite/rewrite_test.go
Normal file
173
middleware/rewrite/rewrite_test.go
Normal file
|
@ -0,0 +1,173 @@
|
|||
//nolint:bodyclose // Much easier to just ignore memory leaks in tests
|
||||
package rewrite
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/gofiber/fiber/v2"
|
||||
"github.com/gofiber/fiber/v2/utils"
|
||||
)
|
||||
|
||||
func Test_New(t *testing.T) {
|
||||
// Test with no config
|
||||
m := New()
|
||||
|
||||
if m == nil {
|
||||
t.Error("Expected middleware to be returned, got nil")
|
||||
}
|
||||
|
||||
// Test with config
|
||||
m = New(Config{
|
||||
Rules: map[string]string{
|
||||
"/old": "/new",
|
||||
},
|
||||
})
|
||||
|
||||
if m == nil {
|
||||
t.Error("Expected middleware to be returned, got nil")
|
||||
}
|
||||
|
||||
// Test with full config
|
||||
m = New(Config{
|
||||
Next: func(*fiber.Ctx) bool {
|
||||
return true
|
||||
},
|
||||
Rules: map[string]string{
|
||||
"/old": "/new",
|
||||
},
|
||||
})
|
||||
|
||||
if m == nil {
|
||||
t.Error("Expected middleware to be returned, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func Test_Rewrite(t *testing.T) {
|
||||
// Case 1: Next function always returns true
|
||||
app := fiber.New()
|
||||
app.Use(New(Config{
|
||||
Next: func(*fiber.Ctx) bool {
|
||||
return true
|
||||
},
|
||||
Rules: map[string]string{
|
||||
"/old": "/new",
|
||||
},
|
||||
}))
|
||||
|
||||
app.Get("/old", func(c *fiber.Ctx) error {
|
||||
return c.SendString("Rewrite Successful")
|
||||
})
|
||||
|
||||
req, err := http.NewRequestWithContext(context.Background(), fiber.MethodGet, "/old", nil)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
resp, err := app.Test(req)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
body, err := io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
bodyString := string(body)
|
||||
|
||||
utils.AssertEqual(t, err, nil)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
utils.AssertEqual(t, "Rewrite Successful", bodyString)
|
||||
|
||||
// Case 2: Next function always returns false
|
||||
app = fiber.New()
|
||||
app.Use(New(Config{
|
||||
Next: func(*fiber.Ctx) bool {
|
||||
return false
|
||||
},
|
||||
Rules: map[string]string{
|
||||
"/old": "/new",
|
||||
},
|
||||
}))
|
||||
|
||||
app.Get("/new", func(c *fiber.Ctx) error {
|
||||
return c.SendString("Rewrite Successful")
|
||||
})
|
||||
|
||||
req, err = http.NewRequestWithContext(context.Background(), fiber.MethodGet, "/old", nil)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
resp, err = app.Test(req)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
bodyString = string(body)
|
||||
|
||||
utils.AssertEqual(t, err, nil)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
utils.AssertEqual(t, "Rewrite Successful", bodyString)
|
||||
|
||||
// Case 3: check for captured tokens in rewrite rule
|
||||
app = fiber.New()
|
||||
app.Use(New(Config{
|
||||
Rules: map[string]string{
|
||||
"/users/*/orders/*": "/user/$1/order/$2",
|
||||
},
|
||||
}))
|
||||
|
||||
app.Get("/user/:userID/order/:orderID", func(c *fiber.Ctx) error {
|
||||
return c.SendString(fmt.Sprintf("User ID: %s, Order ID: %s", c.Params("userID"), c.Params("orderID")))
|
||||
})
|
||||
|
||||
req, err = http.NewRequestWithContext(context.Background(), fiber.MethodGet, "/users/123/orders/456", nil)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
resp, err = app.Test(req)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
bodyString = string(body)
|
||||
|
||||
utils.AssertEqual(t, err, nil)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
utils.AssertEqual(t, "User ID: 123, Order ID: 456", bodyString)
|
||||
|
||||
// Case 4: Send non-matching request, handled by default route
|
||||
app = fiber.New()
|
||||
app.Use(New(Config{
|
||||
Rules: map[string]string{
|
||||
"/users/*/orders/*": "/user/$1/order/$2",
|
||||
},
|
||||
}))
|
||||
|
||||
app.Get("/user/:userID/order/:orderID", func(c *fiber.Ctx) error {
|
||||
return c.SendString(fmt.Sprintf("User ID: %s, Order ID: %s", c.Params("userID"), c.Params("orderID")))
|
||||
})
|
||||
|
||||
app.Use(func(c *fiber.Ctx) error {
|
||||
return c.SendStatus(fiber.StatusOK)
|
||||
})
|
||||
|
||||
req, err = http.NewRequestWithContext(context.Background(), fiber.MethodGet, "/not-matching-any-rule", nil)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
resp, err = app.Test(req)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
body, err = io.ReadAll(resp.Body)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
bodyString = string(body)
|
||||
|
||||
utils.AssertEqual(t, err, nil)
|
||||
utils.AssertEqual(t, fiber.StatusOK, resp.StatusCode)
|
||||
utils.AssertEqual(t, "OK", bodyString)
|
||||
|
||||
// Case 4: Send non-matching request, with no default route
|
||||
app = fiber.New()
|
||||
app.Use(New(Config{
|
||||
Rules: map[string]string{
|
||||
"/users/*/orders/*": "/user/$1/order/$2",
|
||||
},
|
||||
}))
|
||||
|
||||
app.Get("/user/:userID/order/:orderID", func(c *fiber.Ctx) error {
|
||||
return c.SendString(fmt.Sprintf("User ID: %s, Order ID: %s", c.Params("userID"), c.Params("orderID")))
|
||||
})
|
||||
|
||||
req, err = http.NewRequestWithContext(context.Background(), fiber.MethodGet, "/not-matching-any-rule", nil)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
resp, err = app.Test(req)
|
||||
utils.AssertEqual(t, err, nil)
|
||||
utils.AssertEqual(t, fiber.StatusNotFound, resp.StatusCode)
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue