/
hackney_url_tests.erl
313 lines (299 loc) · 13.4 KB
/
hackney_url_tests.erl
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
309
310
311
312
313
% coding: latin-1
-module(hackney_url_tests).
-include_lib("eunit/include/eunit.hrl").
-include("hackney_lib.hrl").
parse_and_unparse_url_test_() ->
%% {Value, Result}.
Tests = [
{<<"http://www.example.com/path?key=value#Section%205">>,
#hackney_url{transport =hackney_tcp,
scheme = http,
netloc = <<"www.example.com">>,
raw_path = <<"/path?key=value#Section%205">>,
path = <<"/path">>,
qs = <<"key=value">>,
fragment = <<"Section%205">>,
host = "www.example.com",
port = 80,
user = <<"">>,
password = <<"">>}
},
{<<"http://www.example.com/">>,
#hackney_url{transport =hackney_tcp,
scheme = http,
netloc = <<"www.example.com">>,
raw_path = <<"/">>,
path = <<"/">>,
qs = <<"">>,
fragment = <<"">>,
host = "www.example.com",
port = 80,
user = <<"">>,
password = <<"">>}
},
{<<"http://[db8:0cec::99:123a]/">>,
#hackney_url{transport =hackney_tcp,
scheme = http,
netloc = <<"[db8:0cec::99:123a]">>,
raw_path = <<"/">>,
path = <<"/">>,
qs = <<"">>,
fragment = <<"">>,
host = "db8:0cec::99:123a",
port = 80,
user = <<"">>,
password = <<"">>}
},
{<<"https://[db8:0cec::99:123a]/">>,
#hackney_url{transport =hackney_ssl,
scheme = https,
netloc = <<"[db8:0cec::99:123a]">>,
raw_path = <<"/">>,
path = <<"/">>,
qs = <<"">>,
fragment = <<"">>,
host = "db8:0cec::99:123a",
port = 443,
user = <<"">>,
password = <<"">>}
},
{<<"https://[db8:0cec::99:123a]:8080/">>,
#hackney_url{transport =hackney_ssl,
scheme = https,
netloc = <<"[db8:0cec::99:123a]:8080">>,
raw_path = <<"/">>,
path = <<"/">>,
qs = <<"">>,
fragment = <<"">>,
host = "db8:0cec::99:123a",
port = 8080,
user = <<"">>,
password = <<"">>}
},
{<<"http://www.example.com/?key=value#Section%205">>,
#hackney_url{transport =hackney_tcp,
scheme = http,
netloc = <<"www.example.com">>,
raw_path = <<"/?key=value#Section%205">>,
path = <<"/">>,
qs = <<"key=value">>,
fragment = <<"Section%205">>,
host = "www.example.com",
port = 80,
user = <<"">>,
password = <<"">>}
},
{<<"http://www.example.com:8080/path?key=value#Section%205">>,
#hackney_url{transport =hackney_tcp,
scheme = http,
netloc = <<"www.example.com:8080">>,
raw_path = <<"/path?key=value#Section%205">>,
path = <<"/path">>,
qs = <<"key=value">>,
fragment = <<"Section%205">>,
host = "www.example.com",
port = 8080,
user = <<"">>,
password = <<"">>}
},
{<<"https://user:passwd@www.example.com/path?key=value#Section%205">>,
#hackney_url{transport =hackney_ssl,
scheme = https,
netloc = <<"www.example.com">>,
raw_path = <<"/path?key=value#Section%205">>,
path = <<"/path">>,
qs = <<"key=value">>,
fragment = <<"Section%205">>,
host = "www.example.com",
port = 443,
user = <<"user">>,
password = <<"passwd">>}
},
{<<"https://user@www.example.com/path?key=value#Section%205">>,
#hackney_url{transport =hackney_ssl,
scheme = https,
netloc = <<"www.example.com">>,
raw_path = <<"/path?key=value#Section%205">>,
path = <<"/path">>,
qs = <<"key=value">>,
fragment = <<"Section%205">>,
host = "www.example.com",
port = 443,
user = <<"user">>,
password = <<"">>}
},
{<<"http+unix://user@%2Fvar%2Frun%2Ftest.sock/path?key=value#Section%205">>,
#hackney_url{transport =hackney_local_tcp,
scheme = http_unix,
netloc = <<"%2Fvar%2Frun%2Ftest.sock">>,
raw_path = <<"/path?key=value#Section%205">>,
path = <<"/path">>,
qs = <<"key=value">>,
fragment = <<"Section%205">>,
host = "/var/run/test.sock",
port = 0,
user = <<"user">>,
password = <<"">>}
}
],
[{V, fun() -> R = hackney_url:parse_url(V) end} || {V, R} <- Tests] ++
[{V, fun() -> V = hackney_url:unparse_url(R) end} || {V, R} <- Tests].
parse_url_test_() ->
%% {Value, Result}.
Tests = [
{"http://www.example.com/path?key=value#Section%205", % list as argument
#hackney_url{transport =hackney_tcp,
scheme = http,
netloc = <<"www.example.com">>,
raw_path = <<"/path?key=value#Section%205">>,
path = <<"/path">>,
qs = <<"key=value">>,
fragment = <<"Section%205">>,
host = "www.example.com",
port = 80,
user = <<"">>,
password = <<"">>}
},
{<<"www.example.com/path?key=value#Section%205">>, % without http://
#hackney_url{transport =hackney_tcp,
scheme = http,
netloc = <<"www.example.com">>,
raw_path = <<"/path?key=value#Section%205">>,
path = <<"/path">>,
qs = <<"key=value">>,
fragment = <<"Section%205">>,
host = "www.example.com",
port = 80,
user = <<"">>,
password = <<"">>}
},
{<<"http://www.example.com">>,
#hackney_url{transport =hackney_tcp,
scheme = http,
netloc = <<"www.example.com">>,
raw_path = <<"">>,
path = <<"/">>,
qs = <<"">>,
fragment = <<"">>,
host = "www.example.com",
port = 80,
user = <<"">>,
password = <<"">>}
},
{<<"http://www.example.com?q=123">>,
#hackney_url{transport =hackney_tcp,
scheme = http,
netloc = <<"www.example.com">>,
raw_path = <<"?q=123">>,
path = <<"/">>,
qs = <<"q=123">>,
fragment = <<"">>,
host = "www.example.com",
port = 80,
user = <<"">>,
password = <<"">>}
}
],
[{V, fun() -> R = hackney_url:parse_url(V) end} || {V, R} <- Tests].
transport_scheme_test_() ->
%% {Value, Result}.
Tests = [
{hackney_tcp, http},
{hackney_ssl, https}
],
[{atom_to_list(V), fun() -> R = hackney_url:transport_scheme(V) end} || {V, R} <- Tests].
url_encode_and_decode_test_() ->
%% {Value, Result}.
Tests = [
{<<"HelloGünter">>, <<"HelloG%c3%bcnter">>},
{<<"Hello.-~_">>, <<"Hello.-~_">>},
{<<"€£©®ÀÁÂÃÄÅ">>, <<"%e2%82%ac%c2%a3%c2%a9%c2%ae%c3%80%c3%81%c3%82%c3%83%c3%84%c3%85">>}
],
[{V, fun() -> R = hackney_url:urlencode(V) end} || {V, R} <- Tests] ++
[{R, fun() -> V = hackney_url:urldecode(R) end} || {V, R} <- Tests].
url_encode_test_() ->
%% {{Url, Options}, Result}.
Tests = [
{{<<"HelloGünter">>, [upper]}, <<"HelloG%C3%BCnter">>},
{{<<"Hello+Günter">>, []}, <<"Hello%2bG%c3%bcnter">>},
{{<<"Hello ">>, []}, <<"Hello+">>},
{{<<"Hello ">>, [noplus]}, <<"Hello%20">>}
],
[{U, fun() -> R = hackney_url:urlencode(U, O) end} || {{U, O}, R} <- Tests].
url_decode_test_() ->
F = fun(U, O) ->
try hackney_url:urldecode(U, O) of
R -> R
catch Error:Reason -> {Error, Reason}
end
end,
%% {{Url, Options}, Result}.
Tests = [
{{<<"HelloG%C3%BCnter">>, skip}, <<"HelloGünter">>},
{{<<"Hello%KC">>, skip}, <<"Hello%KC">>},
{{<<"Hello%">>, skip}, <<"Hello%">>},
{{<<"Hello%">>, crash}, {error, badarg}},
{{<<"Hello%KC">>, crash}, {error, badarg}},
{{<<"Hello+">>, crash}, <<"Hello ">>}
],
[{U, fun() -> R = F(U, O) end} || {{U, O}, R} <- Tests].
parse_qs_test_() ->
%% {Value, Result}.
Tests = [
{<<"a=b">>, [{<<"a">>,<<"b">>}]},
{<<"a=b&c=d">>, [{<<"a">>,<<"b">>}, {<<"c">>, <<"d">>}]},
{<<"&a=b&&c=d&">>, [{<<"a">>,<<"b">>}, {<<"c">>, <<"d">>}]},
{<<"a=b&c">>, [{<<"a">>,<<"b">>}, {<<"c">>, true}]},
{<<"&a=b&c&&">>, [{<<"a">>,<<"b">>}, {<<"c">>, true}]}
],
[{V, fun() -> R = hackney_url:parse_qs(V) end} || {V, R} <- Tests].
qs_test_() ->
%% {Value, Result}.
Tests = [
{<<"a=b">>, [{<<"a">>,<<"b">>}]},
{<<"a=b&c=d">>, [{<<"a">>,<<"b">>}, {<<"c">>, <<"d">>}]}
],
[{V, fun() -> V = hackney_url:qs(R) end} || {V, R} <- Tests].
make_url_test_() ->
%% {Value, Result}.
Tests = [
{[<<"http://example.com">>, <<"path">>, [{<<"a">>, <<"b">>}]], <<"http://example.com/path?a=b">>},
{[<<"http://example.com">>, [<<"path1">>, <<"path2">>], []], <<"http://example.com/path1/path2">>}
],
[{R, fun() -> R = hackney_url:make_url(U, P, Q) end} || {[U, P, Q], R} <- Tests].
fix_path_test_() ->
%% {Value, Result}.
Tests = [
{"/path1/path2", <<"path1/path2">>},
{<<"path1/path2">>, <<"path1/path2">>},
{<<"path1/path2/">>, <<"path1/path2">>}
],
[{V, fun() -> R = hackney_url:fix_path(V) end} || {V, R} <- Tests].
pathencode_test_() ->
%% {Value, Result}.
Tests = [
{<<"/path1/path2">>, <<"/path1/path2">>},
{<<"/path1/path2%2fa">>, <<"/path1/path2%2fa">>},
{<<"/path1/path2%2fa%2fb">>, <<"/path1/path2%2fa%2fb">>},
{<<"/path1/path2%2test">>, <<"/path1/path2%252test">>},
{<<"/id/name:107/name2;p=1,3">>, <<"/id/name:107/name2;p=1,3">>},
{<<"/@foobar">>, <<"/@foobar">>}
],
[{V, fun() -> R = hackney_url:pathencode(V) end} || {V, R} <- Tests].
normalize_test_() ->
Tests = [
{"http://www.%E3%81%BB%E3%82%93%E3%81%A8%E3%81%86%E3%81%AB%E3%81%AA" ++
"%E3%81%8C%E3%81%84%E3%82%8F%E3%81%91%E3%81%AE%E3%82%8F%E3%81%8B%E3" ++
"%82%89%E3%81%AA%E3%81%84%E3%81%A9%E3%82%81%E3%81%84%E3%82%93%E3%82" ++
"%81%E3%81%84%E3%81%AE%E3%82%89%E3%81%B9%E3%82%8B%E3%81%BE%E3%81%A0" ++
"%E3%81%AA%E3%81%8C%E3%81%8F%E3%81%97%E3%81%AA%E3%81%84%E3%81%A8%E3" ++
"%81%9F%E3%82%8A%E3%81%AA%E3%81%84.w3.mag.keio.ac.jp",
<< "http://www.xn--n8jaaaaai5bhf7as8fsfk3jnknefdde3f",
"g11amb5gzdb4wi9bya3kc6lra.w3.mag.keio.ac.jp/" >>}],
[{V, fun() -> R = hackney_url:unparse_url(hackney_url:normalize(V))
end} || {V, R} <- Tests].
normalize2_test_() ->
Tests = [<<"http://www.詹姆斯.com/atomtests/iri/詹.html"/utf8>>,
<<"http://www.xn--8ws00zhy3a.com/atomtests/iri/%e8%a9%b9.html">>],
[{V, fun() -> R = hackney_url:unparse_url(hackney_url:normalize(V))
end} || {V, R} <- Tests].