Ensure correct escaping as defined in replacement (issue #1798)
(t *testing.T)
| 274 | |
| 275 | // Ensure correct escaping as defined in replacement (issue #1798) |
| 276 | func TestEchoRewriteReplacementEscaping(t *testing.T) { |
| 277 | e := echo.New() |
| 278 | |
| 279 | // NOTE: these are incorrect regexps as they do not factor in that URI we are replacing could contain ? (query) and # (fragment) parts |
| 280 | // so in reality they append query and fragment part as `$1` matches everything after that prefix |
| 281 | e.Pre(RewriteWithConfig(RewriteConfig{ |
| 282 | Rules: map[string]string{ |
| 283 | "^/a/*": "/$1?query=param", |
| 284 | "^/b/*": "/$1;part#one", |
| 285 | }, |
| 286 | RegexRules: map[*regexp.Regexp]string{ |
| 287 | regexp.MustCompile("^/x/(.*)"): "/$1?query=param", |
| 288 | regexp.MustCompile("^/y/(.*)"): "/$1;part#one", |
| 289 | regexp.MustCompile("^/z/(.*)"): "/$1?test=1#escaped%20test", |
| 290 | }, |
| 291 | })) |
| 292 | |
| 293 | var rec *httptest.ResponseRecorder |
| 294 | var req *http.Request |
| 295 | |
| 296 | testCases := []struct { |
| 297 | requestPath string |
| 298 | expect string |
| 299 | }{ |
| 300 | {"/unmatched", "/unmatched"}, |
| 301 | {"/a/test", "/test?query=param"}, |
| 302 | {"/b/foo/bar", "/foo/bar;part#one"}, |
| 303 | {"/x/test", "/test?query=param"}, |
| 304 | {"/y/foo/bar", "/foo/bar;part#one"}, |
| 305 | {"/z/foo/b%20ar", "/foo/b%20ar?test=1#escaped%20test"}, |
| 306 | {"/z/foo/b%20ar?nope=1#yes", "/foo/b%20ar?nope=1#yes?test=1%23escaped%20test"}, // example of appending |
| 307 | } |
| 308 | |
| 309 | for _, tc := range testCases { |
| 310 | t.Run(tc.requestPath, func(t *testing.T) { |
| 311 | req = httptest.NewRequest(http.MethodGet, tc.requestPath, nil) |
| 312 | rec = httptest.NewRecorder() |
| 313 | e.ServeHTTP(rec, req) |
| 314 | assert.Equal(t, tc.expect, req.URL.String()) |
| 315 | }) |
| 316 | } |
| 317 | } |
nothing calls this directly
no test coverage detected
searching dependent graphs…