updated for luasocket 1.4

This commit is contained in:
Diego Nehab 2001-09-25 21:34:43 +00:00
parent b319e6a1e8
commit d36460a249
2 changed files with 402 additions and 91 deletions

View File

@ -1,34 +1,121 @@
function mysetglobal (varname, oldvalue, newvalue)
print("changing " .. varname)
%rawset(%globals(), varname, newvalue)
end
function mygetglobal (varname, newvalue)
print("checking " .. varname)
return %rawget(%globals(), varname)
end
settagmethod(tag(nil), "setglobal", mysetglobal)
settagmethod(tag(nil), "getglobal", mygetglobal)
assert(dofile("../lua/ftp.lua")) assert(dofile("../lua/ftp.lua"))
assert(dofile("../lua/buffer.lua")) assert(dofile("../lua/url.lua"))
assert(dofile("auxiliar.lua")) assert(dofile("../lua/concat.lua"))
assert(dofile("../lua/code.lua"))
pdir = "/home/i/diego/public/html/luasocket/test/" local similar = function(s1, s2)
ldir = "/home/luasocket/" return strlower(gsub(s1, "%s", "")) == strlower(gsub(s2, "%s", ""))
adir = "/home/ftp/test/" end
-- needs an accound luasocket:password local capture = function(cmd)
-- and a copy of /home/i/diego/public/html/luasocket/test in ~ftp/test readfrom("| " .. cmd)
local s = read("*a")
readfrom()
return s
end
print("testing authenticated upload") local readfile = function(name)
bf = readfile(pdir .. "index.html") local f = readfrom(name)
e = ftp_put("ftp://luasocket:password@localhost/index.html", bf, "b") if not f then return nil end
assert(not e, e) local s = read("*a")
assert(compare(ldir .. "index.html", bf), "files differ") readfrom()
remove(ldir .. "index.html") return s
end
print("testing authenticated download") local check = function(v, e, o)
f, e = ftp_get("ftp://luasocket:password@localhost/test/index.html", "b") e = e or "failed!"
assert(f, e) o = o or "ok"
assert(compare(pdir .. "index.html", f), "files differ") if v then print(o)
else print(e) exit() end
end
print("testing anonymous download") -- needs an account luasocket:password
f, e = ftp_get("ftp://localhost/test/index.html", "b") -- and some directories and files in ~ftp
assert(f, e)
assert(compare(adir .. "index.html", f), "files differ")
print("testing directory listing") local index, err, saved, back, expected
f, e = ftp_get("ftp://localhost/test/")
assert(f, e) local t = _time()
assert(f == "index.html\r\n", "files differ")
index = readfile("index.html")
write("testing file upload: ")
remove("/home/ftp/dir1/index.up.html")
err = FTP.put("ftp://localhost/dir1/index.up.html;type=i", index)
saved = readfile("/home/ftp/dir1/index.up.html")
check(not err and saved == index, err)
write("testing file download: ")
back, err = FTP.get("ftp://localhost/dir1/index.up.html;type=i")
check(not err and back == index, err)
write("testing no directory changes: ")
back, err = FTP.get("ftp://localhost/index.html;type=i")
check(not err and back == index, err)
write("testing multiple directory changes: ")
back, err = FTP.get("ftp://localhost/dir1/dir2/dir3/dir4/dir5/dir6/index.html;type=i")
check(not err and back == index, err)
write("testing authenticated upload: ")
remove("/home/luasocket/index.up.html")
err = FTP.put("ftp://luasocket:password@localhost/index.up.html;type=i", index)
saved = readfile("/home/luasocket/index.up.html")
check(not err and saved == index, err)
write("testing authenticated download: ")
back, err = FTP.get("ftp://luasocket:password@localhost/index.up.html;type=i")
check(not err and back == index, err)
write("testing weird-character translation: ")
back, err = FTP.get("ftp://luasocket:password@localhost/%2fhome/ftp/dir1/index.html;type=i")
check(not err and back == index, err)
write("testing parameter overriding: ")
back, err = FTP.get {
url = "//stupid:mistake@localhost/dir1/index.html",
user = "luasocket",
password = "password",
type = "i"
}
check(not err and back == index, err)
write("testing invalid url: ")
back, err = FTP.get("localhost/dir1/index.html;type=i")
local c, e = connect("", 21)
check(not back and err == e, err)
write("testing directory listing: ")
expected = capture("ls -F /home/ftp/dir1 | grep -v /")
back, err = FTP.get("ftp://localhost/dir1;type=d")
check(similar(back, expected))
write("testing home directory listing: ")
expected = capture("ls -F /home/ftp | grep -v /")
back, err = FTP.get("ftp://localhost/")
check(back and similar(back, expected), nil, err)
write("testing upload denial: ")
err = FTP.put("ftp://localhost/index.up.html;type=a", index)
check(err, err)
write("testing authentication failure: ")
err = FTP.put("ftp://luasocket:wrong@localhost/index.html;type=a", index)
check(err, err)
write("testing wrong file: ")
back, err = FTP.get("ftp://localhost/index.wrong.html;type=a")
check(err, err)
print("passed all tests") print("passed all tests")
print(format("done in %.2fs", _time() - t))

