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
|
2004-06-21 08:07:58 +02:00
|
|
|
local socket = require("socket")
|
2004-09-27 06:01:18 +02:00
|
|
|
local http = require("socket.http")
|
|
|
|
local url = require("socket.url")
|
2004-07-26 06:03:55 +02:00
|
|
|
|
2004-06-21 08:07:58 +02:00
|
|
|
local mime = require("mime")
|
|
|
|
local ltn12 = require("ltn12")
|
2004-05-28 08:16:43 +02:00
|
|
|
|
2004-09-27 06:01:18 +02:00
|
|
|
-- override protection to make sure we see all errors
|
|
|
|
-- socket.protect = function(s) return s end
|
|
|
|
|
2004-03-22 05:15:03 +01:00
|
|
|
dofile("testsupport.lua")
|
2004-01-19 01:24:41 +01:00
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
local host, proxy, request, response, index_file
|
|
|
|
local ignore, expect, index, prefix, cgiprefix, index_crlf
|
2004-01-19 01:24:41 +01:00
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
http.TIMEOUT = 10
|
2004-01-19 19:22:51 +01:00
|
|
|
|
2004-06-21 00:19:54 +02:00
|
|
|
local t = socket.gettime()
|
2004-01-19 01:24:41 +01:00
|
|
|
|
2006-04-03 05:10:56 +02:00
|
|
|
--host = host or "diego.student.princeton.edu"
|
2006-04-03 06:45:42 +02:00
|
|
|
--host = host or "diego.student.princeton.edu"
|
|
|
|
host = host or "localhost"
|
2005-11-20 09:56:19 +01:00
|
|
|
proxy = proxy or "http://localhost:3128"
|
2004-01-19 01:24:41 +01:00
|
|
|
prefix = prefix or "/luasocket-test"
|
|
|
|
cgiprefix = cgiprefix or "/luasocket-test-cgi"
|
2005-06-20 06:51:55 +02:00
|
|
|
index_file = "index.html"
|
2004-01-19 01:24:41 +01:00
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
-- read index with CRLF convention
|
|
|
|
index = readfile(index_file)
|
2004-01-19 01:24:41 +01:00
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
local check_result = function(response, expect, ignore)
|
2011-05-25 22:57:22 +02:00
|
|
|
for i,v in pairs(response) do
|
|
|
|
if not ignore[i] then
|
|
|
|
if v ~= expect[i] then
|
2004-03-21 08:50:15 +01:00
|
|
|
local f = io.open("err", "w")
|
|
|
|
f:write(tostring(v), "\n\n versus\n\n", tostring(expect[i]))
|
|
|
|
f:close()
|
2004-01-16 08:06:31 +01:00
|
|
|
fail(i .. " differs!")
|
|
|
|
end
|
2011-05-25 22:57:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
for i,v in pairs(expect) do
|
|
|
|
if not ignore[i] then
|
|
|
|
if v ~= response[i] then
|
2004-03-21 08:50:15 +01:00
|
|
|
local f = io.open("err", "w")
|
|
|
|
f:write(tostring(response[i]), "\n\n versus\n\n", tostring(v))
|
2004-01-21 02:09:50 +01:00
|
|
|
v = string.sub(type(v) == "string" and v or "", 1, 70)
|
2004-03-21 08:50:15 +01:00
|
|
|
f:close()
|
2004-01-16 08:06:31 +01:00
|
|
|
fail(i .. " differs!")
|
|
|
|
end
|
2011-05-25 22:57:22 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
print("ok")
|
2001-09-25 23:34:43 +02:00
|
|
|
end
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
local check_request = function(request, expect, ignore)
|
2004-03-21 08:50:15 +01:00
|
|
|
local t
|
2004-06-16 23:56:23 +02:00
|
|
|
if not request.sink then request.sink, t = ltn12.sink.table() end
|
2004-03-21 08:50:15 +01:00
|
|
|
request.source = request.source or
|
|
|
|
(request.body and ltn12.source.string(request.body))
|
2011-05-25 22:57:22 +02:00
|
|
|
local response = {}
|
2004-06-16 23:56:23 +02:00
|
|
|
response.code, response.headers, response.status =
|
|
|
|
socket.skip(1, http.request(request))
|
2004-03-21 08:50:15 +01:00
|
|
|
if t and table.getn(t) > 0 then response.body = table.concat(t) end
|
2004-01-21 02:09:50 +01:00
|
|
|
check_result(response, expect, ignore)
|
|
|
|
end
|
|
|
|
|
|
|
|
------------------------------------------------------------------------
|
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-06-16 23:56:23 +02:00
|
|
|
local back, c, h = http.request("http://" .. host .. forth)
|
|
|
|
if not back then fail(c) end
|
2004-06-15 08:24:00 +02:00
|
|
|
back = url.parse(back)
|
2004-01-19 01:24:41 +01:00
|
|
|
if similar(back.query, "this+is+the+query+string") then print("ok")
|
2004-03-16 07:42:53 +01:00
|
|
|
else fail(back.query) end
|
2001-01-25 22:59:39 +01:00
|
|
|
|
2004-01-21 02:09:50 +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"
|
2004-06-16 23:56:23 +02:00
|
|
|
back = http.request("http://" .. host .. cgiprefix ..
|
2004-01-21 02:09:50 +01:00
|
|
|
"/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
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing document retrieval: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://" .. host .. prefix .. "/index.html"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
body = index,
|
|
|
|
code = 200
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
ignore = {
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2004-01-19 01:24:41 +01:00
|
|
|
io.write("testing redirect loop: ")
|
|
|
|
request = {
|
|
|
|
url = "http://" .. host .. cgiprefix .. "/redirect-loop"
|
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
code = 302
|
|
|
|
}
|
|
|
|
ignore = {
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1,
|
|
|
|
body = 1
|
2004-01-19 01:24:41 +01:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
2004-01-16 08:06:31 +01:00
|
|
|
|
2004-07-26 07:17:37 +02:00
|
|
|
------------------------------------------------------------------------
|
|
|
|
io.write("testing invalid url: ")
|
2007-03-12 05:08:40 +01:00
|
|
|
local r, e = http.request{url = host .. prefix}
|
|
|
|
assert(r == nil and e == "invalid host ''")
|
2004-07-26 07:17:37 +02:00
|
|
|
r, re = http.request(host .. prefix)
|
|
|
|
assert(r == nil and e == re, tostring(r) ..", " .. tostring(re) ..
|
|
|
|
" vs " .. tostring(e))
|
|
|
|
print("ok")
|
|
|
|
|
2006-04-03 05:10:56 +02:00
|
|
|
io.write("testing invalid empty port: ")
|
|
|
|
request = {
|
|
|
|
url = "http://" .. host .. ":" .. prefix .. "/index.html"
|
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
body = index,
|
|
|
|
code = 200
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
status = 1,
|
|
|
|
headers = 1
|
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
io.write("testing post method: ")
|
|
|
|
-- wanted to test chunked post, but apache doesn't support it...
|
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://" .. host .. cgiprefix .. "/cat",
|
|
|
|
method = "POST",
|
|
|
|
body = index,
|
2004-01-16 08:06:31 +01:00
|
|
|
-- remove content-length header to send chunked body
|
|
|
|
headers = { ["content-length"] = string.len(index) }
|
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
body = index,
|
|
|
|
code = 200
|
2004-01-16 08:06:31 +01:00
|
|
|
}
|
|
|
|
ignore = {
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2004-01-16 08:06:31 +01:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2005-06-17 06:04:55 +02:00
|
|
|
--[[
|
2004-01-19 01:24:41 +01:00
|
|
|
io.write("testing proxy with post method: ")
|
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://" .. host .. cgiprefix .. "/cat",
|
|
|
|
method = "POST",
|
|
|
|
body = index,
|
2004-01-19 01:24:41 +01:00
|
|
|
headers = { ["content-length"] = string.len(index) },
|
2004-01-19 19:22:51 +01:00
|
|
|
proxy= proxy
|
2004-01-19 01:24:41 +01:00
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
body = index,
|
|
|
|
code = 200
|
2004-01-19 01:24:41 +01:00
|
|
|
}
|
|
|
|
ignore = {
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2004-01-19 01:24:41 +01:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
2005-06-17 06:04:55 +02:00
|
|
|
]]
|
2004-01-19 01:24:41 +01:00
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2004-01-16 08:06:31 +01:00
|
|
|
io.write("testing simple post function: ")
|
2004-06-16 23:56:23 +02:00
|
|
|
back = http.request("http://" .. host .. cgiprefix .. "/cat", index)
|
2004-03-22 05:15:03 +01:00
|
|
|
assert(back == index)
|
2004-06-18 23:41:44 +02:00
|
|
|
print("ok")
|
2004-01-16 08:06:31 +01:00
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
io.write("testing ltn12.(sink|source).file: ")
|
2004-01-21 02:09:50 +01:00
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://" .. host .. cgiprefix .. "/cat",
|
|
|
|
method = "POST",
|
|
|
|
source = ltn12.source.file(io.open(index_file, "rb")),
|
2004-06-18 23:41:44 +02:00
|
|
|
sink = ltn12.sink.file(io.open(index_file .. "-back", "wb")),
|
2004-01-21 02:09:50 +01:00
|
|
|
headers = { ["content-length"] = string.len(index) }
|
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
code = 200
|
2004-01-21 02:09:50 +01:00
|
|
|
}
|
|
|
|
ignore = {
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2004-01-21 02:09:50 +01:00
|
|
|
}
|
2004-03-21 08:50:15 +01:00
|
|
|
check_request(request, expect, ignore)
|
2004-01-21 02:09:50 +01:00
|
|
|
back = readfile(index_file .. "-back")
|
2004-03-22 05:15:03 +01:00
|
|
|
assert(back == index)
|
2004-01-21 02:09:50 +01:00
|
|
|
os.remove(index_file .. "-back")
|
|
|
|
|
|
|
|
------------------------------------------------------------------------
|
2004-03-16 07:42:53 +01:00
|
|
|
io.write("testing ltn12.(sink|source).chain and mime.(encode|decode): ")
|
2004-01-21 02:09:50 +01:00
|
|
|
|
|
|
|
local function b64length(len)
|
|
|
|
local a = math.ceil(len/3)*4
|
|
|
|
local l = math.ceil(a/76)
|
|
|
|
return a + l*2
|
|
|
|
end
|
|
|
|
|
2004-03-16 07:42:53 +01:00
|
|
|
local source = ltn12.source.chain(
|
2004-06-18 23:41:44 +02:00
|
|
|
ltn12.source.file(io.open(index_file, "rb")),
|
2004-03-16 07:42:53 +01:00
|
|
|
ltn12.filter.chain(
|
|
|
|
mime.encode("base64"),
|
|
|
|
mime.wrap("base64")
|
2004-01-21 02:09:50 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2004-03-16 07:42:53 +01:00
|
|
|
local sink = ltn12.sink.chain(
|
|
|
|
mime.decode("base64"),
|
2004-06-18 23:41:44 +02:00
|
|
|
ltn12.sink.file(io.open(index_file .. "-back", "wb"))
|
2004-01-21 02:09:50 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://" .. host .. cgiprefix .. "/cat",
|
|
|
|
method = "POST",
|
|
|
|
source = source,
|
2004-03-16 07:42:53 +01:00
|
|
|
sink = sink,
|
2004-01-21 02:09:50 +01:00
|
|
|
headers = { ["content-length"] = b64length(string.len(index)) }
|
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
code = 200
|
2004-01-21 02:09:50 +01:00
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
body_cb = 1,
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2004-01-21 02:09:50 +01:00
|
|
|
}
|
2004-03-21 08:50:15 +01:00
|
|
|
check_request(request, expect, ignore)
|
2004-01-21 02:09:50 +01:00
|
|
|
back = readfile(index_file .. "-back")
|
2004-03-22 05:15:03 +01:00
|
|
|
assert(back == index)
|
2004-01-21 02:09:50 +01:00
|
|
|
os.remove(index_file .. "-back")
|
|
|
|
|
|
|
|
------------------------------------------------------------------------
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing http redirection: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://" .. host .. prefix
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
body = index,
|
|
|
|
code = 200
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
ignore = {
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2005-06-17 06:04:55 +02:00
|
|
|
--[[
|
2004-01-19 01:24:41 +01:00
|
|
|
io.write("testing proxy with redirection: ")
|
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://" .. host .. prefix,
|
2004-01-19 19:22:51 +01:00
|
|
|
proxy = proxy
|
2004-01-19 01:24:41 +01:00
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
body = index,
|
|
|
|
code = 200
|
2004-01-19 01:24:41 +01:00
|
|
|
}
|
|
|
|
ignore = {
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2004-01-19 01:24:41 +01:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
2005-06-17 06:04:55 +02:00
|
|
|
]]
|
2004-01-19 01:24:41 +01:00
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
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,
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing http redirection failure: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://" .. host .. prefix,
|
|
|
|
redirect = false
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
|
|
|
code = 301
|
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
body = 1,
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +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 = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://" .. host .. "/wrongdocument.html"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
code = 404
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
body = 1,
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing auth failure: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://" .. host .. prefix .. "/auth/index.html"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
code = 401
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
ignore = {
|
|
|
|
body = 1,
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing manual basic auth: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://" .. host .. prefix .. "/auth/index.html",
|
|
|
|
headers = {
|
|
|
|
authorization = "Basic " .. (mime.b64("luasocket:password"))
|
|
|
|
}
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
code = 200,
|
|
|
|
body = index
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
ignore = {
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing automatic basic auth: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
code = 200,
|
|
|
|
body = index
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
ignore = {
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing auth info overriding: ")
|
2001-09-25 23:34:43 +02:00
|
|
|
request = {
|
2011-05-25 22:57:22 +02:00
|
|
|
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
|
|
|
|
user = "luasocket",
|
|
|
|
password = "password"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
expect = {
|
2011-05-25 22:57:22 +02:00
|
|
|
code = 200,
|
|
|
|
body = index
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
ignore = {
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
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 = {
|
2011-05-25 22:57:22 +02:00
|
|
|
body = index,
|
|
|
|
code = 200
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
ignore = {
|
2011-05-25 22:57:22 +02:00
|
|
|
status = 1,
|
|
|
|
headers = 1
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
|
|
|
check_request(request, expect, ignore)
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2001-09-25 23:34:43 +02:00
|
|
|
local body
|
2004-06-16 23:56:23 +02:00
|
|
|
io.write("testing simple request function: ")
|
|
|
|
body = http.request("http://" .. host .. prefix .. "/index.html")
|
2004-03-22 05:15:03 +01:00
|
|
|
assert(body == index)
|
2004-03-26 07:05:20 +01:00
|
|
|
print("ok")
|
2001-09-25 23:34:43 +02:00
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2002-12-03 08:20:34 +01:00
|
|
|
io.write("testing HEAD method: ")
|
2004-06-16 23:56:23 +02:00
|
|
|
local r, c, h = http.request {
|
2001-09-25 23:34:43 +02:00
|
|
|
method = "HEAD",
|
2009-05-27 11:31:38 +02:00
|
|
|
url = "http://www.tecgraf.puc-rio.br/~diego/"
|
2001-09-25 23:34:43 +02:00
|
|
|
}
|
2004-06-17 08:23:13 +02:00
|
|
|
assert(r and h and (c == 200), c)
|
2004-03-26 07:05:20 +01:00
|
|
|
print("ok")
|
2001-01-25 22:59:39 +01:00
|
|
|
|
2004-06-15 08:24:00 +02:00
|
|
|
------------------------------------------------------------------------
|
|
|
|
io.write("testing host not found: ")
|
2009-05-27 11:31:38 +02:00
|
|
|
local c, e = socket.connect("example.invalid", 80)
|
|
|
|
local r, re = http.request{url = "http://example.invalid/does/not/exist"}
|
|
|
|
assert(r == nil and e == re, tostring(r) .. " " .. tostring(re))
|
|
|
|
r, re = http.request("http://example.invalid/does/not/exist")
|
2004-06-15 08:24:00 +02:00
|
|
|
assert(r == nil and e == re)
|
|
|
|
print("ok")
|
|
|
|
|
2004-01-21 02:09:50 +01:00
|
|
|
------------------------------------------------------------------------
|
2001-01-25 22:59:39 +01:00
|
|
|
print("passed all tests")
|
2004-06-15 08:24:00 +02:00
|
|
|
os.remove("err")
|
2001-06-09 00:40:09 +02:00
|
|
|
|
2004-06-21 00:19:54 +02:00
|
|
|
print(string.format("done in %.2fs", socket.gettime() - t))
|