From 58096449c6044b7aade5cd41cfd71c6bec1d273d Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Tue, 15 Jun 2004 06:24:00 +0000 Subject: 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. --- test/httptest.lua | 80 +++++++++++++++++++--------------------------- test/testclnt.lua | 2 +- test/testmesg.lua | 95 +++++++++++++++++++++++++++++-------------------------- test/urltest.lua | 3 +- 4 files changed, 86 insertions(+), 94 deletions(-) (limited to 'test') 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 @@ -- to "/luasocket-test-cgi" and "/luasocket-test-cgi/" -- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth -require("http") +socket = require("socket") +http = require("http") +mime = require("mime") +url = require("url") +ltn12 = require("ltn12") dofile("testsupport.lua") local host, proxy, request, response, index_file local ignore, expect, index, prefix, cgiprefix, index_crlf -socket.http.TIMEOUT = 10 +http.TIMEOUT = 10 local t = socket.time() @@ -56,7 +60,7 @@ local check_request = function(request, expect, ignore) end request.source = request.source or (request.body and ltn12.source.string(request.body)) - local response = socket.http.request(request) + local response = http.request(request) if t and table.getn(t) > 0 then response.body = table.concat(t) end check_result(response, expect, ignore) end @@ -64,16 +68,16 @@ end ------------------------------------------------------------------------ io.write("testing request uri correctness: ") local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string" -local back, h, c, e = socket.http.get("http://" .. host .. forth) +local back, h, c, e = http.get("http://" .. host .. forth) if not back then fail(e) end -back = socket.url.parse(back) +back = url.parse(back) if similar(back.query, "this+is+the+query+string") then print("ok") else fail(back.query) end ------------------------------------------------------------------------ io.write("testing query string correctness: ") forth = "this+is+the+query+string" -back = socket.http.get("http://" .. host .. cgiprefix .. +back = http.get("http://" .. host .. cgiprefix .. "/query-string?" .. forth) if similar(back, forth) then print("ok") else fail("failed!") end @@ -149,7 +153,7 @@ check_request(request, expect, ignore) ------------------------------------------------------------------------ io.write("testing simple post function: ") -back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index) +back = http.post("http://" .. host .. cgiprefix .. "/cat", index) assert(back == index) ------------------------------------------------------------------------ @@ -277,30 +281,6 @@ ignore = { } check_request(request, expect, ignore) ------------------------------------------------------------------------- -io.write("testing host not found: ") -request = { - url = "http://wronghost/does/not/exist" -} -local c, e = socket.connect("wronghost", 80) -expect = { - error = e -} -ignore = {} -check_request(request, expect, ignore) - ------------------------------------------------------------------------- -io.write("testing invalid url: ") -request = { - url = host .. prefix -} -local c, e = socket.connect("", 80) -expect = { - error = e -} -ignore = {} -check_request(request, expect, ignore) - ------------------------------------------------------------------------ io.write("testing document not found: ") request = { @@ -396,37 +376,43 @@ ignore = { } check_request(request, expect, ignore) ------------------------------------------------------------------------- -io.write("testing wrong scheme: ") -request = { - url = "wrong://" .. host .. cgiprefix .. "/cat", - method = "GET" -} -expect = { - error = "unknown scheme 'wrong'" -} -ignore = { -} -check_request(request, expect, ignore) - ------------------------------------------------------------------------ local body io.write("testing simple get function: ") -body = socket.http.get("http://" .. host .. prefix .. "/index.html") +body = http.get("http://" .. host .. prefix .. "/index.html") assert(body == index) print("ok") ------------------------------------------------------------------------ io.write("testing HEAD method: ") -socket.http.TIMEOUT = 1 -response = socket.http.request { +http.TIMEOUT = 1 +response = http.request { method = "HEAD", url = "http://www.cs.princeton.edu/~diego/" } assert(response and response.headers) print("ok") +------------------------------------------------------------------------ +io.write("testing host not found: ") +local c, e = socket.connect("wronghost", 80) +local r, re = http.request{url = "http://wronghost/does/not/exist"} +assert(r == nil and e == re) +r, re = http.get("http://wronghost/does/not/exist") +assert(r == nil and e == re) +print("ok") + +------------------------------------------------------------------------ +io.write("testing invalid url: ") +local c, e = socket.connect("", 80) +local r, re = http.request{url = host .. prefix} +assert(r == nil and e == re) +r, re = http.get(host .. prefix) +assert(r == nil and e == re) +print("ok") + ------------------------------------------------------------------------ print("passed all tests") +os.remove("err") print(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) end end -if not socket.debug then +if not socket.DEBUG then fail("Please define LUASOCKET_DEBUG and recompile LuaSocket") end 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 @@ -require("smtp") -require("mime") +-- load the smtp support and its friends +local smtp = require("smtp") +local mime = require("mime") +local ltn12 = require("ltn12") -mesgt = { - headers = { - to = "D Burgess ", - subject = "Looking good! (please check headers)" +-- creates a source to send a message with two parts. The first part is +-- plain text, the second part is a PNG image, encoded as base64. +source = smtp.message{ + headers = { + -- Remember that headers are *ignored* by smtp.send. + from = "Sicrano ", + to = "Fulano ", + subject = "Here is a message with attachments" + }, + body = { + preamble = "If your client doesn't understand attachments, \r\n" .. + "it will still display the preamble and the epilogue.\r\n", + "Preamble might show up even in a MIME enabled client.", + -- first part: No headers means plain text, us-ascii. + -- The mime.eol low-level filter normalizes end-of-line markers. + [1] = { + body = mime.eol(0, [[ + Lines in a message body should always end with CRLF. + The smtp module will *NOT* perform translation. It will + perform necessary stuffing, though. + ]]) }, - body = { - preamble = "Some attatched stuff", - [1] = { - body = mime.eol(0, "Testing stuffing.\n.\nGot you.\n.Hehehe.\n") - }, - [2] = { - headers = { - ["content-type"] = 'application/octet-stream; name="testmesg.lua"', - ["content-disposition"] = 'attachment; filename="testmesg.lua"', - ["content-transfer-encoding"] = "BASE64" - }, - body = ltn12.source.chain( - ltn12.source.file(io.open("testmesg.lua", "rb")), - ltn12.filter.chain( - mime.encode("base64"), - mime.wrap() - ) - ) - }, - [3] = { - headers = { - ["content-type"] = 'text/plain; name="testmesg.lua"', - ["content-disposition"] = 'attachment; filename="testmesg.lua"', - ["content-transfer-encoding"] = "QUOTED-PRINTABLE" - }, - body = ltn12.source.chain( - ltn12.source.file(io.open("testmesg.lua", "rb")), - ltn12.filter.chain( - mime.normalize(), - mime.encode("quoted-printable"), - mime.wrap("quoted-printable") - ) - ) - }, - epilogue = "Done attaching stuff", - } + -- second part: Headers describe content the to be an image, + -- sent under the base64 transfer content encoding. + -- Notice that nothing happens until the message is sent. Small + -- chunks are loaded into memory and translation happens on the fly. + [2] = { + headers = { + ["content-type"] = 'image/png; name="image.png"', + ["content-disposition"] = 'attachment; filename="image.png"', + ["content-description"] = 'a beautiful image', + ["content-transfer-encoding"] = "BASE64" + }, + body = ltn12.source.chain( + ltn12.source.file(io.open("image.png", "rb")), + ltn12.filter.chain( + mime.encode("base64"), + mime.wrap() + ) + ) + }, + epilogue = "This might also show up, but after the attachments" + } } -print(socket.smtp.send { +-- finally send it +r, e = smtp.send{ rcpt = "", from = "", - source = socket.smtp.message(mesgt), + source = source, server = "mail.cs.princeton.edu" -}) +} 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 @@ -require"url" +socket = require("socket") +socket.url = require("url") dofile("testsupport.lua") local check_build_url = function(parsed) -- cgit v1.2.3-55-g6feb