Changed the naming convention of the mime module.

Looks beautiful.
This commit is contained in:
Diego Nehab 2004-01-21 01:09:50 +00:00
parent 0b61b577f5
commit 3a7ac1e043
7 changed files with 234 additions and 117 deletions

View file

@ -5,8 +5,8 @@
-- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth
dofile("noglobals.lua")
local host, proxy, request, response
local ignore, expect, index, prefix, cgiprefix
local host, proxy, request, response, index_file
local ignore, expect, index, prefix, cgiprefix, index_crlf
socket.http.TIMEOUT = 5
@ -16,6 +16,7 @@ host = host or "diego.student.dyn.cs.princeton.edu"
proxy = proxy or "http://localhost:3128"
prefix = prefix or "/luasocket-test"
cgiprefix = cgiprefix or "/luasocket-test-cgi"
index_file = "test/index.html"
local readfile = function(name)
local f = io.open(name, "r")
@ -25,7 +26,8 @@ local readfile = function(name)
return s
end
index = readfile("test/index.html")
-- read index with CRLF convention
index = readfile(index_file)
local similar = function(s1, s2)
return string.lower(string.gsub(s1 or "", "%s", "")) ==
@ -42,13 +44,13 @@ local check = function (v, e)
if v then print("ok")
else fail(e) end
end
local check_request = function(request, expect, ignore)
local response = socket.http.request(request)
local check_result = function(response, expect, ignore)
for i,v in response do
if not ignore[i] then
if v ~= expect[i] then
if string.len(v) < 80 then print(v) end
v = string.sub(type(v) == "string" and v or "", 1, 70)
print(v)
fail(i .. " differs!")
end
end
@ -56,7 +58,8 @@ local check_request = function(request, expect, ignore)
for i,v in expect do
if not ignore[i] then
if v ~= response[i] then
if string.len(v) < 80 then print(v) end
v = string.sub(type(v) == "string" and v or "", 1, 70)
print(v)
fail(i .. " differs!")
end
end
@ -64,6 +67,17 @@ local check_request = function(request, expect, ignore)
print("ok")
end
local check_request = function(request, expect, ignore)
local response = socket.http.request(request)
check_result(response, expect, ignore)
end
local check_request_cb = function(request, response, expect, ignore)
local response = socket.http.request_cb(request, response)
check_result(response, expect, ignore)
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)
@ -72,12 +86,15 @@ back = socket.url.parse(back)
if similar(back.query, "this+is+the+query+string") then print("ok")
else fail() end
------------------------------------------------------------------------
io.write("testing query string correctness: ")
forth = "this+is+the+query+string"
back = socket.http.get("http://" .. host .. cgiprefix .. "/query-string?" .. forth)
back = socket.http.get("http://" .. host .. cgiprefix ..
"/query-string?" .. forth)
if similar(back, forth) then print("ok")
else fail("failed!") end
------------------------------------------------------------------------
io.write("testing document retrieval: ")
request = {
url = "http://" .. host .. prefix .. "/index.html"
@ -92,6 +109,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing redirect loop: ")
request = {
url = "http://" .. host .. cgiprefix .. "/redirect-loop"
@ -106,6 +124,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing post method: ")
-- wanted to test chunked post, but apache doesn't support it...
request = {
@ -125,6 +144,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing proxy with post method: ")
request = {
url = "http://" .. host .. cgiprefix .. "/cat",
@ -143,10 +163,78 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing simple post function: ")
back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index)
check(back == index)
------------------------------------------------------------------------
io.write("testing send.file and receive.file callbacks: ")
request = {
url = "http://" .. host .. cgiprefix .. "/cat",
method = "POST",
body_cb = socket.callback.send.file(io.open(index_file, "r")),
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)
back = readfile(index_file .. "-back")
check(back == index)
os.remove(index_file .. "-back")
------------------------------------------------------------------------
io.write("testing send.chain and receive.chain callbacks: ")
local function b64length(len)
local a = math.ceil(len/3)*4
local l = math.ceil(a/76)
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 resp_cb = socket.callback.receive.chain(
socket.mime.decode("base64"),
socket.callback.receive.file(io.open(index_file .. "-back", "w"))
)
request = {
url = "http://" .. host .. cgiprefix .. "/cat",
method = "POST",
body_cb = req_cb,
headers = { ["content-length"] = b64length(string.len(index)) }
}
response = { body_cb = resp_cb }
expect = {
code = 200
}
ignore = {
body_cb = 1,
status = 1,
headers = 1
}
check_request_cb(request, response, expect, ignore)
back = readfile(index_file .. "-back")
check(back == index)
os.remove(index_file .. "-back")
------------------------------------------------------------------------
io.write("testing simple post function with table args: ")
back = socket.http.post {
url = "http://" .. host .. cgiprefix .. "/cat",
@ -154,6 +242,7 @@ back = socket.http.post {
}
check(back == index)
------------------------------------------------------------------------
io.write("testing http redirection: ")
request = {
url = "http://" .. host .. prefix
@ -168,6 +257,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing proxy with redirection: ")
request = {
url = "http://" .. host .. prefix,
@ -183,7 +273,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing automatic auth failure: ")
request = {
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html"
@ -198,6 +288,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing http redirection failure: ")
request = {
url = "http://" .. host .. prefix,
@ -213,6 +304,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing host not found: ")
request = {
url = "http://wronghost/does/not/exist"
@ -224,6 +316,7 @@ expect = {
ignore = {}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing invalid url: ")
request = {
url = host .. prefix
@ -235,6 +328,7 @@ expect = {
ignore = {}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing document not found: ")
request = {
url = "http://" .. host .. "/wrongdocument.html"
@ -249,6 +343,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing auth failure: ")
request = {
url = "http://" .. host .. prefix .. "/auth/index.html"
@ -263,6 +358,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing manual basic auth: ")
request = {
url = "http://" .. host .. prefix .. "/auth/index.html",
@ -280,6 +376,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing automatic basic auth: ")
request = {
url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html"
@ -294,6 +391,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing auth info overriding: ")
request = {
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
@ -310,6 +408,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing cgi output retrieval (probably chunked...): ")
request = {
url = "http://" .. host .. cgiprefix .. "/cat-index-html"
@ -324,6 +423,7 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing wrong scheme: ")
request = {
url = "wrong://" .. host .. cgiprefix .. "/cat",
@ -336,11 +436,13 @@ ignore = {
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
local body
io.write("testing simple get function: ")
body = socket.http.get("http://" .. host .. prefix .. "/index.html")
check(body == index)
------------------------------------------------------------------------
io.write("testing simple get function with table args: ")
body = socket.http.get {
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
@ -349,6 +451,7 @@ body = socket.http.get {
}
check(body == index)
------------------------------------------------------------------------
io.write("testing HEAD method: ")
socket.http.TIMEOUT = 1
response = socket.http.request {
@ -357,6 +460,7 @@ response = socket.http.request {
}
check(response and response.headers)
------------------------------------------------------------------------
print("passed all tests")
print(string.format("done in %.2fs", socket.time() - t))

View file

@ -66,8 +66,8 @@ local function compare(input, output)
end
local function encode_qptest(mode)
local encode = socket.mime.qprint.encode(mode)
local split = socket.mime.qprint.split()
local encode = socket.mime.encode("quoted-printable", mode)
local split = socket.mime.wrap("quoted-printable")
local chain = socket.mime.chain(encode, split)
transform(qptest, eqptest, chain)
end
@ -77,7 +77,7 @@ local function compare_qptest()
end
local function decode_qptest()
local decode = socket.mime.qprint.decode()
local decode = socket.mime.decode("quoted-printable")
transform(eqptest, dqptest, decode)
end
@ -151,23 +151,23 @@ local function cleanup_qptest()
end
local function encode_b64test()
local e1 = socket.mime.base64.encode()
local e2 = socket.mime.base64.encode()
local e3 = socket.mime.base64.encode()
local e4 = socket.mime.base64.encode()
local sp4 = socket.mime.split()
local sp3 = socket.mime.split(59)
local sp2 = socket.mime.split(30)
local sp1 = socket.mime.split(27)
local e1 = socket.mime.encode("base64")
local e2 = socket.mime.encode("base64")
local e3 = socket.mime.encode("base64")
local e4 = socket.mime.encode("base64")
local sp4 = socket.mime.wrap("character")
local sp3 = socket.mime.wrap("character", 59)
local sp2 = socket.mime.wrap("character", 30)
local sp1 = socket.mime.wrap("character", 27)
local chain = socket.mime.chain(e1, sp1, e2, sp2, e3, sp3, e4, sp4)
transform(b64test, eb64test, chain)
end
local function decode_b64test()
local d1 = socket.mime.base64.decode()
local d2 = socket.mime.base64.decode()
local d3 = socket.mime.base64.decode()
local d4 = socket.mime.base64.decode()
local d1 = socket.mime.decode("base64")
local d2 = socket.mime.decode("base64")
local d3 = socket.mime.decode("base64")
local d4 = socket.mime.decode("base64")
local chain = socket.mime.chain(d1, d2, d3, d4)
transform(eb64test, db64test, chain)
end