Debian Codegraph @

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

raw symbols SCIP
1 -- needs Alias from /home/c/diego/tec/luasocket/test to
2 -- "/luasocket-test" and "/luasocket-test/"
3 -- needs ScriptAlias from /home/c/diego/tec/luasocket/test/cgi
4 -- to "/luasocket-test-cgi" and "/luasocket-test-cgi/"
5 -- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth
6 local socket = require("socket")
7 local http = require("socket.http")
8 local url = require("socket.url")
9
10 local mime = require("mime")
11 local ltn12 = require("ltn12")
12
13 -- override protection to make sure we see all errors
14 -- socket.protect = function(s) return s end
15
16 dofile("testsupport.lua")
17
18 local host, proxy, request, response, index_file
19 local ignore, expect, index, prefix, cgiprefix, index_crlf
20
21 http.TIMEOUT = 10
22
23 local t = socket.gettime()
24
25 --host = host or "diego.student.princeton.edu"
26 --host = host or "diego.student.princeton.edu"
27 host = host or "localhost"
28 proxy = proxy or "http://localhost:3128"
29 prefix = prefix or "/luasocket-test"
30 cgiprefix = cgiprefix or "/luasocket-test-cgi"
31 index_file = "index.html"
32
33 -- read index with CRLF convention
34 index = readfile(index_file)
35
36 local check_result = function(response, expect, ignore)
37 for i,v in pairs(response) do
38 if not ignore[i] then
39 if v ~= expect[i] then
40 local f = io.open("err", "w")
41 f:write(tostring(v), "\n\n versus\n\n", tostring(expect[i]))
42 f:close()
43 fail(i .. " differs!")
44 end
45 end
46 end
47 for i,v in pairs(expect) do
48 if not ignore[i] then
49 if v ~= response[i] then
50 local f = io.open("err", "w")
51 f:write(tostring(response[i]), "\n\n versus\n\n", tostring(v))
52 v = string.sub(type(v) == "string" and v or "", 1, 70)
53 f:close()
54 fail(i .. " differs!")
55 end
56 end
57 end
58 print("ok")
59 end
60
61 local check_request = function(request, expect, ignore)
62 local t
63 if not request.sink then request.sink, t = ltn12.sink.table() end
64 request.source = request.source or
65 (request.body and ltn12.source.string(request.body))
66 local response = {}
67 response.code, response.headers, response.status =
68 socket.skip(1, http.request(request))
69 if t and #t > 0 then response.body = table.concat(t) end
70 check_result(response, expect, ignore)
71 end
72
73 ------------------------------------------------------------------------
74 io.write("testing request uri correctness: ")
75 local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
76 local back, c, h = http.request("http://" .. host .. forth)
77 if not back then fail(c) end
78 back = url.parse(back)
79 if similar(back.query, "this+is+the+query+string") then print("ok")
80 else fail(back.query) end
81
82 ------------------------------------------------------------------------
83 io.write("testing query string correctness: ")
84 forth = "this+is+the+query+string"
85 back = http.request("http://" .. host .. cgiprefix ..
86 "/query-string?" .. forth)
87 if similar(back, forth) then print("ok")
88 else fail("failed!") end
89
90 ------------------------------------------------------------------------
91 io.write("testing document retrieval: ")
92 request = {
93 url = "http://" .. host .. prefix .. "/index.html"
94 }
95 expect = {
96 body = index,
97 code = 200
98 }
99 ignore = {
100 status = 1,
101 headers = 1
102 }
103 check_request(request, expect, ignore)
104
105 ------------------------------------------------------------------------
106 io.write("testing redirect loop: ")
107 request = {
108 url = "http://" .. host .. cgiprefix .. "/redirect-loop"
109 }
110 expect = {
111 code = 302
112 }
113 ignore = {
114 status = 1,
115 headers = 1,
116 body = 1
117 }
118 check_request(request, expect, ignore)
119
120 ------------------------------------------------------------------------
121 io.write("testing invalid url: ")
122 local r, e = http.request{url = host .. prefix}
123 assert(r == nil and e == "invalid host ''")
124 r, re = http.request(host .. prefix)
125 assert(r == nil and e == re, tostring(r) ..", " .. tostring(re) ..
126 " vs " .. tostring(e))
127 print("ok")
128
129 io.write("testing invalid empty port: ")
130 request = {
131 url = "http://" .. host .. ":" .. prefix .. "/index.html"
132 }
133 expect = {
134 body = index,
135 code = 200
136 }
137 ignore = {
138 status = 1,
139 headers = 1
140 }
141 check_request(request, expect, ignore)
142
143 ------------------------------------------------------------------------
144 io.write("testing post method: ")
145 -- wanted to test chunked post, but apache doesn't support it...
146 request = {
147 url = "http://" .. host .. cgiprefix .. "/cat",
148 method = "POST",
149 body = index,
150 -- remove content-length header to send chunked body
151 headers = { ["content-length"] = string.len(index) }
152 }
153 expect = {
154 body = index,
155 code = 200
156 }
157 ignore = {
158 status = 1,
159 headers = 1
160 }
161 check_request(request, expect, ignore)
162
163 ------------------------------------------------------------------------
164 --[[
165 io.write("testing proxy with post method: ")
166 request = {
167 url = "http://" .. host .. cgiprefix .. "/cat",
168 method = "POST",
169 body = index,
170 headers = { ["content-length"] = string.len(index) },
171 proxy= proxy
172 }
173 expect = {
174 body = index,
175 code = 200
176 }
177 ignore = {
178 status = 1,
179 headers = 1
180 }
181 check_request(request, expect, ignore)
182 ]]
183
184 ------------------------------------------------------------------------
185 io.write("testing simple post function: ")
186 back = http.request("http://" .. host .. cgiprefix .. "/cat", index)
187 assert(back == index)
188 print("ok")
189
190 ------------------------------------------------------------------------
191 io.write("testing ltn12.(sink|source).file: ")
192 request = {
193 url = "http://" .. host .. cgiprefix .. "/cat",
194 method = "POST",
195 source = ltn12.source.file(io.open(index_file, "rb")),
196 sink = ltn12.sink.file(io.open(index_file .. "-back", "wb")),
197 headers = { ["content-length"] = string.len(index) }
198 }
199 expect = {
200 code = 200
201 }
202 ignore = {
203 status = 1,
204 headers = 1
205 }
206 check_request(request, expect, ignore)
207 back = readfile(index_file .. "-back")
208 assert(back == index)
209 os.remove(index_file .. "-back")
210
211 ------------------------------------------------------------------------
212 io.write("testing ltn12.(sink|source).chain and mime.(encode|decode): ")
213
214 local function b64length(len)
215 local a = math.ceil(len/3)*4
216 local l = math.ceil(a/76)
217 return a + l*2
218 end
219
220 local source = ltn12.source.chain(
221 ltn12.source.file(io.open(index_file, "rb")),
222 ltn12.filter.chain(
223 mime.encode("base64"),
224 mime.wrap("base64")
225 )
226 )
227
228 local sink = ltn12.sink.chain(
229 mime.decode("base64"),
230 ltn12.sink.file(io.open(index_file .. "-back", "wb"))
231 )
232
233 request = {
234 url = "http://" .. host .. cgiprefix .. "/cat",
235 method = "POST",
236 source = source,
237 sink = sink,
238 headers = { ["content-length"] = b64length(string.len(index)) }
239 }
240 expect = {
241 code = 200
242 }
243 ignore = {
244 body_cb = 1,
245 status = 1,
246 headers = 1
247 }
248 check_request(request, expect, ignore)
249 back = readfile(index_file .. "-back")
250 assert(back == index)
251 os.remove(index_file .. "-back")
252
253 ------------------------------------------------------------------------
254 io.write("testing http redirection: ")
255 request = {
256 url = "http://" .. host .. prefix
257 }
258 expect = {
259 body = index,
260 code = 200
261 }
262 ignore = {
263 status = 1,
264 headers = 1
265 }
266 check_request(request, expect, ignore)
267
268 ------------------------------------------------------------------------
269 --[[
270 io.write("testing proxy with redirection: ")
271 request = {
272 url = "http://" .. host .. prefix,
273 proxy = proxy
274 }
275 expect = {
276 body = index,
277 code = 200
278 }
279 ignore = {
280 status = 1,
281 headers = 1
282 }
283 check_request(request, expect, ignore)
284 ]]
285
286 ------------------------------------------------------------------------
287 io.write("testing automatic auth failure: ")
288 request = {
289 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html"
290 }
291 expect = {
292 code = 401
293 }
294 ignore = {
295 body = 1,
296 status = 1,
297 headers = 1
298 }
299 check_request(request, expect, ignore)
300
301 ------------------------------------------------------------------------
302 io.write("testing http redirection failure: ")
303 request = {
304 url = "http://" .. host .. prefix,
305 redirect = false
306 }
307 expect = {
308 code = 301
309 }
310 ignore = {
311 body = 1,
312 status = 1,
313 headers = 1
314 }
315 check_request(request, expect, ignore)
316
317 ------------------------------------------------------------------------
318 io.write("testing document not found: ")
319 request = {
320 url = "http://" .. host .. "/wrongdocument.html"
321 }
322 expect = {
323 code = 404
324 }
325 ignore = {
326 body = 1,
327 status = 1,
328 headers = 1
329 }
330 check_request(request, expect, ignore)
331
332 ------------------------------------------------------------------------
333 io.write("testing auth failure: ")
334 request = {
335 url = "http://" .. host .. prefix .. "/auth/index.html"
336 }
337 expect = {
338 code = 401
339 }
340 ignore = {
341 body = 1,
342 status = 1,
343 headers = 1
344 }
345 check_request(request, expect, ignore)
346
347 ------------------------------------------------------------------------
348 io.write("testing manual basic auth: ")
349 request = {
350 url = "http://" .. host .. prefix .. "/auth/index.html",
351 headers = {
352 authorization = "Basic " .. (mime.b64("luasocket:password"))
353 }
354 }
355 expect = {
356 code = 200,
357 body = index
358 }
359 ignore = {
360 status = 1,
361 headers = 1
362 }
363 check_request(request, expect, ignore)
364
365 ------------------------------------------------------------------------
366 io.write("testing automatic basic auth: ")
367 request = {
368 url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html"
369 }
370 expect = {
371 code = 200,
372 body = index
373 }
374 ignore = {
375 status = 1,
376 headers = 1
377 }
378 check_request(request, expect, ignore)
379
380 ------------------------------------------------------------------------
381 io.write("testing auth info overriding: ")
382 request = {
383 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
384 user = "luasocket",
385 password = "password"
386 }
387 expect = {
388 code = 200,
389 body = index
390 }
391 ignore = {
392 status = 1,
393 headers = 1
394 }
395 check_request(request, expect, ignore)
396
397 ------------------------------------------------------------------------
398 io.write("testing cgi output retrieval (probably chunked...): ")
399 request = {
400 url = "http://" .. host .. cgiprefix .. "/cat-index-html"
401 }
402 expect = {
403 body = index,
404 code = 200
405 }
406 ignore = {
407 status = 1,
408 headers = 1
409 }
410 check_request(request, expect, ignore)
411
412 ------------------------------------------------------------------------
413 local body
414 io.write("testing simple request function: ")
415 body = http.request("http://" .. host .. prefix .. "/index.html")
416 assert(body == index)
417 print("ok")
418
419 ------------------------------------------------------------------------
420 io.write("testing HEAD method: ")
421 local r, c, h = http.request {
422 method = "HEAD",
423 url = "http://www.tecgraf.puc-rio.br/~diego/"
424 }
425 assert(r and h and (c == 200), c)
426 print("ok")
427
428 ------------------------------------------------------------------------
429 io.write("testing host not found: ")
430 local c, e = socket.connect("example.invalid", 80)
431 local r, re = http.request{url = "http://example.invalid/does/not/exist"}
432 assert(r == nil and e == re, tostring(r) .. " " .. tostring(re))
433 r, re = http.request("http://example.invalid/does/not/exist")
434 assert(r == nil and e == re)
435 print("ok")
436
437 ------------------------------------------------------------------------
438 print("passed all tests")
439 os.remove("err")
440
441 print(string.format("done in %.2fs", socket.gettime() - t))