diff options
Diffstat (limited to 'test/httptest.lua')
-rw-r--r-- | test/httptest.lua | 43 |
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") | |||
8 | local host, proxy, request, response, index_file | 8 | local host, proxy, request, response, index_file |
9 | local ignore, expect, index, prefix, cgiprefix, index_crlf | 9 | local ignore, expect, index, prefix, cgiprefix, index_crlf |
10 | 10 | ||
11 | socket.http.TIMEOUT = 5 | 11 | socket.http.TIMEOUT = 10 |
12 | 12 | ||
13 | local t = socket.time() | 13 | local 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) | |||
67 | end | 71 | end |
68 | 72 | ||
69 | local check_request = function(request, expect, ignore) | 73 | local 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 |
72 | end | ||
73 | |||
74 | local 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) |
77 | end | 83 | end |
78 | 84 | ||
@@ -183,7 +189,7 @@ ignore = { | |||
183 | status = 1, | 189 | status = 1, |
184 | headers = 1 | 190 | headers = 1 |
185 | } | 191 | } |
186 | check_request_cb(request, expect, ignore) | 192 | check_request(request, expect, ignore) |
187 | back = readfile(index_file .. "-back") | 193 | back = readfile(index_file .. "-back") |
188 | check(back == index) | 194 | check(back == index) |
189 | os.remove(index_file .. "-back") | 195 | os.remove(index_file .. "-back") |
@@ -225,20 +231,12 @@ ignore = { | |||
225 | status = 1, | 231 | status = 1, |
226 | headers = 1 | 232 | headers = 1 |
227 | } | 233 | } |
228 | check_request_cb(request, expect, ignore) | 234 | check_request(request, expect, ignore) |
229 | back = readfile(index_file .. "-back") | 235 | back = readfile(index_file .. "-back") |
230 | check(back == index) | 236 | check(back == index) |
231 | os.remove(index_file .. "-back") | 237 | os.remove(index_file .. "-back") |
232 | 238 | ||
233 | ------------------------------------------------------------------------ | 239 | ------------------------------------------------------------------------ |
234 | io.write("testing simple post function with table args: ") | ||
235 | back = socket.http.post { | ||
236 | url = "http://" .. host .. cgiprefix .. "/cat", | ||
237 | body = index | ||
238 | } | ||
239 | check(back == index) | ||
240 | |||
241 | ------------------------------------------------------------------------ | ||
242 | io.write("testing http redirection: ") | 240 | io.write("testing http redirection: ") |
243 | request = { | 241 | request = { |
244 | url = "http://" .. host .. prefix | 242 | url = "http://" .. host .. prefix |
@@ -439,15 +437,6 @@ body = socket.http.get("http://" .. host .. prefix .. "/index.html") | |||
439 | check(body == index) | 437 | check(body == index) |
440 | 438 | ||
441 | ------------------------------------------------------------------------ | 439 | ------------------------------------------------------------------------ |
442 | io.write("testing simple get function with table args: ") | ||
443 | body = socket.http.get { | ||
444 | url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html", | ||
445 | user = "luasocket", | ||
446 | password = "password" | ||
447 | } | ||
448 | check(body == index) | ||
449 | |||
450 | ------------------------------------------------------------------------ | ||
451 | io.write("testing HEAD method: ") | 440 | io.write("testing HEAD method: ") |
452 | socket.http.TIMEOUT = 1 | 441 | socket.http.TIMEOUT = 1 |
453 | response = socket.http.request { | 442 | response = socket.http.request { |