-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
Copy pathrewrite_test.go
264 lines (230 loc) · 7.84 KB
/
rewrite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
// SPDX-License-Identifier: MIT
// SPDX-FileCopyrightText: © 2015 LabStack LLC and Echo contributors
package middleware
import (
"io"
"net/http"
"net/http/httptest"
"net/url"
"regexp"
"testing"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestRewriteAfterRouting(t *testing.T) {
e := echo.New()
// middlewares added with `Use()` are executed after routing is done and do not affect which route handler is matched
e.Use(RewriteWithConfig(RewriteConfig{
Rules: map[string]string{
"/old": "/new",
"/api/*": "/$1",
"/js/*": "/public/javascripts/$1",
"/users/*/orders/*": "/user/$1/order/$2",
},
}))
e.GET("/public/*", func(c echo.Context) error {
return c.String(http.StatusOK, c.Param("*"))
})
e.GET("/*", func(c echo.Context) error {
return c.String(http.StatusOK, c.Param("*"))
})
var testCases = []struct {
whenPath string
expectRoutePath string
expectRequestPath string
expectRequestRawPath string
}{
{
whenPath: "/api/users",
expectRoutePath: "api/users",
expectRequestPath: "/users",
expectRequestRawPath: "",
},
{
whenPath: "/js/main.js",
expectRoutePath: "js/main.js",
expectRequestPath: "/public/javascripts/main.js",
expectRequestRawPath: "",
},
{
whenPath: "/users/jack/orders/1",
expectRoutePath: "users/jack/orders/1",
expectRequestPath: "/user/jack/order/1",
expectRequestRawPath: "",
},
{ // no rewrite rule matched. already encoded URL should not be double encoded or changed in any way
whenPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
expectRoutePath: "user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
expectRequestPath: "/user/jill/order/T/cO4lW/t/Vp/", // this is equal to `url.Parse(tc.whenPath)` result
expectRequestRawPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
},
{ // just rewrite but do not touch encoding. already encoded URL should not be double encoded
whenPath: "/users/jill/orders/T%2FcO4lW%2Ft%2FVp%2F",
expectRoutePath: "users/jill/orders/T%2FcO4lW%2Ft%2FVp%2F",
expectRequestPath: "/user/jill/order/T/cO4lW/t/Vp/", // this is equal to `url.Parse(tc.whenPath)` result
expectRequestRawPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
},
{ // ` ` (space) is encoded by httpClient to `%20` when doing request to Echo. `%20` should not be double escaped or changed in any way when rewriting request
whenPath: "/api/new users",
expectRoutePath: "api/new users",
expectRequestPath: "/new users",
expectRequestRawPath: "",
},
}
for _, tc := range testCases {
t.Run(tc.whenPath, func(t *testing.T) {
target, _ := url.Parse(tc.whenPath)
req := httptest.NewRequest(http.MethodGet, target.String(), nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Equal(t, tc.expectRoutePath, rec.Body.String())
assert.Equal(t, tc.expectRequestPath, req.URL.Path)
assert.Equal(t, tc.expectRequestRawPath, req.URL.RawPath)
})
}
}
// Issue #1086
func TestEchoRewritePreMiddleware(t *testing.T) {
e := echo.New()
r := e.Router()
// Rewrite old url to new one
// middlewares added with `Pre()` are executed before routing is done and therefore change which handler matches
e.Pre(Rewrite(map[string]string{
"/old": "/new",
},
))
// Route
r.Add(http.MethodGet, "/new", func(c echo.Context) error {
return c.NoContent(http.StatusOK)
})
req := httptest.NewRequest(http.MethodGet, "/old", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, "/new", req.URL.EscapedPath())
assert.Equal(t, http.StatusOK, rec.Code)
}
// Issue #1143
func TestRewriteWithConfigPreMiddleware_Issue1143(t *testing.T) {
e := echo.New()
r := e.Router()
// middlewares added with `Pre()` are executed before routing is done and therefore change which handler matches
e.Pre(RewriteWithConfig(RewriteConfig{
Rules: map[string]string{
"/api/*/mgmt/proj/*/agt": "/api/$1/hosts/$2",
"/api/*/mgmt/proj": "/api/$1/eng",
},
}))
r.Add(http.MethodGet, "/api/:version/hosts/:name", func(c echo.Context) error {
return c.String(http.StatusOK, "hosts")
})
r.Add(http.MethodGet, "/api/:version/eng", func(c echo.Context) error {
return c.String(http.StatusOK, "eng")
})
for i := 0; i < 100; i++ {
req := httptest.NewRequest(http.MethodGet, "/api/v1/mgmt/proj/test/agt", nil)
rec := httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, "/api/v1/hosts/test", req.URL.EscapedPath())
assert.Equal(t, http.StatusOK, rec.Code)
defer rec.Result().Body.Close()
bodyBytes, _ := io.ReadAll(rec.Result().Body)
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)
}
// Verify regex used with rewrite
func TestEchoRewriteWithRegexRules(t *testing.T) {
e := echo.New()
e.Pre(RewriteWithConfig(RewriteConfig{
Rules: map[string]string{
"^/a/*": "/v1/$1",
"^/b/*/c/*": "/v2/$2/$1",
"^/c/*/*": "/v3/$2",
},
RegexRules: map[*regexp.Regexp]string{
regexp.MustCompile("^/x/.+?/(.*)"): "/v4/$1",
regexp.MustCompile("^/y/(.+?)/(.*)"): "/v5/$2/$1",
},
}))
var rec *httptest.ResponseRecorder
var req *http.Request
testCases := []struct {
requestPath string
expectPath string
}{
{"/unmatched", "/unmatched"},
{"/a/test", "/v1/test"},
{"/b/foo/c/bar/baz", "/v2/bar/baz/foo"},
{"/c/ignore/test", "/v3/test"},
{"/c/ignore1/test/this", "/v3/test/this"},
{"/x/ignore/test", "/v4/test"},
{"/y/foo/bar", "/v5/bar/foo"},
}
for _, tc := range testCases {
t.Run(tc.requestPath, func(t *testing.T) {
req = httptest.NewRequest(http.MethodGet, tc.requestPath, nil)
rec = httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, tc.expectPath, req.URL.EscapedPath())
})
}
}
// Ensure correct escaping as defined in replacement (issue #1798)
func TestEchoRewriteReplacementEscaping(t *testing.T) {
e := echo.New()
// NOTE: these are incorrect regexps as they do not factor in that URI we are replacing could contain ? (query) and # (fragment) parts
// so in reality they append query and fragment part as `$1` matches everything after that prefix
e.Pre(RewriteWithConfig(RewriteConfig{
Rules: map[string]string{
"^/a/*": "/$1?query=param",
"^/b/*": "/$1;part#one",
},
RegexRules: map[*regexp.Regexp]string{
regexp.MustCompile("^/x/(.*)"): "/$1?query=param",
regexp.MustCompile("^/y/(.*)"): "/$1;part#one",
regexp.MustCompile("^/z/(.*)"): "/$1?test=1#escaped%20test",
},
}))
var rec *httptest.ResponseRecorder
var req *http.Request
testCases := []struct {
requestPath string
expect string
}{
{"/unmatched", "/unmatched"},
{"/a/test", "/test?query=param"},
{"/b/foo/bar", "/foo/bar;part#one"},
{"/x/test", "/test?query=param"},
{"/y/foo/bar", "/foo/bar;part#one"},
{"/z/foo/b%20ar", "/foo/b%20ar?test=1#escaped%20test"},
{"/z/foo/b%20ar?nope=1#yes", "/foo/b%20ar?nope=1#yes?test=1%23escaped%20test"}, // example of appending
}
for _, tc := range testCases {
t.Run(tc.requestPath, func(t *testing.T) {
req = httptest.NewRequest(http.MethodGet, tc.requestPath, nil)
rec = httptest.NewRecorder()
e.ServeHTTP(rec, req)
assert.Equal(t, tc.expect, req.URL.String())
})
}
}