aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-03-21 07:50:15 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-03-21 07:50:15 +0000
commit4919a83d2271a9e43b83c7d488e3f94c850681e3 (patch)
treea2ae9a27ead4cb85cbbd857ef74bc39c01b9bf64 /test
parent2a14ac4fe4bb4dd6d7a7ec5195c15a4e3f783ad5 (diff)
downloadluasocket-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.lua43
-rw-r--r--test/testclnt.lua34
-rw-r--r--test/testmesg.lua8
-rw-r--r--test/testsrvr.lua2
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")
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 {
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")
18end 18end
19 19
20pad = string.rep(" ", 8192)
21
22function remote(...) 20function 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()
29end 27end
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
124remote "data:send(str); data:close()" 122remote "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------------------------------------------------------------------------
187function test_totaltimeoutreceive(len, tm, sl) 191function 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)
204end 208end
205 209
206------------------------------------------------------------------------ 210------------------------------------------------------------------------
@@ -226,7 +230,7 @@ end
226------------------------------------------------------------------------ 230------------------------------------------------------------------------
227function test_blockingtimeoutreceive(len, tm, sl) 231function 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)
244end 248end
245 249
246------------------------------------------------------------------------ 250------------------------------------------------------------------------
@@ -298,7 +302,7 @@ end
298 302
299------------------------------------------------------------------------ 303------------------------------------------------------------------------
300function test_closed() 304function 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
462test("select function") 465test("select function")
@@ -481,6 +484,7 @@ accept_timeout()
481accept_errors() 484accept_errors()
482 485
483 486
487
484test("mixed patterns") 488test("mixed patterns")
485test_mixed(1) 489test_mixed(1)
486test_mixed(17) 490test_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
48print(socket.smtp.send { 48print(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
25print(command);
26
25 (loadstring(command))() 27 (loadstring(command))()
26 end 28 end
27end 29end