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

Function Test_Middleware

coderd/tracing/httpmw_test.go:46–102  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

44}
45
46func Test_Middleware(t *testing.T) {
47 t.Parallel()
48
49 t.Run("OnlyRunsOnExpectedRoutes", func(t *testing.T) {
50 t.Parallel()
51
52 cases := []struct {
53 path string
54 runs bool
55 }{
56 // Should pass.
57 {"/api", true},
58 {"/api/v0", true},
59 {"/api/v2", true},
60 {"/api/v2/workspaces/", true},
61 {"/api/v2/workspaces", true},
62 {"/@hi/hi/apps/hi", true},
63 {"/@hi/hi/apps/hi/hi", true},
64 {"/@hi/hi/apps/hi/hi", true},
65 {"/%40hi/hi/apps/hi", true},
66 {"/%40hi/hi/apps/hi/hi", true},
67 {"/%40hi/hi/apps/hi/hi", true},
68 {"/external-auth/hi/callback", true},
69
70 // Other routes that should not be collected.
71 {"/index.html", false},
72 {"/static/coder_linux_amd64", false},
73 {"/workspaces", false},
74 {"/templates", false},
75 {"/@hi/hi/terminal", false},
76 }
77
78 for _, c := range cases {
79 name := strings.ReplaceAll(strings.TrimPrefix(c.path, "/"), "/", "_")
80 t.Run(name, func(t *testing.T) {
81 t.Parallel()
82
83 fake := &fakeTracer{}
84
85 rw := &tracing.StatusWriter{ResponseWriter: httptest.NewRecorder()}
86 r := httptest.NewRequest("GET", c.path, nil)
87
88 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
89 defer cancel()
90 ctx = context.WithValue(ctx, chi.RouteCtxKey, chi.NewRouteContext())
91 r = r.WithContext(ctx)
92
93 tracing.Middleware(fake)(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
94 rw.WriteHeader(http.StatusNoContent)
95 })).ServeHTTP(rw, r)
96
97 didRun := fake.startCalled.Load() == 1
98 require.Equal(t, c.runs, didRun, "expected middleware to run/not run")
99 })
100 }
101 })
102}

Callers

nothing calls this directly

Calls 7

WriteHeaderMethod · 0.95
MiddlewareFunction · 0.92
WithContextMethod · 0.80
RunMethod · 0.65
ServeHTTPMethod · 0.45
LoadMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected