luasocket/test/httptest.lua

316 lines
6.0 KiB
Lua
Raw Normal View History

-- needs Alias from /home/c/diego/tec/luasocket/test to
2001-01-25 22:59:39 +01:00
-- /luasocket-test
-- needs ScriptAlias from /home/c/diego/tec/luasocket/test/cgi
-- to /luasocket-test-cgi
-- needs AllowOverride AuthConfig on /home/c/diego/tec/luasocket/test/auth
2001-09-25 23:34:43 +02:00
dofile("noglobals.lua")
2001-09-25 23:34:43 +02:00
local similar = function(s1, s2)
return strlower(gsub(s1 or "", "%s", "")) ==
strlower(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)
exit()
2001-01-25 22:59:39 +01:00
end
2001-09-25 23:34:43 +02:00
local readfile = function(name)
local f = readfrom(name)
if not f then return nil end
local s = read("*a")
readfrom()
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")
else %fail(e) end
end
local check_request = function(request, expect, ignore)
local response = HTTP.request(request)
for i,v in response do
if not ignore[i] then
if v ~= expect[i] then %fail(i .. " differs!") end
end
end
for i,v in expect do
if not ignore[i] then
if v ~= response[i] then %fail(i .. " differs!") end
end
end
print("ok")
end
dofile("../src/modules/http.lua")
2001-09-25 23:34:43 +02:00
local request, response, ignore, expect, index, prefix, cgiprefix
2001-09-25 23:34:43 +02:00
local t = _time()
HOST = HOST or "localhost"
2001-09-25 23:34:43 +02:00
prefix = prefix or "/luasocket-test"
cgiprefix = cgiprefix or "/luasocket-test-cgi"
index = readfile("index.html")
write("testing request uri correctness: ")
local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
local back = HTTP.get("http://" .. HOST .. 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
2001-09-25 23:34:43 +02:00
write("testing query string correctness: ")
forth = "this+is+the+query+string"
back = 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
2001-09-25 23:34:43 +02:00
write("testing document retrieval: ")
request = {
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)
write("testing HTTP redirection: ")
request = {
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)
write("testing automatic auth failure: ")
request = {
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)
write("testing HTTP redirection failure: ")
request = {
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)
2001-01-25 22:59:39 +01:00
write("testing host not found: ")
2001-09-25 23:34:43 +02:00
request = {
url = "http://wronghost/does/not/exist"
}
local c, e = connect("wronghost", 80)
expect = {
error = e
}
ignore = {}
check_request(request, expect, ignore)
2001-01-25 22:59:39 +01:00
2001-09-25 23:34:43 +02:00
write("testing invalid url: ")
request = {
url = HOST .. prefix
2001-09-25 23:34:43 +02:00
}
local c, e = connect("", 80)
expect = {
error = e
}
ignore = {}
check_request(request, expect, ignore)
2001-01-25 22:59:39 +01:00
write("testing document not found: ")
2001-09-25 23:34:43 +02:00
request = {
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)
write("testing auth failure: ")
request = {
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)
write("testing manual basic auth: ")
request = {
url = "http://" .. HOST .. prefix .. "/auth/index.html",
2001-09-25 23:34:43 +02:00
headers = {
authorization = "Basic " .. Code.base64("luasocket:password")
}
}
expect = {
code = 200,
body = index
}
ignore = {
status = 1,
headers = 1
}
check_request(request, expect, ignore)
write("testing automatic basic auth: ")
request = {
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)
write("testing auth info overriding: ")
request = {
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)
write("testing cgi output retrieval (probably chunked...): ")
request = {
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)
write("testing redirect loop: ")
request = {
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)
write("testing post method: ")
request = {
url = "http://" .. HOST .. cgiprefix .. "/cat",
2001-09-25 23:34:43 +02:00
method = "POST",
body = index
}
expect = {
body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
}
check_request(request, expect, ignore)
2001-09-26 22:29:18 +02:00
write("testing wrong scheme: ")
request = {
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
write("testing simple get function: ")
body = HTTP.get("http://" .. HOST .. prefix .. "/index.html")
2001-09-25 23:34:43 +02:00
check(body == index)
write("testing simple get function with table args: ")
body = HTTP.get {
url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html",
2001-09-25 23:34:43 +02:00
user = "luasocket",
password = "password"
}
check(body == index)
write("testing simple post function: ")
body = HTTP.post("http://" .. HOST .. cgiprefix .. "/cat", index)
2001-09-25 23:34:43 +02:00
check(body == index)
write("testing simple post function with table args: ")
body = HTTP.post {
url = "http://" .. HOST .. cgiprefix .. "/cat",
2001-09-25 23:34:43 +02:00
body = index
}
check(body == index)
write("testing HEAD method: ")
response = HTTP.request {
method = "HEAD",
url = "http://www.tecgraf.puc-rio.br/~diego/"
}
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
print(format("done in %.2fs", _time() - t))