aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-15 06:24:00 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-06-15 06:24:00 +0000
commit58096449c6044b7aade5cd41cfd71c6bec1d273d (patch)
tree1814ffebe89c4c2556d84f97f66db37a7e8b4554 /test
parent9ed7f955e5fc69af9bf1794fa2c8cd227981ba24 (diff)
downloadluasocket-58096449c6044b7aade5cd41cfd71c6bec1d273d.tar.gz
luasocket-58096449c6044b7aade5cd41cfd71c6bec1d273d.tar.bz2
luasocket-58096449c6044b7aade5cd41cfd71c6bec1d273d.zip
Manual is almost done. HTTP is missing.
Implemented new distribution scheme. Select is now purely C. HTTP reimplemented seems faster dunno why. LTN12 functions that coroutines fail gracefully.
Diffstat (limited to 'test')
-rw-r--r--test/httptest.lua80
-rw-r--r--test/testclnt.lua2
-rw-r--r--test/testmesg.lua95
-rw-r--r--test/urltest.lua3
4 files changed, 86 insertions, 94 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))
diff --git a/test/testclnt.lua b/test/testclnt.lua
index 1825007..14d3484 100644
--- a/test/testclnt.lua
+++ b/test/testclnt.lua
@@ -69,7 +69,7 @@ function check_timeout(tm, sl, elapsed, err, opp, mode, alldone)
69 end 69 end
70end 70end
71 71
72if not socket.debug then 72if not socket.DEBUG then
73 fail("Please define LUASOCKET_DEBUG and recompile LuaSocket") 73 fail("Please define LUASOCKET_DEBUG and recompile LuaSocket")
74end 74end
75 75
diff --git a/test/testmesg.lua b/test/testmesg.lua
index 15b0eb7..449f4c2 100644
--- a/test/testmesg.lua
+++ b/test/testmesg.lua
@@ -1,52 +1,57 @@
1require("smtp") 1-- load the smtp support and its friends
2require("mime") 2local smtp = require("smtp")
3local mime = require("mime")
4local ltn12 = require("ltn12")
3 5
4mesgt = { 6-- creates a source to send a message with two parts. The first part is
5 headers = { 7-- plain text, the second part is a PNG image, encoded as base64.
6 to = "D Burgess <db@werx4.com>", 8source = smtp.message{
7 subject = "Looking good! (please check headers)" 9 headers = {
10 -- Remember that headers are *ignored* by smtp.send.
11 from = "Sicrano <sicrano@tecgraf.puc-rio.br>",
12 to = "Fulano <fulano@tecgraf.puc-rio.br>",
13 subject = "Here is a message with attachments"
14 },
15 body = {
16 preamble = "If your client doesn't understand attachments, \r\n" ..
17 "it will still display the preamble and the epilogue.\r\n",
18 "Preamble might show up even in a MIME enabled client.",
19 -- first part: No headers means plain text, us-ascii.
20 -- The mime.eol low-level filter normalizes end-of-line markers.
21 [1] = {
22 body = mime.eol(0, [[
23 Lines in a message body should always end with CRLF.
24 The smtp module will *NOT* perform translation. It will
25 perform necessary stuffing, though.
26 ]])
8 }, 27 },
9 body = { 28 -- second part: Headers describe content the to be an image,
10 preamble = "Some attatched stuff", 29 -- sent under the base64 transfer content encoding.
11 [1] = { 30 -- Notice that nothing happens until the message is sent. Small
12 body = mime.eol(0, "Testing stuffing.\n.\nGot you.\n.Hehehe.\n") 31 -- chunks are loaded into memory and translation happens on the fly.
13 }, 32 [2] = {
14 [2] = { 33 headers = {
15 headers = { 34 ["content-type"] = 'image/png; name="image.png"',
16 ["content-type"] = 'application/octet-stream; name="testmesg.lua"', 35 ["content-disposition"] = 'attachment; filename="image.png"',
17 ["content-disposition"] = 'attachment; filename="testmesg.lua"', 36 ["content-description"] = 'a beautiful image',
18 ["content-transfer-encoding"] = "BASE64" 37 ["content-transfer-encoding"] = "BASE64"
19 }, 38 },
20 body = ltn12.source.chain( 39 body = ltn12.source.chain(
21 ltn12.source.file(io.open("testmesg.lua", "rb")), 40 ltn12.source.file(io.open("image.png", "rb")),
22 ltn12.filter.chain( 41 ltn12.filter.chain(
23 mime.encode("base64"), 42 mime.encode("base64"),
24 mime.wrap() 43 mime.wrap()
25 ) 44 )
26 ) 45 )
27 }, 46 },
28 [3] = { 47 epilogue = "This might also show up, but after the attachments"
29 headers = { 48 }
30 ["content-type"] = 'text/plain; name="testmesg.lua"',
31 ["content-disposition"] = 'attachment; filename="testmesg.lua"',
32 ["content-transfer-encoding"] = "QUOTED-PRINTABLE"
33 },
34 body = ltn12.source.chain(
35 ltn12.source.file(io.open("testmesg.lua", "rb")),
36 ltn12.filter.chain(
37 mime.normalize(),
38 mime.encode("quoted-printable"),
39 mime.wrap("quoted-printable")
40 )
41 )
42 },
43 epilogue = "Done attaching stuff",
44 }
45} 49}
46 50
47print(socket.smtp.send { 51-- finally send it
52r, e = smtp.send{
48 rcpt = "<diego@cs.princeton.edu>", 53 rcpt = "<diego@cs.princeton.edu>",
49 from = "<diego@cs.princeton.edu>", 54 from = "<diego@cs.princeton.edu>",
50 source = socket.smtp.message(mesgt), 55 source = source,
51 server = "mail.cs.princeton.edu" 56 server = "mail.cs.princeton.edu"
52}) 57}
diff --git a/test/urltest.lua b/test/urltest.lua
index 8c2ee88..92f2fae 100644
--- a/test/urltest.lua
+++ b/test/urltest.lua
@@ -1,4 +1,5 @@
1require"url" 1socket = require("socket")
2socket.url = require("url")
2dofile("testsupport.lua") 3dofile("testsupport.lua")
3 4
4local check_build_url = function(parsed) 5local check_build_url = function(parsed)