MCPcopy Index your code
hub / github.com/labstack/echo / TestEchoRewriteWithRegexRules

Function TestEchoRewriteWithRegexRules

middleware/rewrite_test.go:234–273  ·  view source on GitHub ↗

Verify regex used with rewrite

(t *testing.T)

Source from the content-addressed store, hash-verified

232
233// Verify regex used with rewrite
234func TestEchoRewriteWithRegexRules(t *testing.T) {
235 e := echo.New()
236
237 e.Pre(RewriteWithConfig(RewriteConfig{
238 Rules: map[string]string{
239 "^/a/*": "/v1/$1",
240 "^/b/*/c/*": "/v2/$2/$1",
241 "^/c/*/*": "/v3/$2",
242 },
243 RegexRules: map[*regexp.Regexp]string{
244 regexp.MustCompile("^/x/.+?/(.*)"): "/v4/$1",
245 regexp.MustCompile("^/y/(.+?)/(.*)"): "/v5/$2/$1",
246 },
247 }))
248
249 var rec *httptest.ResponseRecorder
250 var req *http.Request
251
252 testCases := []struct {
253 requestPath string
254 expectPath string
255 }{
256 {"/unmatched", "/unmatched"},
257 {"/a/test", "/v1/test"},
258 {"/b/foo/c/bar/baz", "/v2/bar/baz/foo"},
259 {"/c/ignore/test", "/v3/test"},
260 {"/c/ignore1/test/this", "/v3/test/this"},
261 {"/x/ignore/test", "/v4/test"},
262 {"/y/foo/bar", "/v5/bar/foo"},
263 }
264
265 for _, tc := range testCases {
266 t.Run(tc.requestPath, func(t *testing.T) {
267 req = httptest.NewRequest(http.MethodGet, tc.requestPath, nil)
268 rec = httptest.NewRecorder()
269 e.ServeHTTP(rec, req)
270 assert.Equal(t, tc.expectPath, req.URL.EscapedPath())
271 })
272 }
273}
274
275// Ensure correct escaping as defined in replacement (issue #1798)
276func TestEchoRewriteReplacementEscaping(t *testing.T) {

Callers

nothing calls this directly

Calls 3

RewriteWithConfigFunction · 0.85
PreMethod · 0.80
ServeHTTPMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…