aboutsummaryrefslogtreecommitdiff
path: root/test/urltest.lua
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--test/urltest.lua388
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 @@
1function mysetglobal (varname, oldvalue, newvalue)
2 print("changing " .. varname)
3 %rawset(%globals(), varname, newvalue)
4end
5function mygetglobal (varname, newvalue)
6 print("checking " .. varname)
7 return %rawget(%globals(), varname)
8end
9settagmethod(tag(nil), "setglobal", mysetglobal)
10settagmethod(tag(nil), "getglobal", mygetglobal)
11
12assert(dofile "../lua/code.lua")
13assert(dofile "../lua/url.lua")
14
15local 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
22end
23
24local 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
33end
34
35local 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
61end
62
63local 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
70end
71
72local 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
92end
93
94print("testing URL parsing")
95check_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
109check_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
124check_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
138check_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
152check_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
165check_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
179check_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
192check_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
206check_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
216check_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
229check_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
239check_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
248check_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
258check_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
268check_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
278check_parse_url{
279 url = "//host:port/path",
280 authority = "host:port",
281 port = "port",
282 host = "host",
283 path = "/path",
284}
285
286check_parse_url{
287 url = "//host/path",
288 authority = "host",
289 host = "host",
290 path = "/path",
291}
292
293check_parse_url{
294 url = "//host",
295 authority = "host",
296 host = "host",
297}
298
299check_parse_url{
300 url = "/path",
301 path = "/path",
302}
303
304check_parse_url{
305 url = "path",
306 path = "path",
307}
308
309-- standard RFC tests
310print("testing absolute resolution")
311check_absolute_url("http://a/b/c/d;p?q#f", "g:h", "g:h")
312check_absolute_url("http://a/b/c/d;p?q#f", "g", "http://a/b/c/g")
313check_absolute_url("http://a/b/c/d;p?q#f", "./g", "http://a/b/c/g")
314check_absolute_url("http://a/b/c/d;p?q#f", "g/", "http://a/b/c/g/")
315check_absolute_url("http://a/b/c/d;p?q#f", "/g", "http://a/g")
316check_absolute_url("http://a/b/c/d;p?q#f", "//g", "http://g")
317check_absolute_url("http://a/b/c/d;p?q#f", "?y", "http://a/b/c/d;p?y")
318check_absolute_url("http://a/b/c/d;p?q#f", "g?y", "http://a/b/c/g?y")
319check_absolute_url("http://a/b/c/d;p?q#f", "g?y/./x", "http://a/b/c/g?y/./x")
320check_absolute_url("http://a/b/c/d;p?q#f", "#s", "http://a/b/c/d;p?q#s")
321check_absolute_url("http://a/b/c/d;p?q#f", "g#s", "http://a/b/c/g#s")
322check_absolute_url("http://a/b/c/d;p?q#f", "g#s/./x", "http://a/b/c/g#s/./x")
323check_absolute_url("http://a/b/c/d;p?q#f", "g?y#s", "http://a/b/c/g?y#s")
324check_absolute_url("http://a/b/c/d;p?q#f", ";x", "http://a/b/c/d;x")
325check_absolute_url("http://a/b/c/d;p?q#f", "g;x", "http://a/b/c/g;x")
326check_absolute_url("http://a/b/c/d;p?q#f", "g;x?y#s", "http://a/b/c/g;x?y#s")
327check_absolute_url("http://a/b/c/d;p?q#f", ".", "http://a/b/c/")
328check_absolute_url("http://a/b/c/d;p?q#f", "./", "http://a/b/c/")
329check_absolute_url("http://a/b/c/d;p?q#f", "..", "http://a/b/")
330check_absolute_url("http://a/b/c/d;p?q#f", "../", "http://a/b/")
331check_absolute_url("http://a/b/c/d;p?q#f", "../g", "http://a/b/g")
332check_absolute_url("http://a/b/c/d;p?q#f", "../..", "http://a/")
333check_absolute_url("http://a/b/c/d;p?q#f", "../../", "http://a/")
334check_absolute_url("http://a/b/c/d;p?q#f", "../../g", "http://a/g")
335check_absolute_url("http://a/b/c/d;p?q#f", "", "http://a/b/c/d;p?q#f")
336check_absolute_url("http://a/b/c/d;p?q#f", "/./g", "http://a/./g")
337check_absolute_url("http://a/b/c/d;p?q#f", "/../g", "http://a/../g")
338check_absolute_url("http://a/b/c/d;p?q#f", "g.", "http://a/b/c/g.")
339check_absolute_url("http://a/b/c/d;p?q#f", ".g", "http://a/b/c/.g")
340check_absolute_url("http://a/b/c/d;p?q#f", "g..", "http://a/b/c/g..")
341check_absolute_url("http://a/b/c/d;p?q#f", "..g", "http://a/b/c/..g")
342check_absolute_url("http://a/b/c/d;p?q#f", "./../g", "http://a/b/g")
343check_absolute_url("http://a/b/c/d;p?q#f", "./g/.", "http://a/b/c/g/")
344check_absolute_url("http://a/b/c/d;p?q#f", "g/./h", "http://a/b/c/g/h")
345check_absolute_url("http://a/b/c/d;p?q#f", "g/../h", "http://a/b/c/h")
346
347-- extra tests
348check_absolute_url("//a/b/c/d;p?q#f", "d/e/f", "//a/b/c/d/e/f")
349check_absolute_url("/a/b/c/d;p?q#f", "d/e/f", "/a/b/c/d/e/f")
350check_absolute_url("a/b/c/d", "d/e/f", "a/b/c/d/e/f")
351check_absolute_url("a/b/c/d/../", "d/e/f", "a/b/c/d/e/f")
352check_absolute_url("http://velox.telemar.com.br", "/dashboard/index.html",
353 "http://velox.telemar.com.br/dashboard/index.html")
354
355print("testing path parsing and composition")
356check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 })
357check_parse_path("/eu/", { "eu"; is_absolute = 1, is_directory = 1 })
358check_parse_path("eu/tu/ele/nos/vos/eles/",
359 { "eu", "tu", "ele", "nos", "vos", "eles"; is_directory = 1})
360check_parse_path("/", { is_absolute = 1, is_directory = 1})
361check_parse_path("", { })
362check_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})
364check_parse_path("eu/tu", { "eu", "tu" })
365
366print("testing path protection")
367check_protect({ "eu", "-_.!~*'():@&=+$,", "tu" }, "eu/-_.!~*'():@&=+$,/tu")
368check_protect({ "eu ", "~diego" }, "eu%20/~diego")
369check_protect({ "/eu>", "<diego?" }, "%2feu%3e/%3cdiego%3f")
370check_protect({ "\\eu]", "[diego`" }, "%5ceu%5d/%5bdiego%60")
371check_protect({ "{eu}", "|diego\127" }, "%7beu%7d/%7cdiego%7f")
372
373print("testing inversion")
374check_invert("http:")
375check_invert("a/b/c/d.html")
376check_invert("//net_loc")
377check_invert("http:a/b/d/c.html")
378check_invert("//net_loc/a/b/d/c.html")
379check_invert("http://net_loc/a/b/d/c.html")
380check_invert("//who:isit@net_loc")
381check_invert("http://he:man@boo.bar/a/b/c/i.html;type=moo?this=that#mark")
382check_invert("/b/c/d#fragment")
383check_invert("/b/c/d;param#fragment")
384check_invert("/b/c/d;param?query#fragment")
385check_invert("/b/c/d?query")
386check_invert("/b/c/d;param?query")
387
388print("the library passed all tests")