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

Function TestRouteMultiLevelBacktracking

router_test.go:1056–1115  ·  view source on GitHub ↗

Issue #1754 - router needs to backtrack multiple levels upwards in tree to find the matching route route evaluation order Routes: 1) /a/:b/c 2) /a/c/d 3) /a/c/df 4) /a/*/f 5) /:e/c/f 6) /* Searching route for "/a/c/f" should match "/a/*/f" When route `4) /a/*/f` is not added then request for "/a

(t *testing.T)

Source from the content-addressed store, hash-verified

1054// | "f" (static) | | "/c" (static) | | "/f" (static) |
1055// +--------------+ +---------------+ +---------------+
1056func TestRouteMultiLevelBacktracking(t *testing.T) {
1057 var testCases = []struct {
1058 expectRoute any
1059 expectParam map[string]string
1060 name string
1061 whenURL string
1062 }{
1063 {
1064 name: "route /a/c/df to /a/c/df",
1065 whenURL: "/a/c/df",
1066 expectRoute: "/a/c/df",
1067 },
1068 {
1069 name: "route /a/x/df to /a/:b/c",
1070 whenURL: "/a/x/c",
1071 expectRoute: "/a/:b/c",
1072 expectParam: map[string]string{"b": "x"},
1073 },
1074 {
1075 name: "route /a/x/f to /a/*/f",
1076 whenURL: "/a/x/f",
1077 expectRoute: "/a/*/f",
1078 expectParam: map[string]string{"*": "x/f"}, // NOTE: `x` would be probably more suitable
1079 },
1080 {
1081 name: "route /b/c/f to /:e/c/f",
1082 whenURL: "/b/c/f",
1083 expectRoute: "/:e/c/f",
1084 expectParam: map[string]string{"e": "b"},
1085 },
1086 {
1087 name: "route /b/c/c to /*",
1088 whenURL: "/b/c/c",
1089 expectRoute: "/*",
1090 expectParam: map[string]string{"*": "b/c/c"},
1091 },
1092 }
1093
1094 for _, tc := range testCases {
1095 t.Run(tc.name, func(t *testing.T) {
1096 e := New()
1097
1098 e.GET("/a/:b/c", handlerFunc)
1099 e.GET("/a/c/d", handlerFunc)
1100 e.GET("/a/c/df", handlerFunc)
1101 e.GET("/a/*/f", handlerFunc)
1102 e.GET("/:e/c/f", handlerFunc)
1103 e.GET("/*", handlerFunc)
1104
1105 c := e.NewContext(httptest.NewRequest(http.MethodGet, tc.whenURL, nil), nil)
1106 _ = e.router.Route(c)
1107
1108 assert.Equal(t, tc.expectRoute, c.Path())
1109 for param, expectedValue := range tc.expectParam {
1110 assert.Equal(t, expectedValue, c.pathValues.GetOr(param, "---none---"))
1111 }
1112 checkUnusedParamValues(t, c, tc.expectParam)
1113 })

Callers

nothing calls this directly

Calls 7

PathMethod · 0.95
NewFunction · 0.85
checkUnusedParamValuesFunction · 0.85
NewContextMethod · 0.80
GetOrMethod · 0.80
RouteMethod · 0.65
GETMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…