MCPcopy Create free account
hub / github.com/go-chi/chi / TestRoutePattern

Function TestRoutePattern

context_test.go:24–93  ·  view source on GitHub ↗

TestRoutePattern tests correct in-the-middle wildcard removals. If user organizes a router like this: (router.go) r.Route("/v1", func(r chi.Router) { r.Mount("/resources", resourcesController{}.Router()) } (resources_controller.go) r.Route("/", func(r chi.Router) { r.Get("/{resource_id}",

(t *testing.T)

Source from the content-addressed store, hash-verified

22// "/v1/resources/{resource_id}" (right)
23// "/v1/resources/*/{resource_id}" (wrong)
24func TestRoutePattern(t *testing.T) {
25 routePatterns := []string{
26 "/v1/*",
27 "/resources/*",
28 "/{resource_id}",
29 }
30
31 x := &Context{
32 RoutePatterns: routePatterns,
33 }
34
35 if p := x.RoutePattern(); p != "/v1/resources/{resource_id}" {
36 t.Fatal("unexpected route pattern: " + p)
37 }
38
39 x.RoutePatterns = []string{
40 "/v1/*",
41 "/resources/*",
42 // Additional wildcard, depending on the router structure of the user
43 "/*",
44 "/{resource_id}",
45 }
46
47 // Correctly removes in-the-middle wildcards instead of "/v1/resources/*/{resource_id}"
48 if p := x.RoutePattern(); p != "/v1/resources/{resource_id}" {
49 t.Fatal("unexpected route pattern: " + p)
50 }
51
52 x.RoutePatterns = []string{
53 "/v1/*",
54 "/resources/*",
55 // Even with many wildcards
56 "/*",
57 "/*",
58 "/*",
59 "/{resource_id}/*", // Keeping trailing wildcard
60 }
61
62 // Correctly removes in-the-middle wildcards instead of "/v1/resources/*/*/{resource_id}/*"
63 if p := x.RoutePattern(); p != "/v1/resources/{resource_id}/*" {
64 t.Fatal("unexpected route pattern: " + p)
65 }
66
67 x.RoutePatterns = []string{
68 "/v1/*",
69 "/resources/*",
70 // And respects asterisks as part of the paths
71 "/*special_path/*",
72 "/with_asterisks*/*",
73 "/{resource_id}",
74 }
75
76 // Correctly removes in-the-middle wildcards instead of "/v1/resourcesspecial_path/with_asterisks{resource_id}"
77 if p := x.RoutePattern(); p != "/v1/resources/*special_path/with_asterisks*/{resource_id}" {
78 t.Fatal("unexpected route pattern: " + p)
79 }
80
81 // Testing for the root route pattern

Callers

nothing calls this directly

Calls 1

RoutePatternMethod · 0.95

Tested by

no test coverage detected