forked from justinas/nosurf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
62 lines (52 loc) · 1.35 KB
/
utils_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
package nosurf
import (
"net/url"
"testing"
)
func TestSContains(t *testing.T) {
slice := []string{"abc", "def", "ghi"}
s1 := "abc"
if !sContains(slice, s1) {
t.Errorf("sContains said that %v doesn't contain %v, but it does.", slice, s1)
}
s2 := "xyz"
if sContains(slice, s2) {
t.Errorf("sContains said that %v contains %v, but it doesn't.", slice, s2)
}
}
func TestSameOrigin(t *testing.T) {
// a little helper that saves us time
p := func(rawurl string) *url.URL {
u, err := url.Parse(rawurl)
if err != nil {
t.Fatal(err)
}
return u
}
truthy := [][]*url.URL{
{p("http://dummy.us/"), p("http://dummy.us/faq")},
{p("https://dummy.us/some/page"), p("https://dummy.us/faq")},
}
falsy := [][]*url.URL{
// different ports
{p("http://dummy.us/"), p("http://dummy.us:8080")},
// different scheme
{p("https://dummy.us/"), p("http://dummy.us/")},
// different host
{p("https://dummy.us/"), p("http://dummybook.us/")},
// slightly different host
{p("https://beta.dummy.us/"), p("http://dummy.us/")},
}
for _, v := range truthy {
if !sameOrigin(v[0], v[1]) {
t.Errorf("%v and %v have the same origin, but sameOrigin() said otherwise.",
v[0], v[1])
}
}
for _, v := range falsy {
if sameOrigin(v[0], v[1]) {
t.Errorf("%v and %v don't have the same origin, but sameOrigin() said otherwise.",
v[0], v[1])
}
}
}