-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser_test.go
308 lines (287 loc) · 10.8 KB
/
parser_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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package dsnparser
import "testing"
func TestDSN_HasParam(t *testing.T) {
result := Parse("mysql://user:[email protected]:3306/dbname?foo=foo val")
if !result.HasParam("foo") {
t.Errorf("Unexpected value. Must be true")
}
if result.HasParam("bar") {
t.Errorf("Unexpected value. Must be false")
}
}
func TestParse_Scheme(t *testing.T) {
tests := []struct {
dsn string
expected string
}{
{"", ""},
{"mysql", ""},
{"://", ""},
{"mysql://", "mysql"},
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "mysql"},
}
for i, test := range tests {
dsn := Parse(test.dsn)
if dsn.GetScheme() != test.expected {
t.Errorf("Unexpected value in test \"%v\". Expected: \"%s\". Result: \"%s\"", i+1, test.expected, dsn.GetScheme())
}
}
}
func TestParse_User(t *testing.T) {
tests := []struct {
dsn string
expected string
}{
{"", ""},
{"@", ""},
{"user", ""},
{"user@", "user"},
{"mysql://user", ""},
{"mysql://user@", "user"},
{"user:password", ""},
{"user:password@", "user"},
{"mysql://user:password", ""},
{"mysql://user:password@", "user"},
{"mysql://@", ""},
{"mysql://:password@", ""},
{"user:@", "user"},
{":password@", ""},
{"u@", "u"},
{":@", ""},
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "user"},
{"mysql://[email protected]:3306/dbname?tblsprefix=fs_", "user"},
{"mysql://example.com:3306/dbname?tblsprefix=fs_", ""},
{"\\@@", "@"},
{"us\\@er@", "us@er"},
{"mysql://use\\@r:password@", "use@r"},
{"mysql://us\\@e\\:r@", "us@e:r"},
{"mysql://user\\:password\\@example.com\\:3306/dbname?tblsprefix=fs_:password@", "user:[email protected]:3306/dbname?tblsprefix=fs_"},
{"mysql://пользователь:пароль", ""},
{"mysql://пользователь:пароль@", "пользователь"},
{"mysql://пользователь1:пароль@", "пользователь1"},
{"mysql://пользователь", ""},
{"mysql://пользователь@", "пользователь"},
{"mysql://пользователь1@", "пользователь1"},
{"mysql://п#о\\@льзователь\\::\\@пар\\:оль@", "п#о@льзователь:"},
}
for i, test := range tests {
dsn := Parse(test.dsn)
if dsn.GetUser() != test.expected {
t.Errorf("Unexpected value in test \"%v\". Expected: \"%s\". Result: \"%s\"", i+1, test.expected, dsn.GetUser())
}
}
}
func TestParse_Password(t *testing.T) {
tests := []struct {
dsn string
expected string
}{
{"", ""},
{"@", ""},
{"user", ""},
{"user@", ""},
{"mysql://user", ""},
{"mysql://user@", ""},
{"user:password", ""},
{"user:password@", "password"},
{"mysql://user:password", ""},
{"mysql://user:password@", "password"},
{"mysql://@", ""},
{"mysql://:password@", "password"},
{"user:@", ""},
{":password@", "password"},
{":p@", "p"},
{":@", ""},
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "password"},
{"mysql://[email protected]:3306/dbname?tblsprefix=fs_", ""},
{"mysql://example.com:3306/dbname?tblsprefix=fs_", ""},
{"\\@@", ""},
{"us\\@er@", ""},
{"mysql://user:p\\@ssw\\:ord@", "p@ssw:ord"},
{"mysql://:p\\@ssw\\:ord@", "p@ssw:ord"},
{"mysql://user:password\\@example.com\\:3306/dbname?tblsprefix=fs_@", "[email protected]:3306/dbname?tblsprefix=fs_"},
{"mysql://пользователь:пароль", ""},
{"mysql://пользователь:пароль1", ""},
{"mysql://пользователь:пароль@", "пароль"},
{"mysql://пользователь:пароль1@", "пароль1"},
{"mysql://:пароль", ""},
{"mysql://:пароль@", "пароль"},
{"mysql://:1пароль_1@", "1пароль_1"},
{"mysql://п#о\\@льзователь\\::\\@пар\\:оль@", "@пар:оль"},
}
for i, test := range tests {
dsn := Parse(test.dsn)
if dsn.GetPassword() != test.expected {
t.Errorf("Unexpected value in test \"%v\". Expected: \"%s\". Result: \"%s\"", i+1, test.expected, dsn.GetPassword())
}
}
}
func TestParse_Transport(t *testing.T) {
tests := []struct {
dsn string
expected string
}{
{"example.com", ""},
{"example.com:3306", ""},
{"(example.com)", ""},
{"tcp()", "tcp"},
{"x(example.com)", "x"},
{"tcp(example.com)", "tcp"},
{"tcp(example.com:3306)", "tcp"},
{"mysql://user:password@tcp(example.com)", "tcp"},
{"mysql://user:password@tcp(example.com:3306)", "tcp"},
{"mysql://user:password@tcp(example.com)/dbname?tblsprefix=fs_", "tcp"},
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "tcp"},
}
for i, test := range tests {
dsn := Parse(test.dsn)
if dsn.GetTransport() != test.expected {
t.Errorf("Unexpected value in test \"%v\". Expected: \"%s\". Result: \"%s\"", i+1, test.expected, dsn.GetTransport())
}
}
}
func TestParse_Host(t *testing.T) {
tests := []struct {
dsn string
expected string
}{
{"mysql://user:[email protected]:3306/dbname?tblsprefix=fs_", "example.com"},
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "example.com"},
{"mysql://user:[email protected]/dbname?tblsprefix=fs_", "example.com"},
{"mysql://user:password@tcp(example.com)/dbname?tblsprefix=fs_", "example.com"},
{"mysql://user:password@localhost/dbname?tblsprefix=fs_", "localhost"},
{"mysql://user:[email protected]/dbname?tblsprefix=fs_", "127.0.0.1"},
{"mysql://user:[email protected]", "example.com"},
{"mysql://user:[email protected]:3306", "example.com"},
{"mysql://user:password@/dbname?tblsprefix=fs_", ""},
{"mysql://user:password@:3306", ""},
{"mysql://user:password@", ""},
{"mysql://example.loc", "example.loc"},
{"example.loc", "example.loc"},
{"example.loc:3306", "example.loc"},
{"example.loc/path", "example.loc"},
{"example.loc:3306/path", "example.loc"},
{"example.loc:/", "example.loc"},
{"example", "example"},
{"example:3306", "example"},
{"example/path", "example"},
{"example:/", "example"},
{"mysql://user:password@not$valid@host/dbname?tblsprefix=fs_", "not$valid@host"},
{"mysql://user:password@not$valid@hostdbname?tblsprefix=fs_", "not$valid@hostdbname?tblsprefix=fs_"},
{"mysql://user:password@хост.лок:3306/dbname?tblsprefix=fs_", "хост.лок"},
{"mysql://user:password@хост.лок", "хост.лок"},
{"mysql://user:password@хост.лок:3306/путь", "хост.лок"},
{"mysql://user:password@хост.лок:3306", "хост.лок"},
{"mysql://user:password@хост.лок1", "хост.лок1"},
{"mysql://user:password@хост.ло1к", "хост.ло1к"},
}
for i, test := range tests {
dsn := Parse(test.dsn)
if dsn.GetHost() != test.expected {
t.Errorf("Unexpected value in test \"%v\". Expected: \"%s\". Result: \"%s\"", i+1, test.expected, dsn.GetHost())
}
}
}
func TestParse_Port(t *testing.T) {
tests := []struct {
dsn string
expected string
}{
{"mysql://user:[email protected]:3306/dbname?tblsprefix=fs_", "3306"},
{"mysql://user:password@tcp(example.com:3306)/dbname?tblsprefix=fs_", "3306"},
{"mysql://user:[email protected]:3306", "3306"},
{"mysql://user:password@tcp(example.com:3306)", "3306"},
{"mysql://user:[email protected]/dbname?tblsprefix=fs_", ""},
{"mysql://user:[email protected]:/dbname?tblsprefix=fs_", ""},
{"mysql://user:[email protected]:bad, but working/dbname?tblsprefix=fs_", "bad, but working"},
{"example.com:3306", "3306"},
{"tcp(example.com:3306)", "3306"},
{"example.com:", ""},
{"example.com", ""},
{"хост.лок:3306", "3306"},
}
for i, test := range tests {
dsn := Parse(test.dsn)
if dsn.GetPort() != test.expected {
t.Errorf("Unexpected value in test \"%v\". Expected: \"%s\". Result: \"%s\"", i+1, test.expected, dsn.GetPort())
}
}
}
func TestParse_Path(t *testing.T) {
tests := []struct {
dsn string
expected string
}{
{"mysql://user:[email protected]:3306/foo?tblsprefix=fs_", "foo"},
{"mysql://user:password@tcp(example.com:3306)/foo?tblsprefix=fs_", "foo"},
{"mysql://user:[email protected]:3306/foo/bar/baz?tblsprefix=fs_", "foo/bar/baz"},
{"mysql://user:[email protected]:3306//?tblsprefix=fs_", "/"},
{"mysql://user:[email protected]:3306/?tblsprefix=fs_", ""},
{"mysql://user:[email protected]:3306/foo", "foo"},
{"mysql://user:[email protected]:3306/foo/bar/baz", "foo/bar/baz"},
{"mysql://user:[email protected]:3306", ""},
{"mysql://user:[email protected]:3306/", ""},
{"mysql://user:[email protected]:3306//", "/"},
{"example.com/foo?tblsprefix=fs_", "foo"},
{"example.com/foo/bar/baz?tblsprefix=fs_", "foo/bar/baz"},
{"example.com/foo", "foo"},
{"example.com/foo/bar/baz", "foo/bar/baz"},
{"socket:///foo/bar.sock", "foo/bar.sock"},
{"mysql://user:[email protected]:3306/фу/бар/баз?tblsprefix=fs_", "фу/бар/баз"},
{"mysql://user:[email protected]:3306/фу/бар/баз", "фу/бар/баз"},
}
for i, test := range tests {
dsn := Parse(test.dsn)
if dsn.GetPath() != test.expected {
t.Errorf("Unexpected value in test \"%v\". Expected: \"%s\". Result: \"%s\"", i+1, test.expected, dsn.GetPath())
}
}
}
func TestParse_Params(t *testing.T) {
type ExpectedItem struct {
key string
value string
}
type ExpectedList []ExpectedItem
tests := []struct {
dsn string
expected ExpectedList
}{
{"mysql://user:[email protected]:3306/dbname?fooKey=foo val&barKey=bar val", ExpectedList{
ExpectedItem{"fooKey", "foo val"},
ExpectedItem{"barKey", "bar val"},
}},
{"mysql://user:[email protected]:3306/dbname?fooKey&barKey=&&", ExpectedList{
ExpectedItem{"fooKey", ""},
ExpectedItem{"barKey", ""},
}},
{"mysql://user:[email protected]:3306/dbname?foo\\&Key=foo\\&val&bar\\&Key=bar\\&val", ExpectedList{
ExpectedItem{"foo&Key", "foo&val"},
ExpectedItem{"bar&Key", "bar&val"},
}},
{"mysql://user:[email protected]:3306/dbname?foo\\=Key=foo\\=val&bar\\=Key=bar\\=val", ExpectedList{
ExpectedItem{"foo=Key", "foo=val"},
ExpectedItem{"bar=Key", "bar=val"},
}},
{"mysql://user:[email protected]:3306/dbname?foo\\=Key\\&=\\&foo\\=val&bar\\=Key\\&=\\&bar\\=val", ExpectedList{
ExpectedItem{"foo=Key&", "&foo=val"},
ExpectedItem{"bar=Key&", "&bar=val"},
}},
{"mysql://user:[email protected]:3306/?fooKey=foo val&barKey=bar val", ExpectedList{
ExpectedItem{"fooKey", "foo val"},
ExpectedItem{"barKey", "bar val"},
}},
{"mysql://user:[email protected]", ExpectedList{}},
}
for testId, test := range tests {
dsn := Parse(test.dsn)
if len(test.expected) != len(dsn.GetParams()) {
t.Errorf("The number of results obtained is different from the expected. Test: %v.", testId+1)
}
for _, expected := range test.expected {
if dsn.GetParam(expected.key) != expected.value {
t.Errorf("Unexpected value in test \"%v\". Expected: \"%s\". Result: \"%s\"", testId+1, expected.value, dsn.GetParam(expected.key))
}
}
}
}