From bcc0c2a9f0be2ca796ef5206a78e283fe15e6186 Mon Sep 17 00:00:00 2001 From: Diego Nehab Date: Tue, 16 Mar 2004 06:42:53 +0000 Subject: New filter scheme. ltn12 and mime updated. smtp/ftp broken. --- test/httptest.lua | 45 +++++++++++++++++++++------------------------ test/mimetest.lua | 31 ++++++++++++++++++++----------- test/stufftest.lua | 19 +++++++++++++++++++ 3 files changed, 60 insertions(+), 35 deletions(-) create mode 100644 test/stufftest.lua (limited to 'test') diff --git a/test/httptest.lua b/test/httptest.lua index c9a74a8..04c0ed0 100644 --- a/test/httptest.lua +++ b/test/httptest.lua @@ -12,7 +12,7 @@ socket.http.TIMEOUT = 5 local t = socket.time() -host = host or "diego-interface2.student.dyn.CS.Princeton.EDU" +host = host or "diego.student.princeton.edu" proxy = proxy or "http://localhost:3128" prefix = prefix or "/luasocket-test" cgiprefix = cgiprefix or "/luasocket-test-cgi" @@ -71,8 +71,8 @@ local check_request = function(request, expect, ignore) check_result(response, expect, ignore) end -local check_request_cb = function(request, response, expect, ignore) - local response = socket.http.request_cb(request, response) +local check_request_cb = function(request, expect, ignore) + local response = socket.http.request_cb(request) check_result(response, expect, ignore) end @@ -83,7 +83,7 @@ local back, h, c, e = socket.http.get("http://" .. host .. forth) if not back then fail(e) end back = socket.url.parse(back) if similar(back.query, "this+is+the+query+string") then print("ok") -else fail() end +else fail(back.query) end ------------------------------------------------------------------------ io.write("testing query string correctness: ") @@ -168,31 +168,28 @@ back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index) check(back == index) ------------------------------------------------------------------------ -io.write("testing send.file and receive.file callbacks: ") +io.write("testing ltn12.(sink|source).file: ") request = { url = "http://" .. host .. cgiprefix .. "/cat", method = "POST", - body_cb = socket.callback.send.file(io.open(index_file, "r")), + source = ltn12.source.file(io.open(index_file, "r")), + sink = ltn12.sink.file(io.open(index_file .. "-back", "w")), headers = { ["content-length"] = string.len(index) } } -response = { - body_cb = socket.callback.receive.file(io.open(index_file .. "-back", "w")) -} expect = { code = 200 } ignore = { - body_cb = 1, status = 1, headers = 1 } -check_request_cb(request, response, expect, ignore) +check_request_cb(request, expect, ignore) back = readfile(index_file .. "-back") check(back == index) os.remove(index_file .. "-back") ------------------------------------------------------------------------ -io.write("testing send.chain and receive.chain callbacks: ") +io.write("testing ltn12.(sink|source).chain and mime.(encode|decode): ") local function b64length(len) local a = math.ceil(len/3)*4 @@ -200,26 +197,26 @@ local function b64length(len) return a + l*2 end -local req_cb = socket.callback.send.chain( - socket.callback.send.file(io.open(index_file, "r")), - socket.mime.chain( - socket.mime.encode("base64"), - socket.mime.wrap("base64") +local source = ltn12.source.chain( + ltn12.source.file(io.open(index_file, "r")), + ltn12.filter.chain( + mime.encode("base64"), + mime.wrap("base64") ) ) -local resp_cb = socket.callback.receive.chain( - socket.mime.decode("base64"), - socket.callback.receive.file(io.open(index_file .. "-back", "w")) +local sink = ltn12.sink.chain( + mime.decode("base64"), + ltn12.sink.file(io.open(index_file .. "-back", "w")) ) request = { url = "http://" .. host .. cgiprefix .. "/cat", method = "POST", - body_cb = req_cb, + source = source, + sink = sink, headers = { ["content-length"] = b64length(string.len(index)) } } -response = { body_cb = resp_cb } expect = { code = 200 } @@ -228,7 +225,7 @@ ignore = { status = 1, headers = 1 } -check_request_cb(request, response, expect, ignore) +check_request_cb(request, expect, ignore) back = readfile(index_file .. "-back") check(back == index) os.remove(index_file .. "-back") @@ -362,7 +359,7 @@ io.write("testing manual basic auth: ") request = { url = "http://" .. host .. prefix .. "/auth/index.html", headers = { - authorization = "Basic " .. (socket.mime.b64("luasocket:password")) + authorization = "Basic " .. (mime.b64("luasocket:password")) } } expect = { diff --git a/test/mimetest.lua b/test/mimetest.lua index 1a7e427..4a0a20a 100644 --- a/test/mimetest.lua +++ b/test/mimetest.lua @@ -31,18 +31,27 @@ local mao = [[ assim, nem tudo o que dava exprimia grande confiança. ]] +local function random(handle, io_err) + if handle then + return function() + local chunk = handle:read(math.random(0, 1024)) + if not chunk then handle:close() end + return chunk + end + else source.empty(io_err or "unable to open file") end +end + +local what = nil local function transform(input, output, filter) - local fi, err = io.open(input, "rb") - if not fi then fail(err) end - local fo, err = io.open(output, "wb") - if not fo then fail(err) end - while 1 do - local chunk = fi:read(math.random(0, 1024)) - fo:write(filter(chunk)) - if not chunk then break end - end - fi:close() - fo:close() + local source = random(io.open(input, "rb")) + local sink = ltn12.sink.file(io.open(output, "wb")) + if what then + sink = ltn12.sink.chain(filter, sink) + else + source = ltn12.source.chain(source, filter) + end + --what = not what + ltn12.pump(source, sink) end local function encode_qptest(mode) diff --git a/test/stufftest.lua b/test/stufftest.lua new file mode 100644 index 0000000..5eb8005 --- /dev/null +++ b/test/stufftest.lua @@ -0,0 +1,19 @@ +function test_dot(original, right) + local result, n = socket.smtp.dot(2, original) + assert(result == right, "->" .. result .. "<-") + print("ok") +end + +function test_stuff(original, right) + local result, n = socket.smtp.dot(2, original) + assert(result == right, "->" .. result .. "<-") + print("ok") +end + +test_dot("abc", "abc") +test_dot("", "") +test_dot("\r\n", "\r\n") +test_dot("\r\n.", "\r\n..") +test_dot(".\r\n.", "..\r\n..") +test_dot(".\r\n.", "..\r\n..") +test_dot("abcd.\r\n.", "abcd.\r\n..") -- cgit v1.2.3-55-g6feb