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

Function TestStripSlashesMW

coderd/coderd_internal_test.go:13–67  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

11)
12
13func TestStripSlashesMW(t *testing.T) {
14 t.Parallel()
15
16 tests := []struct {
17 name string
18 inputPath string
19 wantPath string
20 }{
21 {"No changes", "/api/v1/buildinfo", "/api/v1/buildinfo"},
22 {"Double slashes", "/api//v2//buildinfo", "/api/v2/buildinfo"},
23 {"Triple slashes", "/api///v2///buildinfo", "/api/v2/buildinfo"},
24 {"Leading slashes", "///api/v2/buildinfo", "/api/v2/buildinfo"},
25 {"Root path", "/", "/"},
26 {"Double slashes root", "//", "/"},
27 {"Only slashes", "/////", "/"},
28 }
29
30 handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
31 w.WriteHeader(http.StatusOK)
32 })
33
34 for _, tt := range tests {
35 t.Run("chi/"+tt.name, func(t *testing.T) {
36 t.Parallel()
37 req := httptest.NewRequest("GET", tt.inputPath, nil)
38 rec := httptest.NewRecorder()
39
40 // given
41 rctx := chi.NewRouteContext()
42 rctx.RoutePath = tt.inputPath
43 req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
44
45 // when
46 singleSlashMW(handler).ServeHTTP(rec, req)
47 updatedCtx := chi.RouteContext(req.Context())
48
49 // then
50 assert.Equal(t, tt.inputPath, req.URL.Path)
51 assert.Equal(t, tt.wantPath, updatedCtx.RoutePath)
52 })
53
54 t.Run("stdlib/"+tt.name, func(t *testing.T) {
55 t.Parallel()
56 req := httptest.NewRequest("GET", tt.inputPath, nil)
57 rec := httptest.NewRecorder()
58
59 // when
60 singleSlashMW(handler).ServeHTTP(rec, req)
61
62 // then
63 assert.Equal(t, tt.wantPath, req.URL.Path)
64 assert.Nil(t, chi.RouteContext(req.Context()))
65 })
66 }
67}

Callers

nothing calls this directly

Calls 7

singleSlashMWFunction · 0.85
WithContextMethod · 0.80
RunMethod · 0.65
ContextMethod · 0.65
WriteHeaderMethod · 0.45
ServeHTTPMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected