-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathurltosalt_test.go
61 lines (57 loc) · 1.78 KB
/
urltosalt_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
package main
import (
"bytes"
"testing"
)
func TestUrlToSalt(t *testing.T) {
urlTests := []struct {
url string
salt string
}{
{"https://google.com", "google.com"}, // Simple.
{"http://google.com", "http://google.com"}, // No-secure urls get no-secure prefix.
{"https://google.com:8043", "google.com"}, // Port numbers are ignored.
{"http://google.com:8080", "http://google.com"},
{"https://subdomain.google.com", "google.com"}, // Don't include subdomain.
{"http://subdomain.google.com", "http://google.com"},
{"https://subdomain.github.io", "subdomain.github.io"}, // Except if it is a public suffix.
{"http://subdomain.github.io", "http://subdomain.github.io"},
{"https://our.warwick.ac.uk", "warwick.ac.uk"},
{"http://www.accommodation.manchester.ac.uk", "http://manchester.ac.uk"}, // Shame on mcr https not covering everything lol
{"ftp://example.com", "ftp://example.com"}, // Protocol passthrough
{"ftp://subdomain.example.com", "ftp://example.com"},
{"ftp://subdomain.github.io", "ftp://subdomain.github.io"},
}
errorTests := []string{
"some-name.com",
"invalid://",
"https://com",
"some-name.com/some/path",
"/some-name.com/some/path",
"name",
}
test := func(url, wantSalt string) {
t.Run(url, func(t *testing.T) {
got, err := UrlToSalt(url)
if err != nil {
t.Errorf("UrlToSalt() errored: %v", err)
return
}
if bytes.Compare([]byte(wantSalt), got) != 0 {
t.Errorf("UrlToSalt() = %v, want %v", string(got), wantSalt)
}
})
}
for _, tt := range urlTests {
test(tt.url, tt.salt)
test(tt.url+"/some/path?query", tt.salt)
}
for _, str := range errorTests {
t.Run(str+" (should error)", func(t *testing.T) {
_, err := UrlToSalt(str)
if err == nil {
t.Errorf("Expected some error, but got none.")
}
})
}
}