2002-07-08 23:54:28 +02:00
|
|
|
-- needs Alias from /home/c/diego/tec/luasocket/test to
|
2004-01-16 08:06:31 +01:00
|
|
|
-- "/luasocket-test" and "/luasocket-test/"
|
2002-07-08 23:54:28 +02:00
|
|
|
-- needs ScriptAlias from /home/c/diego/tec/luasocket/test/cgi
|
2004-01-16 08:06:31 +01:00
|
|
|
-- to "/luasocket-test-cgi" and "/luasocket-test-cgi/"
|
|
|
|
-- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth
|
2001-09-25 23:34:43 +02:00
|
|
|
local similar = function(s1, s2)
|
2002-12-03 08:20:34 +01:00
|
|
|
return string.lower(string.gsub(s1 or "", "%s", "")) ==
|
|
|
|
string.lower(string.gsub(s2 or "", "%s", ""))
|
2001-09-25 23:34:43 +02:00
|
|
|
end
|
2001-01-25 22:59:39 +01:00
|
|
|
|
2001-09-25 23:34:43 +02:00
|
|
|
local fail = function(s)
|
|
|
|
s = s or "failed!"
|
|
|
|
print(s)
|
2002-12-03 08:20:34 +01:00
|
|
|
os.exit()
|
2001-01-25 22:59:39 +01:00
|
|
|
end
|
|
|
|
|
2001-09-25 23:34:43 +02:00
|
|
|
local readfile = function(name)
|
2002-12-03 08:20:34 +01:00
|
|
|
local f = io.open(name, "r")
|
2001-09-25 23:34:43 +02:00
|
|
|
if not f then return nil end
|
2002-12-03 08:20:34 +01:00
|
|
|
local s = f:read("*a")
|
|
|
|
f:close()
|
2001-09-25 23:34:43 +02:00
|
|
|
return s
|
2001-01-25 22:59:39 +01:00
|
|
|
end
|
|
|
|
|
2001-09-25 23:34:43 +02:00
|
|
|
local check = function (v, e)
|
|
|
|
if v then print("ok")
|
2003-05-25 03:54:13 +02:00
|
|
|
else fail(e) end
|
2001-09-25 23:34:43 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local check_request = function(request, expect, ignore)
|
2003-03-20 01:24:44 +01:00
|
|
|
local response = socket.http.request(request)
|
2001-09-25 23:34:43 +02:00
|
|
|
for i,v in response do
|
|
|
|
if not ignore[i] then
|
2004-01-16 08:06:31 +01:00
|
|
|
if v ~= expect[i] then
|
|
|
|
if string.len(v) < 80 then print(v) end
|
|
|
|
fail(i .. " differs!")
|
|
|
|
end
|
2001-09-25 23:34:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
for i,v in expect do
|
|
|
|
if not ignore[i] then
|
2004-01-16 08:06:31 +01:00
|
|
|
if v ~= response[i] then
|
|
|
|
if string.len(v) < 80 then print(v) end
|
|
|
|
fail(i .. " differs!")
|
|
|
|
end
|
2001-09-25 23:34:43 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
print("ok")
|
|
|
|
end
|
|
|
|
|
2003-05-25 03:54:13 +02:00
|
|
|
local host, request, response, ignore, expect, index, prefix, cgiprefix
|
2001-09-25 23:34:43 +02:00
|
|
|
|
2003-05-25 03:54:13 +02:00
|
|
|
local t = socket.time()
|
2001-09-25 23:34:43 +02:00
|
|
|
|
2003-03-28 23:41:26 +01:00
|
|
|
host = host or "localhost"
|
2004-01-16 08:06:31 +01:00
|
|
|
prefix = prefix or "/luasocket-test"
|
|
|
|
cgiprefix = cgiprefix or "/luasocket-test-cgi"
|
2002-12-03 08:20:34 +01:00
|
|
|
index = readfile("test/index.html")
|
2001-09-25 23:34:43 +02:00
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing request uri correctness: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
|
2004-01-16 08:06:31 +01:00
|
|
|
local back, h, c, e = socket.http.get("http://" .. host .. forth)
|
2001-09-25 23:34:43 +02:00
|
|
|
if similar(back, forth) then print("ok")
|
2004-01-16 08:06:31 +01:00
|
|
|
else
|
|
|
|
print(h, c, e)
|
|
|
|
fail()
|
|
|
|
end
|
2001-01-25 22:59:39 +01:00
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing query string correctness: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
forth = "this+is+the+query+string"
|
2003-03-28 23:41:26 +01:00
|
|
|
back = socket.http.get("http://" .. host .. cgiprefix .. "/query-string?" .. forth)
|
2001-09-25 23:34:43 +02:00
|
|
|
if similar(back, forth) then print("ok")
|
|
|
|
else fail("failed!") end
|
2001-01-25 22:59:39 +01:00
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing document retrieval: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://" .. host .. prefix .. "/index.html"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
body = index,
|
|
|
|
code = 200
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-16 08:06:31 +01:00
|
|
|
socket.http.get("http://" .. host .. prefix .. "/lixo.html")
|
|
|
|
|
|
|
|
io.write("testing post method: ")
|
|
|
|
-- wanted to test chunked post, but apache doesn't support it...
|
|
|
|
request = {
|
|
|
|
url = "http://" .. host .. cgiprefix .. "/cat",
|
|
|
|
method = "POST",
|
|
|
|
body = index,
|
|
|
|
-- remove content-length header to send chunked body
|
|
|
|
headers = { ["content-length"] = string.len(index) }
|
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
body = index,
|
|
|
|
code = 200
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
|
|
|
io.write("testing simple post function: ")
|
|
|
|
body = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index)
|
|
|
|
check(body == index)
|
|
|
|
|
|
|
|
io.write("testing simple post function with table args: ")
|
|
|
|
body = socket.http.post {
|
|
|
|
url = "http://" .. host .. cgiprefix .. "/cat",
|
|
|
|
body = index
|
|
|
|
}
|
|
|
|
check(body == index)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing http redirection: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://" .. host .. prefix
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
body = index,
|
|
|
|
code = 200
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing automatic auth failure: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
code = 401
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
body = 1,
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing http redirection failure: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://" .. host .. prefix,
|
2001-09-25 23:34:43 +02:00
|
|
|
stay = 1
|
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
code = 301
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
body = 1,
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing host not found: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
|
|
|
url = "http://wronghost/does/not/exist"
|
|
|
|
}
|
2002-12-03 08:20:34 +01:00
|
|
|
local c, e = socket.connect("wronghost", 80)
|
2001-09-25 23:34:43 +02:00
|
|
|
expect = {
|
|
|
|
error = e
|
|
|
|
}
|
|
|
|
ignore = {}
|
|
|
|
check_request(request, expect, ignore)
|
2001-01-25 22:59:39 +01:00
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing invalid url: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = host .. prefix
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
2002-12-03 08:20:34 +01:00
|
|
|
local c, e = socket.connect("", 80)
|
2001-09-25 23:34:43 +02:00
|
|
|
expect = {
|
|
|
|
error = e
|
|
|
|
}
|
|
|
|
ignore = {}
|
|
|
|
check_request(request, expect, ignore)
|
2001-01-25 22:59:39 +01:00
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing document not found: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://" .. host .. "/wrongdocument.html"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
code = 404
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
body = 1,
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing auth failure: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://" .. host .. prefix .. "/auth/index.html"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
code = 401
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
body = 1,
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing manual basic auth: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://" .. host .. prefix .. "/auth/index.html",
|
2001-09-25 23:34:43 +02:00
|
|
|
headers = {
|
2004-01-16 08:06:31 +01:00
|
|
|
authorization = "Basic " ..
|
|
|
|
socket.code.base64.encode("luasocket:password")
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
code = 200,
|
|
|
|
body = index
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing automatic basic auth: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
code = 200,
|
|
|
|
body = index
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing auth info overriding: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
|
2001-09-25 23:34:43 +02:00
|
|
|
user = "luasocket",
|
|
|
|
password = "password"
|
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
code = 200,
|
|
|
|
body = index
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing cgi output retrieval (probably chunked...): ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://" .. host .. cgiprefix .. "/cat-index-html"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
body = index,
|
|
|
|
code = 200
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing redirect loop: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://" .. host .. cgiprefix .. "/redirect-loop"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
code = 302
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
status = 1,
|
|
|
|
headers = 1,
|
|
|
|
body = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing wrong scheme: ")
|
2001-09-26 22:29:18 +02:00
|
|
|
request = {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "wrong://" .. host .. cgiprefix .. "/cat",
|
2001-09-26 22:29:18 +02:00
|
|
|
method = "GET"
|
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
error = "unknown scheme 'wrong'"
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2001-09-25 23:34:43 +02:00
|
|
|
local body
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing simple get function: ")
|
2003-03-28 23:41:26 +01:00
|
|
|
body = socket.http.get("http://" .. host .. prefix .. "/index.html")
|
2001-09-25 23:34:43 +02:00
|
|
|
check(body == index)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing simple get function with table args: ")
|
2003-03-20 01:24:44 +01:00
|
|
|
body = socket.http.get {
|
2003-03-28 23:41:26 +01:00
|
|
|
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
|
2001-09-25 23:34:43 +02:00
|
|
|
user = "luasocket",
|
|
|
|
password = "password"
|
|
|
|
}
|
|
|
|
check(body == index)
|
|
|
|
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing HEAD method: ")
|
2004-01-18 01:04:20 +01:00
|
|
|
socket.http.TIMEOUT = 1
|
2003-03-20 01:24:44 +01:00
|
|
|
response = socket.http.request {
|
2001-09-25 23:34:43 +02:00
|
|
|
method = "HEAD",
|
2004-01-18 01:04:20 +01:00
|
|
|
url = "http://www.cs.princeton.edu/~diego/"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
check(response and response.headers)
|
2001-01-25 22:59:39 +01:00
|
|
|
|
|
|
|
print("passed all tests")
|
2001-06-09 00:40:09 +02:00
|
|
|
|
2003-05-25 03:54:13 +02:00
|
|
|
print(string.format("done in %.2fs", socket.time() - t))
|