We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent d324506 commit 84da507Copy full SHA for 84da507
middleware/rewrite.go
@@ -59,7 +59,11 @@ func RewriteWithConfig(config RewriteConfig) echo.MiddlewareFunc {
59
for k, v := range config.Rules {
60
k = regexp.QuoteMeta(k)
61
k = strings.Replace(k, `\*`, "(.*)", -1)
62
+ k = strings.Replace(k, `\^`, "^", -1)
63
k = k + "$"
64
+ if strings.HasPrefix(k, "/") {
65
+ k = "^" + k
66
+ }
67
config.rulesRegex[regexp.MustCompile(k)] = v
68
}
69
middleware/rewrite_test.go
@@ -18,6 +18,10 @@ func TestRewrite(t *testing.T) {
18
"/api/*": "/$1",
19
"/js/*": "/public/javascripts/$1",
20
"/users/*/orders/*": "/user/$1/order/$2",
21
+ "/foo/*": "/v1/foo/$1",
22
+ "/v1/foo/*": "/v1/foo/$1",
23
+ "/v2/foo/*": "/v2/foo/$1",
24
+ "^/bar/*": "/foobar/$1",
25
},
26
}))
27
req := httptest.NewRequest(http.MethodGet, "/", nil)
@@ -37,6 +41,18 @@ func TestRewrite(t *testing.T) {
37
41
req.URL.Path = "/api/new users"
38
42
e.ServeHTTP(rec, req)
39
43
assert.Equal(t, "/new users", req.URL.Path)
44
+ req.URL.Path = "/foo/bar"
45
+ e.ServeHTTP(rec, req)
46
+ assert.Equal(t, "/v1/foo/bar", req.URL.Path)
47
+ req.URL.Path = "/v1/foo/bar"
48
49
50
+ req.URL.Path = "/v2/foo/bar"
51
52
+ assert.Equal(t, "/v2/foo/bar", req.URL.Path)
53
+ req.URL.Path = "/bar/baz"
54
55
+ assert.Equal(t, "/foobar/baz", req.URL.Path)
40
56
57
58
// Issue #1086
0 commit comments