diff options
Diffstat (limited to '')
-rw-r--r-- | test/urltest.lua | 388 |
1 files changed, 388 insertions, 0 deletions
diff --git a/test/urltest.lua b/test/urltest.lua new file mode 100644 index 0000000..20a4f7d --- /dev/null +++ b/test/urltest.lua | |||
@@ -0,0 +1,388 @@ | |||
1 | function mysetglobal (varname, oldvalue, newvalue) | ||
2 | print("changing " .. varname) | ||
3 | %rawset(%globals(), varname, newvalue) | ||
4 | end | ||
5 | function mygetglobal (varname, newvalue) | ||
6 | print("checking " .. varname) | ||
7 | return %rawget(%globals(), varname) | ||
8 | end | ||
9 | settagmethod(tag(nil), "setglobal", mysetglobal) | ||
10 | settagmethod(tag(nil), "getglobal", mygetglobal) | ||
11 | |||
12 | assert(dofile "../lua/code.lua") | ||
13 | assert(dofile "../lua/url.lua") | ||
14 | |||
15 | local check_protect = function(parsed, path) | ||
16 | local built = URL.build_path(parsed) | ||
17 | if built ~= path then | ||
18 | print(built, path) | ||
19 | print("path composition failed.") | ||
20 | exit() | ||
21 | end | ||
22 | end | ||
23 | |||
24 | local check_invert = function(url) | ||
25 | local parsed = URL.parse_url(url) | ||
26 | parsed.path = URL.build_path(URL.parse_path(parsed.path)) | ||
27 | local rebuilt = URL.build_url(parsed) | ||
28 | if rebuilt ~= url then | ||
29 | print(url, rebuilt) | ||
30 | print("original and rebuilt are different") | ||
31 | exit() | ||
32 | end | ||
33 | end | ||
34 | |||
35 | local check_parse_path = function(path, expect) | ||
36 | local parsed = URL.parse_path(path) | ||
37 | for i = 1, max(getn(parsed), getn(expect)) do | ||
38 | if parsed[i] ~= expect[i] then | ||
39 | print(path) | ||
40 | write("segment: ", i, " = '", Code.hexa(tostring(parsed[i])), | ||
41 | "' but expected '", Code.hexa(tostring(expect[i])), "'\n") | ||
42 | exit() | ||
43 | end | ||
44 | end | ||
45 | if expect.is_directory ~= parsed.is_directory then | ||
46 | print(path) | ||
47 | print("is_directory mismatch") | ||
48 | exit() | ||
49 | end | ||
50 | if expect.is_absolute ~= parsed.is_absolute then | ||
51 | print(path) | ||
52 | print("is_absolute mismatch") | ||
53 | exit() | ||
54 | end | ||
55 | local built = URL.build_path(expect) | ||
56 | if built ~= path then | ||
57 | print(built, path) | ||
58 | print("path composition failed.") | ||
59 | exit() | ||
60 | end | ||
61 | end | ||
62 | |||
63 | local check_absolute_url = function(base, relative, absolute) | ||
64 | local res = URL.absolute_url(base, relative) | ||
65 | if res ~= absolute then | ||
66 | write("absolute: In test for '", relative, "' expected '", | ||
67 | absolute, "' but got '", res, "'\n") | ||
68 | exit() | ||
69 | end | ||
70 | end | ||
71 | |||
72 | local check_parse_url = function(gaba) | ||
73 | local url = gaba.url | ||
74 | gaba.url = nil | ||
75 | local parsed = URL.parse_url(url) | ||
76 | for i, v in gaba do | ||
77 | if v ~= parsed[i] then | ||
78 | write("parse: In test for '", url, "' expected ", i, " = '", | ||
79 | v, "' but got '", tostring(parsed[i]), "'\n") | ||
80 | for i,v in parsed do print(i,v) end | ||
81 | exit() | ||
82 | end | ||
83 | end | ||
84 | for i, v in parsed do | ||
85 | if v ~= gaba[i] then | ||
86 | write("parse: In test for '", url, "' expected ", i, " = '", | ||
87 | tostring(gaba[i]), "' but got '", v, "'\n") | ||
88 | for i,v in parsed do print(i,v) end | ||
89 | exit() | ||
90 | end | ||
91 | end | ||
92 | end | ||
93 | |||
94 | print("testing URL parsing") | ||
95 | check_parse_url{ | ||
96 | url = "scheme://userinfo@host:port/path;params?query#fragment", | ||
97 | scheme = "scheme", | ||
98 | authority = "userinfo@host:port", | ||
99 | host = "host", | ||
100 | port = "port", | ||
101 | userinfo = "userinfo", | ||
102 | user = "userinfo", | ||
103 | path = "/path", | ||
104 | params = "params", | ||
105 | query = "query", | ||
106 | fragment = "fragment" | ||
107 | } | ||
108 | |||
109 | check_parse_url{ | ||
110 | url = "scheme://user:password@host:port/path;params?query#fragment", | ||
111 | scheme = "scheme", | ||
112 | authority = "user:password@host:port", | ||
113 | host = "host", | ||
114 | port = "port", | ||
115 | userinfo = "user:password", | ||
116 | user = "user", | ||
117 | password = "password", | ||
118 | path = "/path", | ||
119 | params = "params", | ||
120 | query = "query", | ||
121 | fragment = "fragment", | ||
122 | } | ||
123 | |||
124 | check_parse_url{ | ||
125 | url = "scheme://userinfo@host:port/path;params?query#", | ||
126 | scheme = "scheme", | ||
127 | authority = "userinfo@host:port", | ||
128 | host = "host", | ||
129 | port = "port", | ||
130 | userinfo = "userinfo", | ||
131 | user = "userinfo", | ||
132 | path = "/path", | ||
133 | params = "params", | ||
134 | query = "query", | ||
135 | fragment = "" | ||
136 | } | ||
137 | |||
138 | check_parse_url{ | ||
139 | url = "scheme://userinfo@host:port/path;params?#fragment", | ||
140 | scheme = "scheme", | ||
141 | authority = "userinfo@host:port", | ||
142 | host = "host", | ||
143 | port = "port", | ||
144 | userinfo = "userinfo", | ||
145 | user = "userinfo", | ||
146 | path = "/path", | ||
147 | params = "params", | ||
148 | query = "", | ||
149 | fragment = "fragment" | ||
150 | } | ||
151 | |||
152 | check_parse_url{ | ||
153 | url = "scheme://userinfo@host:port/path;params#fragment", | ||
154 | scheme = "scheme", | ||
155 | authority = "userinfo@host:port", | ||
156 | host = "host", | ||
157 | port = "port", | ||
158 | userinfo = "userinfo", | ||
159 | user = "userinfo", | ||
160 | path = "/path", | ||
161 | params = "params", | ||
162 | fragment = "fragment" | ||
163 | } | ||
164 | |||
165 | check_parse_url{ | ||
166 | url = "scheme://userinfo@host:port/path;?query#fragment", | ||
167 | scheme = "scheme", | ||
168 | authority = "userinfo@host:port", | ||
169 | host = "host", | ||
170 | port = "port", | ||
171 | userinfo = "userinfo", | ||
172 | user = "userinfo", | ||
173 | path = "/path", | ||
174 | params = "", | ||
175 | query = "query", | ||
176 | fragment = "fragment" | ||
177 | } | ||
178 | |||
179 | check_parse_url{ | ||
180 | url = "scheme://userinfo@host:port/path?query#fragment", | ||
181 | scheme = "scheme", | ||
182 | authority = "userinfo@host:port", | ||
183 | host = "host", | ||
184 | port = "port", | ||
185 | userinfo = "userinfo", | ||
186 | user = "userinfo", | ||
187 | path = "/path", | ||
188 | query = "query", | ||
189 | fragment = "fragment" | ||
190 | } | ||
191 | |||
192 | check_parse_url{ | ||
193 | url = "scheme://userinfo@host:port/;params?query#fragment", | ||
194 | scheme = "scheme", | ||
195 | authority = "userinfo@host:port", | ||
196 | host = "host", | ||
197 | port = "port", | ||
198 | userinfo = "userinfo", | ||
199 | user = "userinfo", | ||
200 | path = "/", | ||
201 | params = "params", | ||
202 | query = "query", | ||
203 | fragment = "fragment" | ||
204 | } | ||
205 | |||
206 | check_parse_url{ | ||
207 | url = "scheme://userinfo@host:port", | ||
208 | scheme = "scheme", | ||
209 | authority = "userinfo@host:port", | ||
210 | host = "host", | ||
211 | port = "port", | ||
212 | userinfo = "userinfo", | ||
213 | user = "userinfo", | ||
214 | } | ||
215 | |||
216 | check_parse_url{ | ||
217 | url = "//userinfo@host:port/path;params?query#fragment", | ||
218 | authority = "userinfo@host:port", | ||
219 | host = "host", | ||
220 | port = "port", | ||
221 | userinfo = "userinfo", | ||
222 | user = "userinfo", | ||
223 | path = "/path", | ||
224 | params = "params", | ||
225 | query = "query", | ||
226 | fragment = "fragment" | ||
227 | } | ||
228 | |||
229 | check_parse_url{ | ||
230 | url = "//userinfo@host:port/path", | ||
231 | authority = "userinfo@host:port", | ||
232 | host = "host", | ||
233 | port = "port", | ||
234 | userinfo = "userinfo", | ||
235 | user = "userinfo", | ||
236 | path = "/path", | ||
237 | } | ||
238 | |||
239 | check_parse_url{ | ||
240 | url = "//userinfo@host/path", | ||
241 | authority = "userinfo@host", | ||
242 | host = "host", | ||
243 | userinfo = "userinfo", | ||
244 | user = "userinfo", | ||
245 | path = "/path", | ||
246 | } | ||
247 | |||
248 | check_parse_url{ | ||
249 | url = "//user:password@host/path", | ||
250 | authority = "user:password@host", | ||
251 | host = "host", | ||
252 | userinfo = "user:password", | ||
253 | password = "password", | ||
254 | user = "user", | ||
255 | path = "/path", | ||
256 | } | ||
257 | |||
258 | check_parse_url{ | ||
259 | url = "//user:@host/path", | ||
260 | authority = "user:@host", | ||
261 | host = "host", | ||
262 | userinfo = "user:", | ||
263 | password = "", | ||
264 | user = "user", | ||
265 | path = "/path", | ||
266 | } | ||
267 | |||
268 | check_parse_url{ | ||
269 | url = "//user@host:port/path", | ||
270 | authority = "user@host:port", | ||
271 | host = "host", | ||
272 | userinfo = "user", | ||
273 | user = "user", | ||
274 | port = "port", | ||
275 | path = "/path", | ||
276 | } | ||
277 | |||
278 | check_parse_url{ | ||
279 | url = "//host:port/path", | ||
280 | authority = "host:port", | ||
281 | port = "port", | ||
282 | host = "host", | ||
283 | path = "/path", | ||
284 | } | ||
285 | |||
286 | check_parse_url{ | ||
287 | url = "//host/path", | ||
288 | authority = "host", | ||
289 | host = "host", | ||
290 | path = "/path", | ||
291 | } | ||
292 | |||
293 | check_parse_url{ | ||
294 | url = "//host", | ||
295 | authority = "host", | ||
296 | host = "host", | ||
297 | } | ||
298 | |||
299 | check_parse_url{ | ||
300 | url = "/path", | ||
301 | path = "/path", | ||
302 | } | ||
303 | |||
304 | check_parse_url{ | ||
305 | url = "path", | ||
306 | path = "path", | ||
307 | } | ||
308 | |||
309 | -- standard RFC tests | ||
310 | print("testing absolute resolution") | ||
311 | check_absolute_url("http://a/b/c/d;p?q#f", "g:h", "g:h") | ||
312 | check_absolute_url("http://a/b/c/d;p?q#f", "g", "http://a/b/c/g") | ||
313 | check_absolute_url("http://a/b/c/d;p?q#f", "./g", "http://a/b/c/g") | ||
314 | check_absolute_url("http://a/b/c/d;p?q#f", "g/", "http://a/b/c/g/") | ||
315 | check_absolute_url("http://a/b/c/d;p?q#f", "/g", "http://a/g") | ||
316 | check_absolute_url("http://a/b/c/d;p?q#f", "//g", "http://g") | ||
317 | check_absolute_url("http://a/b/c/d;p?q#f", "?y", "http://a/b/c/d;p?y") | ||
318 | check_absolute_url("http://a/b/c/d;p?q#f", "g?y", "http://a/b/c/g?y") | ||
319 | check_absolute_url("http://a/b/c/d;p?q#f", "g?y/./x", "http://a/b/c/g?y/./x") | ||
320 | check_absolute_url("http://a/b/c/d;p?q#f", "#s", "http://a/b/c/d;p?q#s") | ||
321 | check_absolute_url("http://a/b/c/d;p?q#f", "g#s", "http://a/b/c/g#s") | ||
322 | check_absolute_url("http://a/b/c/d;p?q#f", "g#s/./x", "http://a/b/c/g#s/./x") | ||
323 | check_absolute_url("http://a/b/c/d;p?q#f", "g?y#s", "http://a/b/c/g?y#s") | ||
324 | check_absolute_url("http://a/b/c/d;p?q#f", ";x", "http://a/b/c/d;x") | ||
325 | check_absolute_url("http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x") | ||
326 | check_absolute_url("http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s") | ||
327 | check_absolute_url("http://a/b/c/d;p?q#f", ".", "http://a/b/c/") | ||
328 | check_absolute_url("http://a/b/c/d;p?q#f", "./", "http://a/b/c/") | ||
329 | check_absolute_url("http://a/b/c/d;p?q#f", "..", "http://a/b/") | ||
330 | check_absolute_url("http://a/b/c/d;p?q#f", "../", "http://a/b/") | ||
331 | check_absolute_url("http://a/b/c/d;p?q#f", "../g", "http://a/b/g") | ||
332 | check_absolute_url("http://a/b/c/d;p?q#f", "../..", "http://a/") | ||
333 | check_absolute_url("http://a/b/c/d;p?q#f", "../../", "http://a/") | ||
334 | check_absolute_url("http://a/b/c/d;p?q#f", "../../g", "http://a/g") | ||
335 | check_absolute_url("http://a/b/c/d;p?q#f", "", "http://a/b/c/d;p?q#f") | ||
336 | check_absolute_url("http://a/b/c/d;p?q#f", "/./g", "http://a/./g") | ||
337 | check_absolute_url("http://a/b/c/d;p?q#f", "/../g", "http://a/../g") | ||
338 | check_absolute_url("http://a/b/c/d;p?q#f", "g.", "http://a/b/c/g.") | ||
339 | check_absolute_url("http://a/b/c/d;p?q#f", ".g", "http://a/b/c/.g") | ||
340 | check_absolute_url("http://a/b/c/d;p?q#f", "g..", "http://a/b/c/g..") | ||
341 | check_absolute_url("http://a/b/c/d;p?q#f", "..g", "http://a/b/c/..g") | ||
342 | check_absolute_url("http://a/b/c/d;p?q#f", "./../g", "http://a/b/g") | ||
343 | check_absolute_url("http://a/b/c/d;p?q#f", "./g/.", "http://a/b/c/g/") | ||
344 | check_absolute_url("http://a/b/c/d;p?q#f", "g/./h", "http://a/b/c/g/h") | ||
345 | check_absolute_url("http://a/b/c/d;p?q#f", "g/../h", "http://a/b/c/h") | ||
346 | |||
347 | -- extra tests | ||
348 | check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f") | ||
349 | check_absolute_url("/a/b/c/d;p?q#f", "d/e/f", "/a/b/c/d/e/f") | ||
350 | check_absolute_url("a/b/c/d", "d/e/f", "a/b/c/d/e/f") | ||
351 | check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f") | ||
352 | check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html", | ||
353 | "http://velox.telemar.com.br/dashboard/index.html") | ||
354 | |||
355 | print("testing path parsing and composition") | ||
356 | check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 }) | ||
357 | check_parse_path("/eu/", { "eu"; is_absolute = 1, is_directory = 1 }) | ||
358 | check_parse_path("eu/tu/ele/nos/vos/eles/", | ||
359 | { "eu", "tu", "ele", "nos", "vos", "eles"; is_directory = 1}) | ||
360 | check_parse_path("/", { is_absolute = 1, is_directory = 1}) | ||
361 | check_parse_path("", { }) | ||
362 | check_parse_path("eu%01/%02tu/e%03l%04e/nos/vos%05/e%12les/", | ||
363 | { "eu\1", "\2tu", "e\3l\4e", "nos", "vos\5", "e\18les"; is_directory = 1}) | ||
364 | check_parse_path("eu/tu", { "eu", "tu" }) | ||
365 | |||
366 | print("testing path protection") | ||
367 | check_protect({ "eu", "-_.!~*'():@&=+$,", "tu" }, "eu/-_.!~*'():@&=+$,/tu") | ||
368 | check_protect({ "eu ", "~diego" }, "eu%20/~diego") | ||
369 | check_protect({ "/eu>", "<diego?" }, "%2feu%3e/%3cdiego%3f") | ||
370 | check_protect({ "\\eu]", "[diego`" }, "%5ceu%5d/%5bdiego%60") | ||
371 | check_protect({ "{eu}", "|diego\127" }, "%7beu%7d/%7cdiego%7f") | ||
372 | |||
373 | print("testing inversion") | ||
374 | check_invert("http:") | ||
375 | check_invert("a/b/c/d.html") | ||
376 | check_invert("//net_loc") | ||
377 | check_invert("http:a/b/d/c.html") | ||
378 | check_invert("//net_loc/a/b/d/c.html") | ||
379 | check_invert("http://net_loc/a/b/d/c.html") | ||
380 | check_invert("//who:isit@net_loc") | ||
381 | check_invert("http://he:man@boo.bar/a/b/c/i.html;type=moo?this=that#mark") | ||
382 | check_invert("/b/c/d#fragment") | ||
383 | check_invert("/b/c/d;param#fragment") | ||
384 | check_invert("/b/c/d;param?query#fragment") | ||
385 | check_invert("/b/c/d?query") | ||
386 | check_invert("/b/c/d;param?query") | ||
387 | |||
388 | print("the library passed all tests") | ||