MCPcopy
hub / github.com/go-chi/chi / TestPattern

Function TestPattern

pattern_test.go:12–70  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

10)
11
12func TestPattern(t *testing.T) {
13 testCases := []struct {
14 name string
15 pattern string
16 method string
17 requestPath string
18 }{
19 {
20 name: "Basic path value",
21 pattern: "/hubs/{hubID}",
22 method: "GET",
23 requestPath: "/hubs/392",
24 },
25 {
26 name: "Two path values",
27 pattern: "/users/{userID}/conversations/{conversationID}",
28 method: "POST",
29 requestPath: "/users/Gojo/conversations/2948",
30 },
31 {
32 name: "Wildcard path",
33 pattern: "/users/{userID}/friends/*",
34 method: "POST",
35 requestPath: "/users/Gojo/friends/all-of-them/and/more",
36 },
37 {
38 name: "Nested sub-router",
39 pattern: "/accounts/{accountID}/hi",
40 method: "GET",
41 requestPath: "/accounts/44/hi",
42 },
43 }
44
45 for _, tc := range testCases {
46 t.Run(tc.name, func(t *testing.T) {
47 r := NewRouter()
48
49 if tc.name == "Nested sub-router" {
50 r.Route("/accounts/{accountID}", func(r Router) {
51 r.Handle(tc.method+" /hi", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
52 w.Write([]byte(r.Pattern))
53 }))
54 })
55 } else {
56 r.Handle(tc.method+" "+tc.pattern, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
57 w.Write([]byte(r.Pattern))
58 }))
59 }
60
61 ts := httptest.NewServer(r)
62 defer ts.Close()
63
64 _, body := testRequest(t, ts, tc.method, tc.requestPath, nil)
65 if body != tc.pattern {
66 t.Fatalf("expecting %q, got %q", tc.pattern, body)
67 }
68 })
69 }

Callers

nothing calls this directly

Calls 7

RouteMethod · 0.95
HandleMethod · 0.95
NewRouterFunction · 0.85
HandlerFuncMethod · 0.80
CloseMethod · 0.80
testRequestFunction · 0.70
WriteMethod · 0.65

Tested by

no test coverage detected