View File

@ -1,89 +1,313 @@
-- load http
assert(dofile("../lua/http.lua"))
assert(dofile("../lua/base64.lua"))
assert(dofile("../lua/buffer.lua"))
assert(dofile("auxiliar.lua"))
t = _time()
-- needs Alias from /home/i/diego/public/html/luasocket/test to -- needs Alias from /home/i/diego/public/html/luasocket/test to
-- /luasocket-test -- /luasocket-test
-- needs ScriptAlias from /home/i/diego/public/html/luasocket/test/cgi-bin -- needs ScriptAlias from /home/i/diego/public/html/luasocket/test/cgi
-- to /luasocket-cgi-bin/ -- to /luasocket-test/cgi
function join(s, e) function mysetglobal (varname, oldvalue, newvalue)
return tostring(s) .. ":" .. tostring(e) print("changing " .. varname)
%rawset(%globals(), varname, newvalue)
end
function mygetglobal (varname, newvalue)
print("checking " .. varname)
return %rawget(%globals(), varname)
end
settagmethod(tag(nil), "setglobal", mysetglobal)
settagmethod(tag(nil), "getglobal", mygetglobal)
local similar = function(s1, s2)
return strlower(gsub(s1, "%s", "")) == strlower(gsub(s2, "%s", ""))
end end
function status(s) local fail = function(s)
local code s = s or "failed!"
_,_, code = strfind(s, "(%d%d%d)") print(s)
return tonumber(code) exit()
end end
pdir = pdir or "/home/i/diego/public/html/luasocket/test/" local readfile = function(name)
local f = readfrom(name)
if not f then return nil end
local s = read("*a")
readfrom()
return s
end
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
local host, request, response, ignore, expect, index, prefix, cgiprefix
-- load http
assert(dofile("../lua/http.lua"))
assert(dofile("../lua/code.lua"))
assert(dofile("../lua/concat.lua"))
assert(dofile("../lua/url.lua"))
local t = _time()
host = host or "localhost" host = host or "localhost"
prefix = prefix or "/luasocket-test"
cgiprefix = cgiprefix or "/luasocket-test-cgi"
index = readfile("index.html")
print("testing document retrieval") write("testing request uri correctness: ")
url = "http://" .. host .. "/luasocket-test/index.html" local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
f, m, s, e = http_get(url) local back = HTTP.get("http://" .. host .. forth)
assert(f and m and s and not e, join(s, e)) if similar(back, forth) then print("ok")
assert(compare(pdir .. "index.html", f), "documents differ") else fail("failed!") end
print("testing HTTP redirection") write("testing query string correctness: ")
url = "http://" .. host .. "/luasocket-test" forth = "this+is+the+query+string"
f, m, s, e = http_get(url) back = HTTP.get("http://" .. host .. cgiprefix .. "/query-string?" .. forth)
assert(f and m and s and not e, join(s, e)) if similar(back, forth) then print("ok")
assert(compare(pdir .. "index.html", f), "documents differ") else fail("failed!") end
print("testing cgi output retrieval (probably chunked...)") write("testing document retrieval: ")
url = "http://" .. host .. "/luasocket-cgi-bin/cat-index-html" request = {
f, m, s, e = http_get(url) url = "http://" .. host .. prefix .. "/index.html"
assert(f and m and s and not e, join(s, e)) }
assert(compare(pdir .. "index.html", f), "documents differ") expect = {
body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
}
check_request(request, expect, ignore)
print("testing post method") write("testing HTTP redirection: ")
url = "http://" .. host .. "/luasocket-cgi-bin/cat" request = {
rf = strrep("!@#$!@#%", 80000) url = "http://" .. host .. prefix
f, m, s, e = http_post(url, rf) }
assert(f and m and s and not e) expect = {
assert(rf == f, "files differ") body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
}
check_request(request, expect, ignore)
print("testing automatic auth failure")
url = "http://really:wrong@" .. host .. "/luasocket-test/auth/index.html"
f, m, s, e = http_get(url)
assert(f and m and s and not e and status(s) == 401)
write("testing automatic auth failure: ")
request = {
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html"
}
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,
stay = 1
}
expect = {
code = 301
}
ignore = {
body = 1,
status = 1,
headers = 1
}
check_request(request, expect, ignore)
write("testing host not found: ") write("testing host not found: ")
url = "http://wronghost/luasocket-test/index.html" request = {
f, m, s, e = http_get(url) url = "http://wronghost/does/not/exist"
assert(not f and not m and not s and e) }
print(e) local c, e = connect("wronghost", 80)
expect = {
error = e
}
ignore = {}
check_request(request, expect, ignore)
write("testing auth failure: ") write("testing invalid url: ")
url = "http://" .. host .. "/luasocket-test/auth/index.html" request = {
f, m, s, e = http_get(url) url = host .. prefix
assert(f and m and s and not e and status(s) == 401) }
print(s) local c, e = connect("", 80)
expect = {
error = e
}
ignore = {}
check_request(request, expect, ignore)
write("testing document not found: ") write("testing document not found: ")
url = "http://" .. host .. "/luasocket-test/wrongdocument.html" request = {
f, m, s, e = http_get(url) url = "http://" .. host .. "/wrongdocument.html"
assert(f and m and s and not e and status(s) == 404) }
print(s) expect = {
code = 404
}
ignore = {
body = 1,
status = 1,
headers = 1
}
check_request(request, expect, ignore)
print("testing manual auth") write("testing auth failure: ")
url = "http://" .. host .. "/luasocket-test/auth/index.html" request = {
h = {authorization = "Basic " .. base64("luasocket:password")} url = "http://" .. host .. prefix .. "/auth/index.html"
f, m, s, e = http_get(url, h) }
assert(f and m and s and not e, join(s, e)) expect = {
assert(compare(pdir .. "auth/index.html", f), "documents differ") code = 401
}
ignore = {
body = 1,
status = 1,
headers = 1
}
check_request(request, expect, ignore)
print("testing automatic auth") write("testing manual basic auth: ")
url = "http://luasocket:password@" .. host .. "/luasocket-test/auth/index.html" request = {
f, m, s, e = http_get(url) url = "http://" .. host .. prefix .. "/auth/index.html",
assert(f and m and s and not e, join(s, e)) headers = {
assert(compare(pdir .. "auth/index.html", f), "documents differ") 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"
}
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",
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"
}
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"
}
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",
method = "POST",
body = index
}
expect = {
body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
}
check_request(request, expect, ignore)
local body
write("testing simple get function: ")
body = HTTP.get("http://" .. host .. prefix .. "/index.html")
check(body == index)
write("testing simple get function with table args: ")
body = HTTP.get {
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
user = "luasocket",
password = "password"
}
check(body == index)
write("testing simple post function: ")
body = HTTP.post("http://" .. host .. cgiprefix .. "/cat", index)
check(body == index)
write("testing simple post function with table args: ")
body = HTTP.post {
url = "http://" .. host .. cgiprefix .. "/cat",
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)
print("passed all tests") print("passed all tests")