diff options
author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-21 07:50:15 +0000 |
---|---|---|
committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-21 07:50:15 +0000 |
commit | 4919a83d2271a9e43b83c7d488e3f94c850681e3 (patch) | |
tree | a2ae9a27ead4cb85cbbd857ef74bc39c01b9bf64 /test | |
parent | 2a14ac4fe4bb4dd6d7a7ec5195c15a4e3f783ad5 (diff) | |
download | luasocket-4919a83d2271a9e43b83c7d488e3f94c850681e3.tar.gz luasocket-4919a83d2271a9e43b83c7d488e3f94c850681e3.tar.bz2 luasocket-4919a83d2271a9e43b83c7d488e3f94c850681e3.zip |
Changed receive function. Now uniform with all other functions. Returns nil
on error, return partial result in the end.
http.lua rewritten.
Diffstat (limited to 'test')
-rw-r--r-- | test/httptest.lua | 43 | ||||
-rw-r--r-- | test/testclnt.lua | 34 | ||||
-rw-r--r-- | test/testmesg.lua | 8 | ||||
-rw-r--r-- | test/testsrvr.lua | 2 |
4 files changed, 41 insertions, 46 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 { |
diff --git a/test/testclnt.lua b/test/testclnt.lua index 1b64abd..ecf419b 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua | |||
@@ -17,14 +17,12 @@ function warn(...) | |||
17 | io.stderr:write("WARNING: ", s, "\n") | 17 | io.stderr:write("WARNING: ", s, "\n") |
18 | end | 18 | end |
19 | 19 | ||
20 | pad = string.rep(" ", 8192) | ||
21 | |||
22 | function remote(...) | 20 | function remote(...) |
23 | local s = string.format(unpack(arg)) | 21 | local s = string.format(unpack(arg)) |
24 | s = string.gsub(s, "\n", ";") | 22 | s = string.gsub(s, "\n", ";") |
25 | s = string.gsub(s, "%s+", " ") | 23 | s = string.gsub(s, "%s+", " ") |
26 | s = string.gsub(s, "^%s*", "") | 24 | s = string.gsub(s, "^%s*", "") |
27 | control:send(pad, s, "\n") | 25 | control:send(s, "\n") |
28 | control:receive() | 26 | control:receive() |
29 | end | 27 | end |
30 | 28 | ||
@@ -122,7 +120,13 @@ remote (string.format("str = data:receive(%d)", | |||
122 | sent, err = data:send(p1, p2, p3, p4) | 120 | sent, err = data:send(p1, p2, p3, p4) |
123 | if err then fail(err) end | 121 | if err then fail(err) end |
124 | remote "data:send(str); data:close()" | 122 | remote "data:send(str); data:close()" |
125 | bp1, bp2, bp3, bp4, err = data:receive("*l", "*l", string.len(p3), "*a") | 123 | bp1, err = data:receive() |
124 | if err then fail(err) end | ||
125 | bp2, err = data:receive() | ||
126 | if err then fail(err) end | ||
127 | bp3, err = data:receive(string.len(p3)) | ||
128 | if err then fail(err) end | ||
129 | bp4, err = data:receive("*a") | ||
126 | if err then fail(err) end | 130 | if err then fail(err) end |
127 | if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 and bp4 == p4 then | 131 | if bp1.."\n" == p1 and bp2.."\r\n" == p2 and bp3 == p3 and bp4 == p4 then |
128 | pass("patterns match") | 132 | pass("patterns match") |
@@ -186,7 +190,7 @@ end | |||
186 | ------------------------------------------------------------------------ | 190 | ------------------------------------------------------------------------ |
187 | function test_totaltimeoutreceive(len, tm, sl) | 191 | function test_totaltimeoutreceive(len, tm, sl) |
188 | reconnect() | 192 | reconnect() |
189 | local str, err, total | 193 | local str, err, partial |
190 | pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) | 194 | pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl) |
191 | remote (string.format ([[ | 195 | remote (string.format ([[ |
192 | data:settimeout(%d) | 196 | data:settimeout(%d) |
@@ -198,9 +202,9 @@ function test_totaltimeoutreceive(len, tm, sl) | |||
198 | data:send(str) | 202 | data:send(str) |
199 | ]], 2*tm, len, sl, sl)) | 203 | ]], 2*tm, len, sl, sl)) |
200 | data:settimeout(tm, "total") | 204 | data:settimeout(tm, "total") |
201 | str, err, elapsed = data:receive(2*len) | 205 | str, err, partial, elapsed = data:receive(2*len) |
202 | check_timeout(tm, sl, elapsed, err, "receive", "total", | 206 | check_timeout(tm, sl, elapsed, err, "receive", "total", |
203 | string.len(str) == 2*len) | 207 | string.len(str or partial) == 2*len) |
204 | end | 208 | end |
205 | 209 | ||
206 | ------------------------------------------------------------------------ | 210 | ------------------------------------------------------------------------ |
@@ -226,7 +230,7 @@ end | |||
226 | ------------------------------------------------------------------------ | 230 | ------------------------------------------------------------------------ |
227 | function test_blockingtimeoutreceive(len, tm, sl) | 231 | function test_blockingtimeoutreceive(len, tm, sl) |
228 | reconnect() | 232 | reconnect() |
229 | local str, err, total | 233 | local str, err, partial |
230 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) | 234 | pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl) |
231 | remote (string.format ([[ | 235 | remote (string.format ([[ |
232 | data:settimeout(%d) | 236 | data:settimeout(%d) |
@@ -238,9 +242,9 @@ function test_blockingtimeoutreceive(len, tm, sl) | |||
238 | data:send(str) | 242 | data:send(str) |
239 | ]], 2*tm, len, sl, sl)) | 243 | ]], 2*tm, len, sl, sl)) |
240 | data:settimeout(tm) | 244 | data:settimeout(tm) |
241 | str, err, elapsed = data:receive(2*len) | 245 | str, err, partial, elapsed = data:receive(2*len) |
242 | check_timeout(tm, sl, elapsed, err, "receive", "blocking", | 246 | check_timeout(tm, sl, elapsed, err, "receive", "blocking", |
243 | string.len(str) == 2*len) | 247 | string.len(str or partial) == 2*len) |
244 | end | 248 | end |
245 | 249 | ||
246 | ------------------------------------------------------------------------ | 250 | ------------------------------------------------------------------------ |
@@ -298,7 +302,7 @@ end | |||
298 | 302 | ||
299 | ------------------------------------------------------------------------ | 303 | ------------------------------------------------------------------------ |
300 | function test_closed() | 304 | function test_closed() |
301 | local back, err | 305 | local back, partial, err |
302 | local str = 'little string' | 306 | local str = 'little string' |
303 | reconnect() | 307 | reconnect() |
304 | pass("trying read detection") | 308 | pass("trying read detection") |
@@ -308,10 +312,10 @@ function test_closed() | |||
308 | data = nil | 312 | data = nil |
309 | ]], str)) | 313 | ]], str)) |
310 | -- try to get a line | 314 | -- try to get a line |
311 | back, err = data:receive() | 315 | back, err, partial = data:receive() |
312 | if not err then fail("shold have gotten 'closed'.") | 316 | if not err then fail("should have gotten 'closed'.") |
313 | elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") | 317 | elseif err ~= "closed" then fail("got '"..err.."' instead of 'closed'.") |
314 | elseif str ~= back then fail("didn't receive partial result.") | 318 | elseif str ~= partial then fail("didn't receive partial result.") |
315 | else pass("graceful 'closed' received") end | 319 | else pass("graceful 'closed' received") end |
316 | reconnect() | 320 | reconnect() |
317 | pass("trying write detection") | 321 | pass("trying write detection") |
@@ -456,7 +460,6 @@ test_methods(socket.udp(), { | |||
456 | "setpeername", | 460 | "setpeername", |
457 | "setsockname", | 461 | "setsockname", |
458 | "settimeout", | 462 | "settimeout", |
459 | "shutdown", | ||
460 | }) | 463 | }) |
461 | 464 | ||
462 | test("select function") | 465 | test("select function") |
@@ -481,6 +484,7 @@ accept_timeout() | |||
481 | accept_errors() | 484 | accept_errors() |
482 | 485 | ||
483 | 486 | ||
487 | |||
484 | test("mixed patterns") | 488 | test("mixed patterns") |
485 | test_mixed(1) | 489 | test_mixed(1) |
486 | test_mixed(17) | 490 | test_mixed(17) |
diff --git a/test/testmesg.lua b/test/testmesg.lua index 228bbe4..8b33133 100644 --- a/test/testmesg.lua +++ b/test/testmesg.lua | |||
@@ -6,7 +6,7 @@ mesgt = { | |||
6 | body = { | 6 | body = { |
7 | preamble = "Some attatched stuff", | 7 | preamble = "Some attatched stuff", |
8 | [1] = { | 8 | [1] = { |
9 | body = "Testing stuffing.\r\n.\r\nGot you.\r\n.Hehehe.\r\n" | 9 | body = mime.eol(0, "Testing stuffing.\n.\nGot you.\n.Hehehe.\n") |
10 | }, | 10 | }, |
11 | [2] = { | 11 | [2] = { |
12 | headers = { | 12 | headers = { |
@@ -29,7 +29,7 @@ mesgt = { | |||
29 | ["content-transfer-encoding"] = "QUOTED-PRINTABLE" | 29 | ["content-transfer-encoding"] = "QUOTED-PRINTABLE" |
30 | }, | 30 | }, |
31 | body = ltn12.source.chain( | 31 | body = ltn12.source.chain( |
32 | ltn12.source.file(io.open("message.lua", "rb")), | 32 | ltn12.source.file(io.open("testmesg.lua", "rb")), |
33 | ltn12.filter.chain( | 33 | ltn12.filter.chain( |
34 | mime.normalize(), | 34 | mime.normalize(), |
35 | mime.encode("quoted-printable"), | 35 | mime.encode("quoted-printable"), |
@@ -46,8 +46,8 @@ mesgt = { | |||
46 | -- ltn12.pump(source, sink) | 46 | -- ltn12.pump(source, sink) |
47 | 47 | ||
48 | print(socket.smtp.send { | 48 | print(socket.smtp.send { |
49 | rcpt = {"<db@werx4.com>", "<diego@cs.princeton.edu>"}, | 49 | rcpt = "<diego@cs.princeton.edu>", |
50 | from = "<diego@cs.princeton.edu>", | 50 | from = "<diego@cs.princeton.edu>", |
51 | source = socket.smtp.message(mesgt), | 51 | source = socket.smtp.message(mesgt), |
52 | server = "smtp.princeton.edu" | 52 | server = "mail.cs.princeton.edu" |
53 | }) | 53 | }) |
diff --git a/test/testsrvr.lua b/test/testsrvr.lua index 99b54e5..5c05239 100644 --- a/test/testsrvr.lua +++ b/test/testsrvr.lua | |||
@@ -22,6 +22,8 @@ while 1 do | |||
22 | print("server: closing connection...") | 22 | print("server: closing connection...") |
23 | break | 23 | break |
24 | end | 24 | end |
25 | print(command); | ||
26 | |||
25 | (loadstring(command))() | 27 | (loadstring(command))() |
26 | end | 28 | end |
27 | end | 29 | end |