mirror of
https://github.com/labstack/echo.git
synced 2024-12-24 20:14:31 +02:00
Fixes the uses of caret(^) at the beginning of the rewrite regex
This commit is contained in:
parent
68e8bce645
commit
3dbd5dcf6e
@ -59,6 +59,9 @@ func RewriteWithConfig(config RewriteConfig) echo.MiddlewareFunc {
|
||||
for k, v := range config.Rules {
|
||||
k = regexp.QuoteMeta(k)
|
||||
k = strings.Replace(k, `\*`, "(.*)", -1)
|
||||
if strings.HasPrefix(k, `\^`) {
|
||||
k = strings.Replace(k, `\^`, "^", -1)
|
||||
}
|
||||
k = k + "$"
|
||||
config.rulesRegex[regexp.MustCompile(k)] = v
|
||||
}
|
||||
|
@ -94,3 +94,30 @@ func TestRewriteWithConfigPreMiddleware_Issue1143(t *testing.T) {
|
||||
assert.Equal(t, "hosts", string(bodyBytes))
|
||||
}
|
||||
}
|
||||
|
||||
// Issue #1573
|
||||
func TestEchoRewriteWithCaret(t *testing.T) {
|
||||
e := echo.New()
|
||||
|
||||
e.Pre(RewriteWithConfig(RewriteConfig{
|
||||
Rules: map[string]string{
|
||||
"^/abc/*": "/v1/abc/$1",
|
||||
},
|
||||
}))
|
||||
|
||||
rec := httptest.NewRecorder()
|
||||
|
||||
var req *http.Request
|
||||
|
||||
req = httptest.NewRequest(http.MethodGet, "/abc/test", nil)
|
||||
e.ServeHTTP(rec, req)
|
||||
assert.Equal(t, "/v1/abc/test", req.URL.Path)
|
||||
|
||||
req = httptest.NewRequest(http.MethodGet, "/v1/abc/test", nil)
|
||||
e.ServeHTTP(rec, req)
|
||||
assert.Equal(t, "/v1/abc/test", req.URL.Path)
|
||||
|
||||
req = httptest.NewRequest(http.MethodGet, "/v2/abc/test", nil)
|
||||
e.ServeHTTP(rec, req)
|
||||
assert.Equal(t, "/v2/abc/test", req.URL.Path)
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user