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

Function TestRouterBacktrackingFromMultipleParamKinds

router_test.go:1216–1279  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1214}
1215
1216func TestRouterBacktrackingFromMultipleParamKinds(t *testing.T) {
1217 e := New()
1218
1219 e.GET("/*", handlerFunc) // this can match only path that does not have slash in it
1220 e.GET("/:1/second", handlerFunc)
1221 e.GET("/:1/:2", handlerFunc) // this acts as match ANY for all routes that have at least one slash
1222 e.GET("/:1/:2/third", handlerFunc)
1223 e.GET("/:1/:2/:3/fourth", handlerFunc)
1224 e.GET("/:1/:2/:3/:4/fifth", handlerFunc)
1225
1226 var testCases = []struct {
1227 expectParam map[string]string
1228 name string
1229 whenURL string
1230 expectRoute string
1231 }{
1232 {
1233 name: "route /first to /*",
1234 whenURL: "/first",
1235 expectRoute: "/*",
1236 expectParam: map[string]string{"*": "first"},
1237 },
1238 {
1239 name: "route /first/second to /:1/second",
1240 whenURL: "/first/second",
1241 expectRoute: "/:1/second",
1242 expectParam: map[string]string{"1": "first"},
1243 },
1244 {
1245 name: "route /first/second-new to /:1/:2",
1246 whenURL: "/first/second-new",
1247 expectRoute: "/:1/:2",
1248 expectParam: map[string]string{
1249 "1": "first",
1250 "2": "second-new",
1251 },
1252 },
1253 { // FIXME: should match `/:1/:2` when backtracking in tree. this 1 level backtracking fails even with old implementation
1254 name: "route /first/second/ to /:1/:2",
1255 whenURL: "/first/second/", /// <-- slash at the end is problematic
1256 expectRoute: "/*", // "/:1/:2",
1257 expectParam: map[string]string{"*": "first/second/"}, // map[string]string{"1": "first", "2": "second/"},
1258 },
1259 { // FIXME: should match `/:1/:2`. same backtracking problem. when backtracking is at `/:1/:2` during backtracking this node should be match as it has executable handler
1260 name: "route /first/second/third/fourth/fifth/nope to /:1/:2",
1261 whenURL: "/first/second/third/fourth/fifth/nope",
1262 expectRoute: "/*", // "/:1/:2",
1263 expectParam: map[string]string{"*": "first/second/third/fourth/fifth/nope"}, // map[string]string{"1": "first", "2": "second/third/fourth/fifth/nope"},
1264 },
1265 }
1266
1267 for _, tc := range testCases {
1268 t.Run(tc.name, func(t *testing.T) {
1269 c := e.NewContext(httptest.NewRequest(http.MethodGet, tc.whenURL, nil), nil)
1270 _ = e.router.Route(c)
1271
1272 assert.Equal(t, tc.expectRoute, c.Path())
1273 for param, expectedValue := range tc.expectParam {

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…