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

Function TestPattern

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

Source from the content-addressed store, hash-verified

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

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