-
Notifications
You must be signed in to change notification settings - Fork 126
/
exempt_test.go
235 lines (190 loc) · 5.39 KB
/
exempt_test.go
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
package nosurf
import (
"net/http"
"regexp"
"testing"
)
func TestExemptPath(t *testing.T) {
// the handler doesn't matter here, let's use nil
hand := New(nil)
path := "/home"
exempt, _ := http.NewRequest("GET", path, nil)
hand.ExemptPath(path)
if !hand.IsExempt(exempt) {
t.Errorf("%v is not exempt, but it should be", exempt.URL.Path)
}
other, _ := http.NewRequest("GET", "/faq", nil)
if hand.IsExempt(other) {
t.Errorf("%v is exempt, but it shouldn't be", other.URL.Path)
}
}
func TestExemptPaths(t *testing.T) {
hand := New(nil)
paths := []string{"/home", "/news", "/help"}
hand.ExemptPaths(paths...)
for _, v := range paths {
request, _ := http.NewRequest("GET", v, nil)
if !hand.IsExempt(request) {
t.Errorf("%v should be exempt, but it isn't", v)
}
}
other, _ := http.NewRequest("GET", "/accounts", nil)
if hand.IsExempt(other) {
t.Errorf("%v is exempt, but it shouldn't be", other)
}
}
func TestExemptGlob(t *testing.T) {
hand := New(nil)
glob := "/[m-n]ail"
hand.ExemptGlob(glob)
test, _ := http.NewRequest("GET", "/mail", nil)
if !hand.IsExempt(test) {
t.Errorf("%v should be exempt, but it isn't.", test)
}
test, _ = http.NewRequest("GET", "/nail", nil)
if !hand.IsExempt(test) {
t.Errorf("%v should be exempt, but it isn't.", test)
}
test, _ = http.NewRequest("GET", "/snail", nil)
if hand.IsExempt(test) {
t.Errorf("%v should not be exempt, but it is.", test)
}
test, _ = http.NewRequest("GET", "/mail/outbox", nil)
if hand.IsExempt(test) {
t.Errorf("%v should not be exempt, but it is.", test)
}
}
func TestExemptGlobs(t *testing.T) {
slice := []string{"/", "/accounts/*", "/post/?*"}
matching := []string{"/", "/accounts/", "/accounts/johndoe", "/post/1", "/post/123"}
nonMatching := []string{"", "/accounts",
// glob's * and ? don't match a forward slash
"/accounts/johndoe/posts",
"/post/",
}
hand := New(nil)
hand.ExemptGlobs(slice...)
for _, v := range matching {
test, _ := http.NewRequest("GET", v, nil)
if !hand.IsExempt(test) {
t.Errorf("%v should be exempt, but it isn't.", v)
}
}
for _, v := range nonMatching {
test, _ := http.NewRequest("GET", v, nil)
if hand.IsExempt(test) {
t.Errorf("%v shouldn't be exempt, but it is", v)
}
}
}
// This only tests that ExemptRegexp handles the argument correctly
// The matching itself is tested by TestExemptRegexpMatching
func TestExemptRegexpCall(t *testing.T) {
pattern := "^/[rd]ope$"
// case 1: a string
hand := New(nil)
hand.ExemptRegexp(pattern)
// String() returns the original pattern
got := hand.exemptRegexps[0].String()
if pattern != got {
t.Errorf("The compiled pattern has changed: expected %v, got %v",
pattern, got)
}
// case 2: a compiled *Regexp
hand = New(nil)
re := regexp.MustCompile(pattern)
hand.ExemptRegexp(re)
gotRe := hand.exemptRegexps[0]
if re != gotRe {
t.Errorf("The compiled pattern is not what we gave: expected %v, got %v",
re, gotRe)
}
}
func TestExemptRegexpInvalidType(t *testing.T) {
arg := 123
defer func() {
r := recover()
if r == nil {
t.Error("The function didn't panic on an invalid argument type")
}
}()
hand := New(nil)
hand.ExemptRegexp(arg)
}
func TestExemptRegexpInvalidPattern(t *testing.T) {
// an unclosed group
pattern := "a(b"
defer func() {
r := recover()
if r == nil {
t.Error("The function didn't panic on an invalid regular expression")
}
}()
hand := New(nil)
hand.ExemptRegexp(pattern)
}
// The same as TestExemptRegexCall, but for the variadic function
func TestExemptRegexpsCall(t *testing.T) {
// case 1: a slice of strings
hand := New(nil)
slice1 := []interface{}{"^/$", "^/accounts$"}
hand.ExemptRegexps(slice1...)
for i := range slice1 {
pat := hand.exemptRegexps[i].String()
got := slice1[i]
if pat != got {
t.Errorf("The compiled pattern has changed: expected %v, got %v", pat, got)
}
}
// case 2: a mixed slice
hand = New(nil)
slice2 := []interface{}{"^/$", regexp.MustCompile("^/accounts$")}
hand.ExemptRegexps(slice2...)
pat := slice2[0].(string)
got := hand.exemptRegexps[0].String()
if pat != got {
t.Errorf("The compiled pattern has changed: expected %v, got %v", pat, got)
}
pat = slice2[1].(*regexp.Regexp).String()
got = hand.exemptRegexps[1].String()
if pat != got {
t.Errorf("The compiled pattern has changed: expected %v, got %v", pat, got)
}
}
func TestExemptRegexpMatching(t *testing.T) {
hand := New(nil)
re := `^/[mn]ail$`
hand.ExemptRegexp(re)
// valid
test, _ := http.NewRequest("GET", "/mail", nil)
if !hand.IsExempt(test) {
t.Errorf("%v should be exempt, but it isn't.", test)
}
test, _ = http.NewRequest("GET", "/nail", nil)
if !hand.IsExempt(test) {
t.Errorf("%v should be exempt, but it isn't.", test)
}
test, _ = http.NewRequest("GET", "/mail/outbox", nil)
if hand.IsExempt(test) {
t.Errorf("%v shouldn't be exempt, but it is.", test)
}
test, _ = http.NewRequest("GET", "/snail", nil)
if hand.IsExempt(test) {
t.Errorf("%v shouldn't be exempt, but it is.", test)
}
}
func TestExemptFunc(t *testing.T) {
// the handler doesn't matter here, let's use nil
hand := New(nil)
hand.ExemptFunc(func(r *http.Request) bool {
return r.Method == "GET"
})
test, _ := http.NewRequest("GET", "/path", nil)
if !hand.IsExempt(test) {
t.Errorf("%v is not exempt, but it should be", test)
}
other, _ := http.NewRequest("POST", "/path", nil)
if hand.IsExempt(other) {
t.Errorf("%v is exempt, but it shouldn't be", other)
}
}