-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathcoderd_internal_test.go
More file actions
67 lines (55 loc) · 1.62 KB
/
coderd_internal_test.go
File metadata and controls
67 lines (55 loc) · 1.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package coderd
import (
"context"
"net/http"
"net/http/httptest"
"testing"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
)
func TestStripSlashesMW(t *testing.T) {
t.Parallel()
tests := []struct {
name string
inputPath string
wantPath string
}{
{"No changes", "/api/v1/buildinfo", "/api/v1/buildinfo"},
{"Double slashes", "/api//v2//buildinfo", "/api/v2/buildinfo"},
{"Triple slashes", "/api///v2///buildinfo", "/api/v2/buildinfo"},
{"Leading slashes", "///api/v2/buildinfo", "/api/v2/buildinfo"},
{"Root path", "/", "/"},
{"Double slashes root", "//", "/"},
{"Only slashes", "/////", "/"},
}
handler := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusOK)
})
for _, tt := range tests {
t.Run("chi/"+tt.name, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest("GET", tt.inputPath, nil)
rec := httptest.NewRecorder()
// given
rctx := chi.NewRouteContext()
rctx.RoutePath = tt.inputPath
req = req.WithContext(context.WithValue(req.Context(), chi.RouteCtxKey, rctx))
// when
singleSlashMW(handler).ServeHTTP(rec, req)
updatedCtx := chi.RouteContext(req.Context())
// then
assert.Equal(t, tt.inputPath, req.URL.Path)
assert.Equal(t, tt.wantPath, updatedCtx.RoutePath)
})
t.Run("stdlib/"+tt.name, func(t *testing.T) {
t.Parallel()
req := httptest.NewRequest("GET", tt.inputPath, nil)
rec := httptest.NewRecorder()
// when
singleSlashMW(handler).ServeHTTP(rec, req)
// then
assert.Equal(t, tt.wantPath, req.URL.Path)
assert.Nil(t, chi.RouteContext(req.Context()))
})
}
}