-
Notifications
You must be signed in to change notification settings - Fork 1
/
url_test.go
executable file
·157 lines (132 loc) · 4.81 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
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Package net contains helper function for handling
// e.g. ip addresses or domain names
package net
import "testing"
func TestIsURL(t *testing.T) {
var urlTests = []struct {
url string
expected bool
}{
// {"http://localhost", true},
// {"http://server", true},
{"http://www.microsoft.com", true},
{"HttP://www.microsoft.com", true},
{"www_test.microsoft.com", false},
{"http://www_test.microsoft.com", true},
{"www-test.microsoft.com", false},
{"http://www-test.microsoft.com", true},
{"microsoft.com", false},
{"http://www.microsoft.com", true},
{"http://microsoft.com", true},
{"http://microsoft.com:80", true},
{"https://microsoft.com:443/test", true},
{"https://microsoft.com?hello#fragment", true},
{"http://[2001:db8::1]/32", true},
{"https://[2001:db8::1]:80/32", true},
{"[2001:db8::1]:80/32", true},
{"[2001:db8::]/32", true},
{"1.2.3.4/24", true},
{"1.2.3.0/24", false},
{"2001:db8::/32", false},
{"WWW.EXAMPLE.COM", false},
{"WWW.EXAMPLE.COM/test", true},
{"президент.рф", false}, // kremlin.ru (unicode)
{"xn--d1abbgf6aiiy.xn--p1ai", false}, // kremlin.ru (punycode)
{"www.президент.рф", false},
{"www.xn--d1abbgf6aiiy.xn--p1ai", false},
{"президент.рф/test", true},
{"xn--d1abbgf6aiiy.xn--p1ai/test", true},
{"www.президент.рф/test", true},
{"www.xn--d1abbgf6aiiy.xn--p1ai/test", true},
{"www.президент.рф:8443/test", true},
{"www.xn--d1abbgf6aiiy.xn--p1ai:8443/test", true},
{"https://www.xn--d1abbgf6aiiy.xn--p1ai/test", true},
{"HTTPS://www.xn--d1abbgf6aiiy.xn--p1ai/test", true},
{"www-2.ext.example.com:8443/hello/https://www.example.com", true},
{"www-2.example.com/hello/https://www.example.com", true},
{"na01.safelinks.protection.outlook.com/?url=http://enbau.net/client/past-due-invoice", true},
{"linkprotect.cudasvc.com/url?a=http://irissnuances.com/aug2018/us/invoice-35443454&c=e", true},
{"www.trickyguy.com/wp-includes/01-56889677218-6377383240704407401.php/https://my.klarna.com/uk/business", true},
}
for _, e := range urlTests {
if IsURL(e.url) != e.expected {
t.Errorf("%s", e.url)
}
}
}
func TestFqdnFromURL(t *testing.T) {
var hostTests = []struct {
url string
expected string
}{
// {"http://localhost", "localhost"},
{"http://www.microsoft.com", "www.microsoft.com"},
{"http://microsoft.com", "microsoft.com"},
{"http://microsoft.com:80", "microsoft.com"},
{"https://www.microsoft.com:443/test", "www.microsoft.com"},
{"https://microsoft.com?hello#fragment", "microsoft.com"},
{"[2001:db8::1]/32", "2001:db8::1"},
{"[2001:db8::1]:80/32", "2001:db8::1"},
{"[2001:db8::]/32", "2001:db8::"},
{"1.2.3.4/24", "1.2.3.4"},
{"1.2.3.4:8080/24", "1.2.3.4"},
{"https://1.2.3.4:8080/24", "1.2.3.4"},
{"HTTPS://1.2.3.4:8080/24", "1.2.3.4"},
{"na01.safelinks.protection.outlook.com/?url=http://enbau.net/client/past-due-invoice", "na01.safelinks.protection.outlook.com"},
{"linkprotect.cudasvc.com/url?a=http://irissnuances.com/aug2018/us/invoice-35443454&c=e", "linkprotect.cudasvc.com"},
{"www.trickyguy.com/wp-includes/01-56889677218-6377383240704407401.php/https://my.klarna.com/uk/business", "www.trickyguy.com"},
}
for _, e := range hostTests {
r, err := HostFromURL(e.url)
if r != e.expected || err != nil {
t.Errorf("%s, expected: '%s' != '%s'", e.url, e.expected, r)
}
}
}
func TestNormaliseURLSchema(t *testing.T) {
var hostTests = []struct {
url string
expected string
}{
{"www.example.com/test", "http://www.example.com/test"},
{"http://www.example.com/test", "http://www.example.com/test"},
{"президент.рф/test", "http://президент.рф/test"},
{"http://президент.рф/test", "http://президент.рф/test"},
{"xn--d1abbgf6aiiy.xn--p1ai/test", "http://xn--d1abbgf6aiiy.xn--p1ai/test"},
{"http://xn--d1abbgf6aiiy.xn--p1ai/test", "http://xn--d1abbgf6aiiy.xn--p1ai/test"},
}
for _, e := range hostTests {
r, err := NormaliseURLSchema(e.url)
if r != e.expected || err != nil {
t.Errorf("%s, expected: '%s' != '%s'", e.url, e.expected, r)
}
}
}
func TestToUnicode(t *testing.T) {
var hostTests = []struct {
url string
expected string
}{
{"http://xn--d1abbgf6aiiy.xn--p1ai/test", "http://президент.рф/test"},
}
for _, e := range hostTests {
r, err := URLToUnicode(e.url)
if r != e.expected || err != nil {
t.Errorf("%s, expected: '%s' != '%s'", e.url, e.expected, r)
}
}
}
func TestToPunycode(t *testing.T) {
var hostTests = []struct {
url string
expected string
}{
{"http://президент.рф/test", "http://xn--d1abbgf6aiiy.xn--p1ai/test"},
}
for _, e := range hostTests {
r, err := URLToPunycode(e.url)
if r != e.expected || err != nil {
t.Errorf("%s, expected: '%s' != '%s'", e.url, e.expected, r)
}
}
}