-
-
Notifications
You must be signed in to change notification settings - Fork 273
/
url_test.go
57 lines (51 loc) · 1.29 KB
/
url_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
package templ
import (
"strings"
"testing"
)
type urlTest struct {
url string
expectSanitized bool
}
var urlTests = []urlTest{
{"//example.com", false},
{"/", false},
{"/index", false},
{"http://example.com", false},
{"https://example.com", false},
{"mailto:[email protected]", false},
{"tel:+1234567890", false},
{"ftp://example.com", false},
{"ftps://example.com", false},
{"irc://example.com", true},
{"bitcoin://example.com", true},
}
func testURL(t *testing.T, url string, expectSanitized bool) {
u := URL(url)
wasSanitized := u == FailedSanitizationURL
if expectSanitized != wasSanitized {
t.Errorf("expected sanitized=%v, got %v", expectSanitized, wasSanitized)
}
}
func TestURL(t *testing.T) {
for _, test := range urlTests {
t.Run(test.url, func(t *testing.T) {
testURL(t, test.url, test.expectSanitized)
})
test.url = strings.ToUpper(test.url)
t.Run(strings.ToUpper(test.url), func(t *testing.T) {
testURL(t, test.url, test.expectSanitized)
})
}
}
func BenchmarkURL(b *testing.B) {
for i := 0; i < b.N; i++ {
for _, test := range urlTests {
u := URL(test.url)
wasSanitized := u == FailedSanitizationURL
if test.expectSanitized != wasSanitized {
b.Errorf("expected sanitized=%v, got %v", test.expectSanitized, wasSanitized)
}
}
}
}