github.com/justinas/nosurf@v1.1.1/utils_test.go (about) 1 package nosurf 2 3 import ( 4 "net/url" 5 "testing" 6 ) 7 8 func TestsContains(t *testing.T) { 9 slice := []string{"abc", "def", "ghi"} 10 11 s1 := "abc" 12 if !sContains(slice, s1) { 13 t.Errorf("sContains said that %v doesn't contain %v, but it does.", slice, s1) 14 } 15 16 s2 := "xyz" 17 if !sContains(slice, s2) { 18 t.Errorf("sContains said that %v contains %v, but it doesn't.", slice, s2) 19 } 20 } 21 22 func TestsameOrigin(t *testing.T) { 23 // a little helper that saves us time 24 p := func(rawurl string) *url.URL { 25 u, err := url.Parse(rawurl) 26 if err != nil { 27 t.Fatal(err) 28 } 29 return u 30 } 31 32 truthy := [][]*url.URL{ 33 {p("http://dummy.us/"), p("http://dummy.us/faq")}, 34 {p("https://dummy.us/some/page"), p("https://dummy.us/faq")}, 35 } 36 37 falsy := [][]*url.URL{ 38 // different ports 39 {p("http://dummy.us/"), p("http://dummy.us:8080")}, 40 // different scheme 41 {p("https://dummy.us/"), p("http://dummy.us/")}, 42 // different host 43 {p("https://dummy.us/"), p("http://dummybook.us/")}, 44 // slightly different host 45 {p("https://beta.dummy.us/"), p("http://dummy.us/")}, 46 } 47 48 for _, v := range truthy { 49 if !sameOrigin(v[0], v[1]) { 50 t.Errorf("%v and %v have the same origin, but sameOrigin() said otherwise.", 51 v[0], v[1]) 52 } 53 } 54 55 for _, v := range falsy { 56 if sameOrigin(v[0], v[1]) { 57 t.Errorf("%v and %v don't have the same origin, but sameOrigin() said otherwise.", 58 v[0], v[1]) 59 } 60 } 61 62 }