File tree Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Expand file tree Collapse file tree 2 files changed +30
-0
lines changed Original file line number Diff line number Diff line change @@ -59,6 +59,9 @@ func RewriteWithConfig(config RewriteConfig) echo.MiddlewareFunc {
59
59
for k , v := range config .Rules {
60
60
k = regexp .QuoteMeta (k )
61
61
k = strings .Replace (k , `\*` , "(.*)" , - 1 )
62
+ if strings .HasPrefix (k , `\^` ) {
63
+ k = strings .Replace (k , `\^` , "^" , - 1 )
64
+ }
62
65
k = k + "$"
63
66
config .rulesRegex [regexp .MustCompile (k )] = v
64
67
}
Original file line number Diff line number Diff line change @@ -94,3 +94,30 @@ func TestRewriteWithConfigPreMiddleware_Issue1143(t *testing.T) {
94
94
assert .Equal (t , "hosts" , string (bodyBytes ))
95
95
}
96
96
}
97
+
98
+ // Issue #1573
99
+ func TestEchoRewriteWithCaret (t * testing.T ) {
100
+ e := echo .New ()
101
+
102
+ e .Pre (RewriteWithConfig (RewriteConfig {
103
+ Rules : map [string ]string {
104
+ "^/abc/*" : "/v1/abc/$1" ,
105
+ },
106
+ }))
107
+
108
+ rec := httptest .NewRecorder ()
109
+
110
+ var req * http.Request
111
+
112
+ req = httptest .NewRequest (http .MethodGet , "/abc/test" , nil )
113
+ e .ServeHTTP (rec , req )
114
+ assert .Equal (t , "/v1/abc/test" , req .URL .Path )
115
+
116
+ req = httptest .NewRequest (http .MethodGet , "/v1/abc/test" , nil )
117
+ e .ServeHTTP (rec , req )
118
+ assert .Equal (t , "/v1/abc/test" , req .URL .Path )
119
+
120
+ req = httptest .NewRequest (http .MethodGet , "/v2/abc/test" , nil )
121
+ e .ServeHTTP (rec , req )
122
+ assert .Equal (t , "/v2/abc/test" , req .URL .Path )
123
+ }
You can’t perform that action at this time.
0 commit comments