Debian Codegraph @

texk/web2c/luatexdir/luasocket/test/urltest.lua

raw symbols SCIP
1 local socket = require("socket")
2 socket.url = require("socket.url")
3 dofile("testsupport.lua")
4
5 local check_build_url = function(parsed)
6 local built = socket.url.build(parsed)
7 if built ~= parsed.url then
8 print("built is different from expected")
9 print(built)
10 print(expected)
11 os.exit()
12 end
13 end
14
15 local check_protect = function(parsed, path, unsafe)
16 local built = socket.url.build_path(parsed, unsafe)
17 if built ~= path then
18 print(built, path)
19 print("path composition failed.")
20 os.exit()
21 end
22 end
23
24 local check_invert = function(url)
25 local parsed = socket.url.parse(url)
26 parsed.path = socket.url.build_path(socket.url.parse_path(parsed.path))
27 local rebuilt = socket.url.build(parsed)
28 if rebuilt ~= url then
29 print(url, rebuilt)
30 print("original and rebuilt are different")
31 os.exit()
32 end
33 end
34
35 local check_parse_path = function(path, expect)
36 local parsed = socket.url.parse_path(path)
37 for i = 1, math.max(#parsed, #expect) do
38 if parsed[i] ~= expect[i] then
39 print(path)
40 os.exit()
41 end
42 end
43 if expect.is_directory ~= parsed.is_directory then
44 print(path)
45 print("is_directory mismatch")
46 os.exit()
47 end
48 if expect.is_absolute ~= parsed.is_absolute then
49 print(path)
50 print("is_absolute mismatch")
51 os.exit()
52 end
53 local built = socket.url.build_path(expect)
54 if built ~= path then
55 print(built, path)
56 print("path composition failed.")
57 os.exit()
58 end
59 end
60
61 local check_absolute_url = function(base, relative, absolute)
62 local res = socket.url.absolute(base, relative)
63 if res ~= absolute then
64 io.write("absolute: In test for '", relative, "' expected '",
65 absolute, "' but got '", res, "'\n")
66 os.exit()
67 end
68 end
69
70 local check_parse_url = function(gaba)
71 local url = gaba.url
72 gaba.url = nil
73 local parsed = socket.url.parse(url)
74 for i, v in pairs(gaba) do
75 if v ~= parsed[i] then
76 io.write("parse: In test for '", url, "' expected ", i, " = '",
77 v, "' but got '", tostring(parsed[i]), "'\n")
78 for i,v in pairs(parsed) do print(i,v) end
79 os.exit()
80 end
81 end
82 for i, v in pairs(parsed) do
83 if v ~= gaba[i] then
84 io.write("parse: In test for '", url, "' expected ", i, " = '",
85 tostring(gaba[i]), "' but got '", v, "'\n")
86 for i,v in pairs(parsed) do print(i,v) end
87 os.exit()
88 end
89 end
90 end
91
92 print("testing URL parsing")
93 check_parse_url{
94 url = "scheme://userinfo@host:port/path;params?query#fragment",
95 scheme = "scheme",
96 authority = "userinfo@host:port",
97 host = "host",
98 port = "port",
99 userinfo = "userinfo",
100 user = "userinfo",
101 path = "/path",
102 params = "params",
103 query = "query",
104 fragment = "fragment"
105 }
106
107 check_parse_url{
108 url = "scheme://user:password@host:port/path;params?query#fragment",
109 scheme = "scheme",
110 authority = "user:password@host:port",
111 host = "host",
112 port = "port",
113 userinfo = "user:password",
114 user = "user",
115 password = "password",
116 path = "/path",
117 params = "params",
118 query = "query",
119 fragment = "fragment",
120 }
121
122 check_parse_url{
123 url = "scheme://userinfo@host:port/path;params?query#",
124 scheme = "scheme",
125 authority = "userinfo@host:port",
126 host = "host",
127 port = "port",
128 userinfo = "userinfo",
129 user = "userinfo",
130 path = "/path",
131 params = "params",
132 query = "query",
133 fragment = ""
134 }
135
136 check_parse_url{
137 url = "scheme://userinfo@host:port/path;params?#fragment",
138 scheme = "scheme",
139 authority = "userinfo@host:port",
140 host = "host",
141 port = "port",
142 userinfo = "userinfo",
143 user = "userinfo",
144 path = "/path",
145 params = "params",
146 query = "",
147 fragment = "fragment"
148 }
149
150 check_parse_url{
151 url = "scheme://userinfo@host:port/path;params#fragment",
152 scheme = "scheme",
153 authority = "userinfo@host:port",
154 host = "host",
155 port = "port",
156 userinfo = "userinfo",
157 user = "userinfo",
158 path = "/path",
159 params = "params",
160 fragment = "fragment"
161 }
162
163 check_parse_url{
164 url = "scheme://userinfo@host:port/path;?query#fragment",
165 scheme = "scheme",
166 authority = "userinfo@host:port",
167 host = "host",
168 port = "port",
169 userinfo = "userinfo",
170 user = "userinfo",
171 path = "/path",
172 params = "",
173 query = "query",
174 fragment = "fragment"
175 }
176
177 check_parse_url{
178 url = "scheme://userinfo@host:port/path?query#fragment",
179 scheme = "scheme",
180 authority = "userinfo@host:port",
181 host = "host",
182 port = "port",
183 userinfo = "userinfo",
184 user = "userinfo",
185 path = "/path",
186 query = "query",
187 fragment = "fragment"
188 }
189
190 check_parse_url{
191 url = "scheme://userinfo@host:port/;params?query#fragment",
192 scheme = "scheme",
193 authority = "userinfo@host:port",
194 host = "host",
195 port = "port",
196 userinfo = "userinfo",
197 user = "userinfo",
198 path = "/",
199 params = "params",
200 query = "query",
201 fragment = "fragment"
202 }
203
204 check_parse_url{
205 url = "scheme://userinfo@host:port",
206 scheme = "scheme",
207 authority = "userinfo@host:port",
208 host = "host",
209 port = "port",
210 userinfo = "userinfo",
211 user = "userinfo",
212 }
213
214 check_parse_url{
215 url = "//userinfo@host:port/path;params?query#fragment",
216 authority = "userinfo@host:port",
217 host = "host",
218 port = "port",
219 userinfo = "userinfo",
220 user = "userinfo",
221 path = "/path",
222 params = "params",
223 query = "query",
224 fragment = "fragment"
225 }
226
227 check_parse_url{
228 url = "//userinfo@host:port/path",
229 authority = "userinfo@host:port",
230 host = "host",
231 port = "port",
232 userinfo = "userinfo",
233 user = "userinfo",
234 path = "/path",
235 }
236
237 check_parse_url{
238 url = "//userinfo@host/path",
239 authority = "userinfo@host",
240 host = "host",
241 userinfo = "userinfo",
242 user = "userinfo",
243 path = "/path",
244 }
245
246 check_parse_url{
247 url = "//user:password@host/path",
248 authority = "user:password@host",
249 host = "host",
250 userinfo = "user:password",
251 password = "password",
252 user = "user",
253 path = "/path",
254 }
255
256 check_parse_url{
257 url = "//user:@host/path",
258 authority = "user:@host",
259 host = "host",
260 userinfo = "user:",
261 password = "",
262 user = "user",
263 path = "/path",
264 }
265
266 check_parse_url{
267 url = "//user@host:port/path",
268 authority = "user@host:port",
269 host = "host",
270 userinfo = "user",
271 user = "user",
272 port = "port",
273 path = "/path",
274 }
275
276 check_parse_url{
277 url = "//host:port/path",
278 authority = "host:port",
279 port = "port",
280 host = "host",
281 path = "/path",
282 }
283
284 check_parse_url{
285 url = "//host/path",
286 authority = "host",
287 host = "host",
288 path = "/path",
289 }
290
291 check_parse_url{
292 url = "//host",
293 authority = "host",
294 host = "host",
295 }
296
297 check_parse_url{
298 url = "/path",
299 path = "/path",
300 }
301
302 check_parse_url{
303 url = "path",
304 path = "path",
305 }
306
307 -- IPv6 tests
308
309 check_parse_url{
310 url = "http://[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80/index.html",
311 scheme = "http",
312 host = "FEDC:BA98:7654:3210:FEDC:BA98:7654:3210",
313 authority = "[FEDC:BA98:7654:3210:FEDC:BA98:7654:3210]:80",
314 port = "80",
315 path = "/index.html"
316 }
317
318 check_parse_url{
319 url = "http://[1080:0:0:0:8:800:200C:417A]/index.html",
320 scheme = "http",
321 host = "1080:0:0:0:8:800:200C:417A",
322 authority = "[1080:0:0:0:8:800:200C:417A]",
323 path = "/index.html"
324 }
325
326 check_parse_url{
327 url = "http://[3ffe:2a00:100:7031::1]",
328 scheme = "http",
329 host = "3ffe:2a00:100:7031::1",
330 authority = "[3ffe:2a00:100:7031::1]",
331 }
332
333 check_parse_url{
334 url = "http://[1080::8:800:200C:417A]/foo",
335 scheme = "http",
336 host = "1080::8:800:200C:417A",
337 authority = "[1080::8:800:200C:417A]",
338 path = "/foo"
339 }
340
341 check_parse_url{
342 url = "http://[::192.9.5.5]/ipng",
343 scheme = "http",
344 host = "::192.9.5.5",
345 authority = "[::192.9.5.5]",
346 path = "/ipng"
347 }
348
349 check_parse_url{
350 url = "http://[::FFFF:129.144.52.38]:80/index.html",
351 scheme = "http",
352 host = "::FFFF:129.144.52.38",
353 port = "80",
354 authority = "[::FFFF:129.144.52.38]:80",
355 path = "/index.html"
356 }
357
358 check_parse_url{
359 url = "http://[2010:836B:4179::836B:4179]",
360 scheme = "http",
361 host = "2010:836B:4179::836B:4179",
362 authority = "[2010:836B:4179::836B:4179]",
363 }
364
365 check_parse_url{
366 url = "//userinfo@[::FFFF:129.144.52.38]:port/path;params?query#fragment",
367 authority = "userinfo@[::FFFF:129.144.52.38]:port",
368 host = "::FFFF:129.144.52.38",
369 port = "port",
370 userinfo = "userinfo",
371 user = "userinfo",
372 path = "/path",
373 params = "params",
374 query = "query",
375 fragment = "fragment"
376 }
377
378 check_parse_url{
379 url = "scheme://user:password@[::192.9.5.5]:port/path;params?query#fragment",
380 scheme = "scheme",
381 authority = "user:password@[::192.9.5.5]:port",
382 host = "::192.9.5.5",
383 port = "port",
384 userinfo = "user:password",
385 user = "user",
386 password = "password",
387 path = "/path",
388 params = "params",
389 query = "query",
390 fragment = "fragment"
391 }
392
393 print("testing URL building")
394 check_build_url {
395 url = "scheme://user:password@host:port/path;params?query#fragment",
396 scheme = "scheme",
397 host = "host",
398 port = "port",
399 user = "user",
400 password = "password",
401 path = "/path",
402 params = "params",
403 query = "query",
404 fragment = "fragment"
405 }
406
407 check_build_url{
408 url = "//userinfo@[::FFFF:129.144.52.38]:port/path;params?query#fragment",
409 host = "::FFFF:129.144.52.38",
410 port = "port",
411 user = "userinfo",
412 path = "/path",
413 params = "params",
414 query = "query",
415 fragment = "fragment"
416 }
417
418 check_build_url{
419 url = "scheme://user:password@[::192.9.5.5]:port/path;params?query#fragment",
420 scheme = "scheme",
421 host = "::192.9.5.5",
422 port = "port",
423 user = "user",
424 password = "password",
425 path = "/path",
426 params = "params",
427 query = "query",
428 fragment = "fragment"
429 }
430
431 check_build_url {
432 url = "scheme://user:password@host/path;params?query#fragment",
433 scheme = "scheme",
434 host = "host",
435 user = "user",
436 password = "password",
437 path = "/path",
438 params = "params",
439 query = "query",
440 fragment = "fragment"
441 }
442
443 check_build_url {
444 url = "scheme://user@host/path;params?query#fragment",
445 scheme = "scheme",
446 host = "host",
447 user = "user",
448 path = "/path",
449 params = "params",
450 query = "query",
451 fragment = "fragment"
452 }
453
454 check_build_url {
455 url = "scheme://host/path;params?query#fragment",
456 scheme = "scheme",
457 host = "host",
458 path = "/path",
459 params = "params",
460 query = "query",
461 fragment = "fragment"
462 }
463
464 check_build_url {
465 url = "scheme://host/path;params#fragment",
466 scheme = "scheme",
467 host = "host",
468 path = "/path",
469 params = "params",
470 fragment = "fragment"
471 }
472
473 check_build_url {
474 url = "scheme://host/path#fragment",
475 scheme = "scheme",
476 host = "host",
477 path = "/path",
478 fragment = "fragment"
479 }
480
481 check_build_url {
482 url = "scheme://host/path",
483 scheme = "scheme",
484 host = "host",
485 path = "/path",
486 }
487
488 check_build_url {
489 url = "//host/path",
490 host = "host",
491 path = "/path",
492 }
493
494 check_build_url {
495 url = "/path",
496 path = "/path",
497 }
498
499 check_build_url {
500 url = "scheme://user:password@host:port/path;params?query#fragment",
501 scheme = "scheme",
502 host = "host",
503 port = "port",
504 user = "user",
505 userinfo = "not used",
506 password = "password",
507 path = "/path",
508 params = "params",
509 query = "query",
510 fragment = "fragment"
511 }
512
513 check_build_url {
514 url = "scheme://user:password@host:port/path;params?query#fragment",
515 scheme = "scheme",
516 host = "host",
517 port = "port",
518 user = "user",
519 userinfo = "not used",
520 authority = "not used",
521 password = "password",
522 path = "/path",
523 params = "params",
524 query = "query",
525 fragment = "fragment"
526 }
527
528 check_build_url {
529 url = "scheme://user:password@host:port/path;params?query#fragment",
530 scheme = "scheme",
531 host = "host",
532 port = "port",
533 userinfo = "user:password",
534 authority = "not used",
535 path = "/path",
536 params = "params",
537 query = "query",
538 fragment = "fragment"
539 }
540
541 check_build_url {
542 url = "scheme://user:password@host:port/path;params?query#fragment",
543 scheme = "scheme",
544 authority = "user:password@host:port",
545 path = "/path",
546 params = "params",
547 query = "query",
548 fragment = "fragment"
549 }
550
551 -- standard RFC tests
552 print("testing absolute resolution")
553 check_absolute_url("http://a/b/c/d;p?q#f", "g:h", "g:h")
554 check_absolute_url("http://a/b/c/d;p?q#f", "g", "http://a/b/c/g")
555 check_absolute_url("http://a/b/c/d;p?q#f", "./g", "http://a/b/c/g")
556 check_absolute_url("http://a/b/c/d;p?q#f", "g/", "http://a/b/c/g/")
557 check_absolute_url("http://a/b/c/d;p?q#f", "/g", "http://a/g")
558 check_absolute_url("http://a/b/c/d;p?q#f", "//g", "http://g")
559 check_absolute_url("http://a/b/c/d;p?q#f", "?y", "http://a/b/c/d;p?y")
560 check_absolute_url("http://a/b/c/d;p?q#f", "g?y", "http://a/b/c/g?y")
561 check_absolute_url("http://a/b/c/d;p?q#f", "g?y/./x", "http://a/b/c/g?y/./x")
562 check_absolute_url("http://a/b/c/d;p?q#f", "#s", "http://a/b/c/d;p?q#s")
563 check_absolute_url("http://a/b/c/d;p?q#f", "g#s", "http://a/b/c/g#s")
564 check_absolute_url("http://a/b/c/d;p?q#f", "g#s/./x", "http://a/b/c/g#s/./x")
565 check_absolute_url("http://a/b/c/d;p?q#f", "g?y#s", "http://a/b/c/g?y#s")
566 check_absolute_url("http://a/b/c/d;p?q#f", ";x", "http://a/b/c/d;x")
567 check_absolute_url("http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x")
568 check_absolute_url("http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s")
569 check_absolute_url("http://a/b/c/d;p?q#f", ".", "http://a/b/c/")
570 check_absolute_url("http://a/b/c/d;p?q#f", "./", "http://a/b/c/")
571 check_absolute_url("http://a/b/c/d;p?q#f", "..", "http://a/b/")
572 check_absolute_url("http://a/b/c/d;p?q#f", "../", "http://a/b/")
573 check_absolute_url("http://a/b/c/d;p?q#f", "../g", "http://a/b/g")
574 check_absolute_url("http://a/b/c/d;p?q#f", "../..", "http://a/")
575 check_absolute_url("http://a/b/c/d;p?q#f", "../../", "http://a/")
576 check_absolute_url("http://a/b/c/d;p?q#f", "../../g", "http://a/g")
577 check_absolute_url("http://a/b/c/d;p?q#f", "", "http://a/b/c/d;p?q#f")
578 check_absolute_url("http://a/b/c/d;p?q#f", "/./g", "http://a/./g")
579 check_absolute_url("http://a/b/c/d;p?q#f", "/../g", "http://a/../g")
580 check_absolute_url("http://a/b/c/d;p?q#f", "g.", "http://a/b/c/g.")
581 check_absolute_url("http://a/b/c/d;p?q#f", ".g", "http://a/b/c/.g")
582 check_absolute_url("http://a/b/c/d;p?q#f", "g..", "http://a/b/c/g..")
583 check_absolute_url("http://a/b/c/d;p?q#f", "..g", "http://a/b/c/..g")
584 check_absolute_url("http://a/b/c/d;p?q#f", "./../g", "http://a/b/g")
585 check_absolute_url("http://a/b/c/d;p?q#f", "./g/.", "http://a/b/c/g/")
586 check_absolute_url("http://a/b/c/d;p?q#f", "g/./h", "http://a/b/c/g/h")
587 check_absolute_url("http://a/b/c/d;p?q#f", "g/../h", "http://a/b/c/h")
588
589 -- extra tests
590 check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f")
591 check_absolute_url("/a/b/c/d;p?q#f", "d/e/f", "/a/b/c/d/e/f")
592 check_absolute_url("a/b/c/d", "d/e/f", "a/b/c/d/e/f")
593 check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f")
594 check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html",
595 "http://velox.telemar.com.br/dashboard/index.html")
596
597 print("testing path parsing and composition")
598 check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 })
599 check_parse_path("/eu/", { "eu"; is_absolute = 1, is_directory = 1 })
600 check_parse_path("eu/tu/ele/nos/vos/eles/",
601 { "eu", "tu", "ele", "nos", "vos", "eles"; is_directory = 1})
602 check_parse_path("/", { is_absolute = 1, is_directory = 1})
603 check_parse_path("", { })
604 check_parse_path("eu%01/%02tu/e%03l%04e/nos/vos%05/e%12les/",
605 { "eu\1", "\2tu", "e\3l\4e", "nos", "vos\5", "e\18les"; is_directory = 1})
606 check_parse_path("eu/tu", { "eu", "tu" })
607
608 print("testing path protection")
609 check_protect({ "eu", "-_.!~*'():@&=+$,", "tu" }, "eu/-_.!~*'():@&=+$,/tu")
610 check_protect({ "eu ", "~diego" }, "eu%20/~diego")
611 check_protect({ "/eu>", "<diego?" }, "%2feu%3e/%3cdiego%3f")
612 check_protect({ "\\eu]", "[diego`" }, "%5ceu%5d/%5bdiego%60")
613 check_protect({ "{eu}", "|diego\127" }, "%7beu%7d/%7cdiego%7f")
614 check_protect({ "eu ", "~diego" }, "eu /~diego", 1)
615 check_protect({ "/eu>", "<diego?" }, "/eu>/<diego?", 1)
616 check_protect({ "\\eu]", "[diego`" }, "\\eu]/[diego`", 1)
617 check_protect({ "{eu}", "|diego\127" }, "{eu}/|diego\127", 1)
618
619 print("testing inversion")
620 check_invert("http:")
621 check_invert("a/b/c/d.html")
622 check_invert("//net_loc")
623 check_invert("http:a/b/d/c.html")
624 check_invert("//net_loc/a/b/d/c.html")
625 check_invert("http://net_loc/a/b/d/c.html")
626 check_invert("//who:isit@net_loc")
627 check_invert("http://he:[email protected]/a/b/c/i.html;type=moo?this=that#mark")
628 check_invert("/b/c/d#fragment")
629 check_invert("/b/c/d;param#fragment")
630 check_invert("/b/c/d;param?query#fragment")
631 check_invert("/b/c/d?query")
632 check_invert("/b/c/d;param?query")
633 check_invert("http://he:man@[::192.168.1.1]/a/b/c/i.html;type=moo?this=that#mark")
634
635 print("the library passed all tests")