-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathhttp_utils_test.go
More file actions
203 lines (185 loc) · 5.56 KB
/
http_utils_test.go
File metadata and controls
203 lines (185 loc) · 5.56 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package limen
import (
"net/http"
"net/http/httptest"
"regexp"
"testing"
"github.com/stretchr/testify/assert"
)
func TestNormalizePath(t *testing.T) {
t.Parallel()
tests := []struct {
name string
input string
want string
}{
{name: "already normalized", input: "/auth", want: "/auth"},
{name: "missing leading slash", input: "auth", want: "/auth"},
{name: "trailing slash", input: "/auth/", want: "/auth"},
{name: "both issues", input: "auth/", want: "/auth"},
{name: "root", input: "/", want: ""},
{name: "nested path", input: "/api/v1/auth/", want: "/api/v1/auth"},
}
for _, tt := range tests {
tt := tt
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := normalizePath(tt.input)
assert.Equal(t, tt.want, got)
})
}
}
func TestJoinURL(t *testing.T) {
t.Parallel()
tests := []struct {
name string
base string
parts []string
want string
}{
{name: "simple join", base: "http://localhost:8080", parts: []string{"/auth"}, want: "http://localhost:8080/auth"},
{name: "multiple parts", base: "http://localhost:8080", parts: []string{"/auth", "/signin"}, want: "http://localhost:8080/auth/signin"},
{name: "trailing slashes normalized", base: "http://localhost:8080/", parts: []string{"/auth/"}, want: "http://localhost:8080/auth/"},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
got := joinURL(tt.base, tt.parts...)
assert.Equal(t, tt.want, got)
})
}
}
func TestExtractCookieValue(t *testing.T) {
t.Parallel()
w := httptest.NewRecorder()
http.SetCookie(w, &http.Cookie{Name: "session", Value: "abc123"})
http.SetCookie(w, &http.Cookie{Name: "other", Value: "xyz"})
val := ExtractCookieValue(w.Header(), "session")
assert.Equal(t, "abc123", val)
val = ExtractCookieValue(w.Header(), "nonexistent")
assert.Equal(t, "", val)
}
func TestGlobToRegex(t *testing.T) {
t.Parallel()
tests := []struct {
name string
pattern string
matches []string
noMatches []string
supportPathParameters bool
}{
// exact literal
{
name: "exact",
pattern: "/auth/signin",
matches: []string{"/auth/signin"},
noMatches: []string{"/auth/signup", "/auth", "/auth/signin/extra"},
},
{
name: "port",
pattern: "test:8080",
matches: []string{"test:8080"},
noMatches: []string{"test:1080", "test"},
},
// * matches one segment (no slashes)
{
name: "single star",
pattern: "/auth/*",
matches: []string{"/auth/signin", "/auth/signup", "/auth/me", "/auth/"},
noMatches: []string{"/auth/oauth/google"},
},
// ** matches any depth including slashes
{
name: "double star",
pattern: "/auth/**",
matches: []string{"/auth/oauth/google/callback", "/auth/a/b/c", "/auth/signin", "/auth/"},
},
// ? matches single character (not slash)
{
name: "question mark",
pattern: "/auth/m?",
matches: []string{"/auth/me"},
noMatches: []string{"/auth/m", "/auth/m/", "/auth/mee"},
},
{
name: "multiple question marks",
pattern: "/api/v?/auth",
matches: []string{"/api/v1/auth", "/api/v2/auth"},
},
// :param matches one path segment
{
name: "route param mid-path",
pattern: "/oauth/:provider/callback",
supportPathParameters: true,
matches: []string{"/oauth/google/callback", "/oauth/github/callback"},
noMatches: []string{"/oauth/google/bad/callback", "/oauth//callback"},
},
{
name: "route param at end",
pattern: "/users/:id",
supportPathParameters: true,
matches: []string{"/users/42", "/users/abc"},
noMatches: []string{"/users/", "/users/42/edit"},
},
{
name: "multiple route params",
pattern: "/api/:version/:resource",
supportPathParameters: true,
matches: []string{"/api/v1/users", "/api/v2/posts"},
},
// [...] character class
{
name: "character class",
pattern: "/api/v[12]/auth",
matches: []string{"/api/v1/auth", "/api/v2/auth"},
noMatches: []string{"/api/v3/auth", "/api/v9/auth"},
},
// backslash escape
{
name: "escaped star is literal",
pattern: `/auth/\*`,
matches: []string{"/auth/*"},
noMatches: []string{"/auth/signin"},
},
// regex special characters are escaped
{
name: "dot is literal",
pattern: "/auth/file.txt",
matches: []string{"/auth/file.txt"},
noMatches: []string{"/auth/fileXtxt"},
},
{
name: "plus is literal",
pattern: "/auth/c++",
matches: []string{"/auth/c++"},
},
{
name: "parens are literal",
pattern: "/auth/(group)",
matches: []string{"/auth/(group)"},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
regex := globToRegex(tt.pattern, tt.supportPathParameters)
re := regexp.MustCompile(regex)
for _, input := range tt.matches {
assert.True(t, re.MatchString(input), "expected %q to match %q (regex: %s)", tt.pattern, input, regex)
}
for _, input := range tt.noMatches {
assert.False(t, re.MatchString(input), "expected %q NOT to match %q (regex: %s)", tt.pattern, input, regex)
}
})
}
}
func TestIsValidCoreSchema(t *testing.T) {
t.Parallel()
assert.True(t, isValidCoreSchema("users"))
assert.True(t, isValidCoreSchema("sessions"))
assert.True(t, isValidCoreSchema("verifications"))
assert.True(t, isValidCoreSchema("rate_limits"))
assert.True(t, isValidCoreSchema("accounts"))
assert.False(t, isValidCoreSchema("unknown"))
assert.False(t, isValidCoreSchema(""))
}