aboutsummaryrefslogtreecommitdiff
path: root/test/httptest.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/httptest.lua')
-rw-r--r--test/httptest.lua80
1 files changed, 33 insertions, 47 deletions
diff --git a/test/httptest.lua b/test/httptest.lua
index a70aa87..61dc60a 100644
--- a/test/httptest.lua
+++ b/test/httptest.lua
@@ -4,14 +4,18 @@
4-- to "/luasocket-test-cgi" and "/luasocket-test-cgi/" 4-- to "/luasocket-test-cgi" and "/luasocket-test-cgi/"
5-- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth 5-- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth
6 6
7require("http") 7socket = require("socket")
8http = require("http")
9mime = require("mime")
10url = require("url")
11ltn12 = require("ltn12")
8 12
9dofile("testsupport.lua") 13dofile("testsupport.lua")
10 14
11local host, proxy, request, response, index_file 15local host, proxy, request, response, index_file
12local ignore, expect, index, prefix, cgiprefix, index_crlf 16local ignore, expect, index, prefix, cgiprefix, index_crlf
13 17
14socket.http.TIMEOUT = 10 18http.TIMEOUT = 10
15 19
16local t = socket.time() 20local t = socket.time()
17 21
@@ -56,7 +60,7 @@ local check_request = function(request, expect, ignore)
56 end 60 end
57 request.source = request.source or 61 request.source = request.source or
58 (request.body and ltn12.source.string(request.body)) 62 (request.body and ltn12.source.string(request.body))
59 local response = socket.http.request(request) 63 local response = http.request(request)
60 if t and table.getn(t) > 0 then response.body = table.concat(t) end 64 if t and table.getn(t) > 0 then response.body = table.concat(t) end
61 check_result(response, expect, ignore) 65 check_result(response, expect, ignore)
62end 66end
@@ -64,16 +68,16 @@ end
64------------------------------------------------------------------------ 68------------------------------------------------------------------------
65io.write("testing request uri correctness: ") 69io.write("testing request uri correctness: ")
66local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string" 70local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
67local back, h, c, e = socket.http.get("http://" .. host .. forth) 71local back, h, c, e = http.get("http://" .. host .. forth)
68if not back then fail(e) end 72if not back then fail(e) end
69back = socket.url.parse(back) 73back = url.parse(back)
70if similar(back.query, "this+is+the+query+string") then print("ok") 74if similar(back.query, "this+is+the+query+string") then print("ok")
71else fail(back.query) end 75else fail(back.query) end
72 76
73------------------------------------------------------------------------ 77------------------------------------------------------------------------
74io.write("testing query string correctness: ") 78io.write("testing query string correctness: ")
75forth = "this+is+the+query+string" 79forth = "this+is+the+query+string"
76back = socket.http.get("http://" .. host .. cgiprefix .. 80back = http.get("http://" .. host .. cgiprefix ..
77 "/query-string?" .. forth) 81 "/query-string?" .. forth)
78if similar(back, forth) then print("ok") 82if similar(back, forth) then print("ok")
79else fail("failed!") end 83else fail("failed!") end
@@ -149,7 +153,7 @@ check_request(request, expect, ignore)
149 153
150------------------------------------------------------------------------ 154------------------------------------------------------------------------
151io.write("testing simple post function: ") 155io.write("testing simple post function: ")
152back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index) 156back = http.post("http://" .. host .. cgiprefix .. "/cat", index)
153assert(back == index) 157assert(back == index)
154 158
155------------------------------------------------------------------------ 159------------------------------------------------------------------------
@@ -278,30 +282,6 @@ ignore = {
278check_request(request, expect, ignore) 282check_request(request, expect, ignore)
279 283
280------------------------------------------------------------------------ 284------------------------------------------------------------------------
281io.write("testing host not found: ")
282request = {
283 url = "http://wronghost/does/not/exist"
284}
285local c, e = socket.connect("wronghost", 80)
286expect = {
287 error = e
288}
289ignore = {}
290check_request(request, expect, ignore)
291
292------------------------------------------------------------------------
293io.write("testing invalid url: ")
294request = {
295 url = host .. prefix
296}
297local c, e = socket.connect("", 80)
298expect = {
299 error = e
300}
301ignore = {}
302check_request(request, expect, ignore)
303
304------------------------------------------------------------------------
305io.write("testing document not found: ") 285io.write("testing document not found: ")
306request = { 286request = {
307 url = "http://" .. host .. "/wrongdocument.html" 287 url = "http://" .. host .. "/wrongdocument.html"
@@ -397,29 +377,16 @@ ignore = {
397check_request(request, expect, ignore) 377check_request(request, expect, ignore)
398 378
399------------------------------------------------------------------------ 379------------------------------------------------------------------------
400io.write("testing wrong scheme: ")
401request = {
402 url = "wrong://" .. host .. cgiprefix .. "/cat",
403 method = "GET"
404}
405expect = {
406 error = "unknown scheme 'wrong'"
407}
408ignore = {
409}
410check_request(request, expect, ignore)
411
412------------------------------------------------------------------------
413local body 380local body
414io.write("testing simple get function: ") 381io.write("testing simple get function: ")
415body = socket.http.get("http://" .. host .. prefix .. "/index.html") 382body = http.get("http://" .. host .. prefix .. "/index.html")
416assert(body == index) 383assert(body == index)
417print("ok") 384print("ok")
418 385
419------------------------------------------------------------------------ 386------------------------------------------------------------------------
420io.write("testing HEAD method: ") 387io.write("testing HEAD method: ")
421socket.http.TIMEOUT = 1 388http.TIMEOUT = 1
422response = socket.http.request { 389response = http.request {
423 method = "HEAD", 390 method = "HEAD",
424 url = "http://www.cs.princeton.edu/~diego/" 391 url = "http://www.cs.princeton.edu/~diego/"
425} 392}
@@ -427,6 +394,25 @@ assert(response and response.headers)
427print("ok") 394print("ok")
428 395
429------------------------------------------------------------------------ 396------------------------------------------------------------------------
397io.write("testing host not found: ")
398local c, e = socket.connect("wronghost", 80)
399local r, re = http.request{url = "http://wronghost/does/not/exist"}
400assert(r == nil and e == re)
401r, re = http.get("http://wronghost/does/not/exist")
402assert(r == nil and e == re)
403print("ok")
404
405------------------------------------------------------------------------
406io.write("testing invalid url: ")
407local c, e = socket.connect("", 80)
408local r, re = http.request{url = host .. prefix}
409assert(r == nil and e == re)
410r, re = http.get(host .. prefix)
411assert(r == nil and e == re)
412print("ok")
413
414------------------------------------------------------------------------
430print("passed all tests") 415print("passed all tests")
416os.remove("err")
431 417
432print(string.format("done in %.2fs", socket.time() - t)) 418print(string.format("done in %.2fs", socket.time() - t))