aboutsummaryrefslogtreecommitdiff
path: root/test/httptest.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/httptest.lua')
-rw-r--r--test/httptest.lua43
1 files changed, 16 insertions, 27 deletions
diff --git a/test/httptest.lua b/test/httptest.lua
index 04c0ed0..ddeea50 100644
--- a/test/httptest.lua
+++ b/test/httptest.lua
@@ -8,7 +8,7 @@ dofile("noglobals.lua")
8local host, proxy, request, response, index_file 8local host, proxy, request, response, index_file
9local ignore, expect, index, prefix, cgiprefix, index_crlf 9local ignore, expect, index, prefix, cgiprefix, index_crlf
10 10
11socket.http.TIMEOUT = 5 11socket.http.TIMEOUT = 10
12 12
13local t = socket.time() 13local t = socket.time()
14 14
@@ -49,7 +49,9 @@ local check_result = function(response, expect, ignore)
49 for i,v in response do 49 for i,v in response do
50 if not ignore[i] then 50 if not ignore[i] then
51 if v ~= expect[i] then 51 if v ~= expect[i] then
52 print(string.sub(tostring(v), 1, 70)) 52 local f = io.open("err", "w")
53 f:write(tostring(v), "\n\n versus\n\n", tostring(expect[i]))
54 f:close()
53 fail(i .. " differs!") 55 fail(i .. " differs!")
54 end 56 end
55 end 57 end
@@ -57,8 +59,10 @@ local check_result = function(response, expect, ignore)
57 for i,v in expect do 59 for i,v in expect do
58 if not ignore[i] then 60 if not ignore[i] then
59 if v ~= response[i] then 61 if v ~= response[i] then
62 local f = io.open("err", "w")
63 f:write(tostring(response[i]), "\n\n versus\n\n", tostring(v))
60 v = string.sub(type(v) == "string" and v or "", 1, 70) 64 v = string.sub(type(v) == "string" and v or "", 1, 70)
61 print(string.sub(tostring(v), 1, 70)) 65 f:close()
62 fail(i .. " differs!") 66 fail(i .. " differs!")
63 end 67 end
64 end 68 end
@@ -67,12 +71,14 @@ local check_result = function(response, expect, ignore)
67end 71end
68 72
69local check_request = function(request, expect, ignore) 73local check_request = function(request, expect, ignore)
74 local t
75 if not request.sink then
76 request.sink, t = ltn12.sink.table(t)
77 end
78 request.source = request.source or
79 (request.body and ltn12.source.string(request.body))
70 local response = socket.http.request(request) 80 local response = socket.http.request(request)
71 check_result(response, expect, ignore) 81 if t and table.getn(t) > 0 then response.body = table.concat(t) end
72end
73
74local check_request_cb = function(request, expect, ignore)
75 local response = socket.http.request_cb(request)
76 check_result(response, expect, ignore) 82 check_result(response, expect, ignore)
77end 83end
78 84
@@ -183,7 +189,7 @@ ignore = {
183 status = 1, 189 status = 1,
184 headers = 1 190 headers = 1
185} 191}
186check_request_cb(request, expect, ignore) 192check_request(request, expect, ignore)
187back = readfile(index_file .. "-back") 193back = readfile(index_file .. "-back")
188check(back == index) 194check(back == index)
189os.remove(index_file .. "-back") 195os.remove(index_file .. "-back")
@@ -225,20 +231,12 @@ ignore = {
225 status = 1, 231 status = 1,
226 headers = 1 232 headers = 1
227} 233}
228check_request_cb(request, expect, ignore) 234check_request(request, expect, ignore)
229back = readfile(index_file .. "-back") 235back = readfile(index_file .. "-back")
230check(back == index) 236check(back == index)
231os.remove(index_file .. "-back") 237os.remove(index_file .. "-back")
232 238
233------------------------------------------------------------------------ 239------------------------------------------------------------------------
234io.write("testing simple post function with table args: ")
235back = socket.http.post {
236 url = "http://" .. host .. cgiprefix .. "/cat",
237 body = index
238}
239check(back == index)
240
241------------------------------------------------------------------------
242io.write("testing http redirection: ") 240io.write("testing http redirection: ")
243request = { 241request = {
244 url = "http://" .. host .. prefix 242 url = "http://" .. host .. prefix
@@ -439,15 +437,6 @@ body = socket.http.get("http://" .. host .. prefix .. "/index.html")
439check(body == index) 437check(body == index)
440 438
441------------------------------------------------------------------------ 439------------------------------------------------------------------------
442io.write("testing simple get function with table args: ")
443body = socket.http.get {
444 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
445 user = "luasocket",
446 password = "password"
447}
448check(body == index)
449
450------------------------------------------------------------------------
451io.write("testing HEAD method: ") 440io.write("testing HEAD method: ")
452socket.http.TIMEOUT = 1 441socket.http.TIMEOUT = 1
453response = socket.http.request { 442response = socket.http.request {