/
uri_test.go
182 lines (167 loc) · 3.86 KB
/
uri_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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
package uri
import (
"reflect"
"testing"
"github.com/ttacon/pretty"
)
func Test_rawURIParse(t *testing.T) {
var tests = []struct {
uriRaw string
uri *uri
err error
}{
{
"foo://example.com:8042/over/there?name=ferret#nose",
&uri{"foo", "//example.com:8042/over/there", "name=ferret", "nose",
&authorityInfo{
"//",
"",
"example.com",
"8042",
"/over/there",
},
},
nil,
},
{
"http://httpbin.org/get?utf8=\xe2\x98\x83",
&uri{"http", "//httpbin.org/get", "utf8=\xe2\x98\x83", "",
&authorityInfo{
"//",
"",
"httpbin.org",
"",
"/get",
},
},
nil,
},
{
"mailto:user@domain.com",
&uri{"mailto", "user@domain.com", "", "",
&authorityInfo{
"",
"user",
"domain.com",
"",
"",
},
},
nil,
},
{
"ssh://user@git.openstack.org:29418/openstack/keystone.git",
&uri{"ssh", "//user@git.openstack.org:29418/openstack/keystone.git", "", "",
&authorityInfo{
"//",
"user",
"git.openstack.org",
"29418",
"/openstack/keystone.git",
},
},
nil,
},
{
"https://willo.io/#yolo",
&uri{"https", "//willo.io/", "", "yolo",
&authorityInfo{"//", "", "willo.io", "", "/"},
},
nil,
},
}
for _, test := range tests {
got, err := ParseURI(test.uriRaw)
if err != test.err {
t.Errorf("got back unexpected err: %v != %v", err, test.err)
continue
} else if !reflect.DeepEqual(got, test.uri) {
t.Errorf("got back unexpected (raw: %s), uri: %v != %v",
test.uriRaw, pretty.Sprintf("%#v", got), pretty.Sprintf("%#v", test.uri))
}
}
}
func Test_ParseThenString(t *testing.T) {
var tests = []string{
"foo://example.com:8042/over/there?name=ferret#nose",
"http://httpbin.org/get?utf8=\xe2\x98\x83",
"mailto:user@domain.com",
"ssh://user@git.openstack.org:29418/openstack/keystone.git",
"https://willo.io/#yolo",
}
for _, test := range tests {
uri, err := ParseURI(test)
if err != nil {
t.Errorf("failed to parse URI, err: %v", err)
} else if uri.String() != test {
pretty.Println(uri)
t.Errorf("uri.String() != test: %v != %v", uri.String(), test)
}
}
}
func Benchmark_Parse(b *testing.B) {
var tests = []string{
"foo://example.com:8042/over/there?name=ferret#nose",
"http://httpbin.org/get?utf8=\xe2\x98\x83",
"mailto:user@domain.com",
"ssh://user@git.openstack.org:29418/openstack/keystone.git",
"https://willo.io/#yolo",
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, _ = ParseURI(tests[i%5])
}
}
func Benchmark_String(b *testing.B) {
var tests = []*uri{
&uri{"foo", "//example.com:8042/over/there", "name=ferret", "nose",
&authorityInfo{"//", "", "example.com", "8042", "/over/there"},
},
&uri{"http", "//httpbin.org/get", "utf8=\xe2\x98\x83", "",
&authorityInfo{"//", "", "httpbin.org", "", "/get"},
},
&uri{"mailto", "user@domain.com", "", "",
&authorityInfo{"", "user", "domain.com", "", ""},
},
&uri{"ssh", "//user@git.openstack.org:29418/openstack/keystone.git", "", "",
&authorityInfo{"//", "user", "git.openstack.org", "29418", "/openstack/keystone.git"},
},
&uri{"https", "//willo.io/", "", "yolo",
&authorityInfo{"//", "", "willo.io", "", "/"},
},
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_ = tests[i%5].String()
}
}
func Test_Building(t *testing.T) {
var tests = []struct {
uri, uriChanged string
name string
}{
{
"mailto:user@domain.com",
"mailto:yolo@domain.com",
"yolo",
},
{
"https://user@domain.com",
"https://yolo2@domain.com",
"yolo2",
},
}
for _, test := range tests {
uri, err := ParseURI(test.uri)
if err != nil {
t.Errorf("failed to parse uri: %v", err)
continue
}
val := uri.Builder().SetUserInfo(test.name).String()
if val != test.uriChanged {
t.Errorf("vals don't match: %v != %v", val, test.uriChanged)
}
}
}