-
Notifications
You must be signed in to change notification settings - Fork 35
/
exemptions_test.go
85 lines (68 loc) · 1.68 KB
/
exemptions_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
package csrf
import (
"testing"
)
func TestExemptedFullPath(t *testing.T) {
path := "/Hello"
ExemptedFullPath(path)
if !IsExempted(path) {
t.Errorf("%v is not exempted, but it should be", path)
}
other := "/Goodbye"
if IsExempted(other) {
t.Errorf("%v is exempted, but it shouldn't be", other)
}
}
func TestExemptedFullPaths(t *testing.T) {
paths := []string{"/home", "/news", "/help"}
ExemptedFullPaths(paths...)
for _, v := range paths {
if !IsExempted(v) {
t.Errorf("%v should be exempted, but it isn't", v)
}
}
other := "/accounts"
if IsExempted(other) {
t.Errorf("%v is exempted, but it shouldn't be", other)
}
}
func TestExemptedGlob(t *testing.T) {
glob := "/[m-n]ail"
ExemptedGlob(glob)
test := "/mail"
if !IsExempted(test) {
t.Errorf("%v should be exempted, but it isn't.", test)
}
test = "/nail"
if !IsExempted(test) {
t.Errorf("%v should be exempted, but it isn't.", test)
}
test = "/snail"
if IsExempted(test) {
t.Errorf("%v should not be exempted, but it is.", test)
}
test = "/mail/outbox"
if IsExempted(test) {
t.Errorf("%v should not be exempted, but it is.", test)
}
}
func TestExemptedGlobs(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/",
}
ExemptedGlobs(slice...)
for _, v := range matching {
if !IsExempted(v) {
t.Errorf("%v should be exempted, but it isn't.", v)
}
}
for _, v := range nonMatching {
if IsExempted(v) {
t.Errorf("%v shouldn't be exempted, but it is", v)
}
}
}