MCPcopy
hub / github.com/labstack/echo / TestRewriteAfterRouting

Function TestRewriteAfterRouting

middleware/rewrite_test.go:18–94  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

16)
17
18func TestRewriteAfterRouting(t *testing.T) {
19 e := echo.New()
20 // middlewares added with `Use()` are executed after routing is done and do not affect which route handler is matched
21 e.Use(RewriteWithConfig(RewriteConfig{
22 Rules: map[string]string{
23 "/old": "/new",
24 "/api/*": "/$1",
25 "/js/*": "/public/javascripts/$1",
26 "/users/*/orders/*": "/user/$1/order/$2",
27 },
28 }))
29 e.GET("/public/*", func(c *echo.Context) error {
30 return c.String(http.StatusOK, c.Param("*"))
31 })
32 e.GET("/*", func(c *echo.Context) error {
33 return c.String(http.StatusOK, c.Param("*"))
34 })
35
36 var testCases = []struct {
37 whenPath string
38 expectRoutePath string
39 expectRequestPath string
40 expectRequestRawPath string
41 }{
42 {
43 whenPath: "/api/users",
44 expectRoutePath: "api/users",
45 expectRequestPath: "/users",
46 expectRequestRawPath: "",
47 },
48 {
49 whenPath: "/js/main.js",
50 expectRoutePath: "js/main.js",
51 expectRequestPath: "/public/javascripts/main.js",
52 expectRequestRawPath: "",
53 },
54 {
55 whenPath: "/users/jack/orders/1",
56 expectRoutePath: "users/jack/orders/1",
57 expectRequestPath: "/user/jack/order/1",
58 expectRequestRawPath: "",
59 },
60 { // no rewrite rule matched. already encoded URL should not be double encoded or changed in any way
61 whenPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
62 expectRoutePath: "user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
63 expectRequestPath: "/user/jill/order/T/cO4lW/t/Vp/", // this is equal to `url.Parse(tc.whenPath)` result
64 expectRequestRawPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
65 },
66 { // just rewrite but do not touch encoding. already encoded URL should not be double encoded
67 whenPath: "/users/jill/orders/T%2FcO4lW%2Ft%2FVp%2F",
68 expectRoutePath: "users/jill/orders/T%2FcO4lW%2Ft%2FVp%2F",
69 expectRequestPath: "/user/jill/order/T/cO4lW/t/Vp/", // this is equal to `url.Parse(tc.whenPath)` result
70 expectRequestRawPath: "/user/jill/order/T%2FcO4lW%2Ft%2FVp%2F",
71 },
72 { // ` ` (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
73 whenPath: "/api/new users",
74 expectRoutePath: "api/new users",
75 expectRequestPath: "/new users",

Callers

nothing calls this directly

Calls 6

RewriteWithConfigFunction · 0.85
ParamMethod · 0.80
ServeHTTPMethod · 0.80
UseMethod · 0.45
GETMethod · 0.45
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…