MCPcopy Index your code
hub / github.com/coder/coder / TestHTTPRoute

Function TestHTTPRoute

coderd/httpmw/httproute_test.go:15–104  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

13)
14
15func TestHTTPRoute(t *testing.T) {
16 t.Parallel()
17
18 for _, tc := range []struct {
19 name string
20 reqFn func() *http.Request
21 registerRoutes map[string]string
22 mws []func(http.Handler) http.Handler
23 expectedRoute string
24 expectedMethod string
25 }{
26 {
27 name: "without middleware",
28 reqFn: func() *http.Request {
29 return httptest.NewRequest(http.MethodGet, "/", nil)
30 },
31 registerRoutes: map[string]string{http.MethodGet: "/"},
32 mws: []func(http.Handler) http.Handler{},
33 expectedRoute: "",
34 expectedMethod: "",
35 },
36 {
37 name: "root",
38 reqFn: func() *http.Request {
39 return httptest.NewRequest(http.MethodGet, "/", nil)
40 },
41 registerRoutes: map[string]string{http.MethodGet: "/"},
42 mws: []func(http.Handler) http.Handler{httpmw.HTTPRoute},
43 expectedRoute: "/",
44 expectedMethod: http.MethodGet,
45 },
46 {
47 name: "parameterized route",
48 reqFn: func() *http.Request {
49 return httptest.NewRequest(http.MethodPut, "/users/123", nil)
50 },
51 registerRoutes: map[string]string{http.MethodPut: "/users/{id}"},
52 mws: []func(http.Handler) http.Handler{httpmw.HTTPRoute},
53 expectedRoute: "/users/{id}",
54 expectedMethod: http.MethodPut,
55 },
56 {
57 name: "unknown",
58 reqFn: func() *http.Request {
59 return httptest.NewRequest(http.MethodGet, "/api/a", nil)
60 },
61 registerRoutes: map[string]string{http.MethodGet: "/api/b"},
62 mws: []func(http.Handler) http.Handler{httpmw.HTTPRoute},
63 expectedRoute: "UNKNOWN",
64 expectedMethod: http.MethodGet,
65 },
66 {
67 name: "static",
68 reqFn: func() *http.Request {
69 return httptest.NewRequest(http.MethodGet, "/some/static/file.png", nil)
70 },
71 registerRoutes: map[string]string{http.MethodGet: "/"},
72 mws: []func(http.Handler) http.Handler{httpmw.HTTPRoute},

Callers

nothing calls this directly

Calls 8

ContextFunction · 0.92
ExtractHTTPMethodFunction · 0.92
ExtractHTTPRouteFunction · 0.92
TryReceiveFunction · 0.92
RunMethod · 0.65
ContextMethod · 0.65
EqualMethod · 0.45
ServeHTTPMethod · 0.45

Tested by

no test coverage detected