Saving before big changes to support IPv6.

This commit is contained in:
Diego Nehab 2011-05-25 20:57:22 +00:00
parent bce60be30f
commit 3a8ba90dfb
30 changed files with 948 additions and 850 deletions

View file

@ -82,10 +82,10 @@ print("ok")
io.write("testing parameter overriding: ")
local back = {}
ret, err = ftp.get{
url = "//stupid:mistake@" .. host .. "/index.html",
user = "luasocket",
password = "pedrovian",
type = "i",
url = "//stupid:mistake@" .. host .. "/index.html",
user = "luasocket",
password = "pedrovian",
type = "i",
sink = ltn12.sink.table(back)
}
assert(ret and not err and table.concat(back) == index, err)

View file

@ -34,28 +34,28 @@ index_file = "index.html"
index = readfile(index_file)
local check_result = function(response, expect, ignore)
for i,v in pairs(response) do
if not ignore[i] then
if v ~= expect[i] then
for i,v in pairs(response) do
if not ignore[i] then
if v ~= expect[i] then
local f = io.open("err", "w")
f:write(tostring(v), "\n\n versus\n\n", tostring(expect[i]))
f:close()
fail(i .. " differs!")
end
end
end
for i,v in pairs(expect) do
if not ignore[i] then
if v ~= response[i] then
end
end
for i,v in pairs(expect) do
if not ignore[i] then
if v ~= response[i] then
local f = io.open("err", "w")
f:write(tostring(response[i]), "\n\n versus\n\n", tostring(v))
v = string.sub(type(v) == "string" and v or "", 1, 70)
f:close()
fail(i .. " differs!")
end
end
end
print("ok")
end
end
print("ok")
end
local check_request = function(request, expect, ignore)
@ -63,7 +63,7 @@ local check_request = function(request, expect, ignore)
if not request.sink then request.sink, t = ltn12.sink.table() end
request.source = request.source or
(request.body and ltn12.source.string(request.body))
local response = {}
local response = {}
response.code, response.headers, response.status =
socket.skip(1, http.request(request))
if t and table.getn(t) > 0 then response.body = table.concat(t) end
@ -90,15 +90,15 @@ else fail("failed!") end
------------------------------------------------------------------------
io.write("testing document retrieval: ")
request = {
url = "http://" .. host .. prefix .. "/index.html"
url = "http://" .. host .. prefix .. "/index.html"
}
expect = {
body = index,
code = 200
body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
@ -111,9 +111,9 @@ expect = {
code = 302
}
ignore = {
status = 1,
headers = 1,
body = 1
status = 1,
headers = 1,
body = 1
}
check_request(request, expect, ignore)
@ -144,19 +144,19 @@ check_request(request, expect, ignore)
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,
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
body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
@ -164,19 +164,19 @@ check_request(request, expect, ignore)
--[[
io.write("testing proxy with post method: ")
request = {
url = "http://" .. host .. cgiprefix .. "/cat",
method = "POST",
body = index,
url = "http://" .. host .. cgiprefix .. "/cat",
method = "POST",
body = index,
headers = { ["content-length"] = string.len(index) },
proxy= proxy
}
expect = {
body = index,
code = 200
body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
]]
@ -190,18 +190,18 @@ print("ok")
------------------------------------------------------------------------
io.write("testing ltn12.(sink|source).file: ")
request = {
url = "http://" .. host .. cgiprefix .. "/cat",
method = "POST",
source = ltn12.source.file(io.open(index_file, "rb")),
url = "http://" .. host .. cgiprefix .. "/cat",
method = "POST",
source = ltn12.source.file(io.open(index_file, "rb")),
sink = ltn12.sink.file(io.open(index_file .. "-back", "wb")),
headers = { ["content-length"] = string.len(index) }
}
expect = {
code = 200
code = 200
}
ignore = {
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
back = readfile(index_file .. "-back")
@ -231,19 +231,19 @@ local sink = ltn12.sink.chain(
)
request = {
url = "http://" .. host .. cgiprefix .. "/cat",
method = "POST",
source = source,
url = "http://" .. host .. cgiprefix .. "/cat",
method = "POST",
source = source,
sink = sink,
headers = { ["content-length"] = b64length(string.len(index)) }
}
expect = {
code = 200
code = 200
}
ignore = {
body_cb = 1,
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
back = readfile(index_file .. "-back")
@ -253,15 +253,15 @@ os.remove(index_file .. "-back")
------------------------------------------------------------------------
io.write("testing http redirection: ")
request = {
url = "http://" .. host .. prefix
url = "http://" .. host .. prefix
}
expect = {
body = index,
code = 200
body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
@ -269,16 +269,16 @@ check_request(request, expect, ignore)
--[[
io.write("testing proxy with redirection: ")
request = {
url = "http://" .. host .. prefix,
url = "http://" .. host .. prefix,
proxy = proxy
}
expect = {
body = index,
code = 200
body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
]]
@ -293,104 +293,104 @@ expect = {
}
ignore = {
body = 1,
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing http redirection failure: ")
request = {
url = "http://" .. host .. prefix,
redirect = false
url = "http://" .. host .. prefix,
redirect = false
}
expect = {
code = 301
}
ignore = {
body = 1,
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing document not found: ")
request = {
url = "http://" .. host .. "/wrongdocument.html"
url = "http://" .. host .. "/wrongdocument.html"
}
expect = {
code = 404
code = 404
}
ignore = {
body = 1,
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing auth failure: ")
request = {
url = "http://" .. host .. prefix .. "/auth/index.html"
url = "http://" .. host .. prefix .. "/auth/index.html"
}
expect = {
code = 401
code = 401
}
ignore = {
body = 1,
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing manual basic auth: ")
request = {
url = "http://" .. host .. prefix .. "/auth/index.html",
headers = {
authorization = "Basic " .. (mime.b64("luasocket:password"))
}
url = "http://" .. host .. prefix .. "/auth/index.html",
headers = {
authorization = "Basic " .. (mime.b64("luasocket:password"))
}
}
expect = {
code = 200,
body = index
code = 200,
body = index
}
ignore = {
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing automatic basic auth: ")
request = {
url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html"
url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html"
}
expect = {
code = 200,
body = index
code = 200,
body = index
}
ignore = {
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
------------------------------------------------------------------------
io.write("testing auth info overriding: ")
request = {
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
user = "luasocket",
password = "password"
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
user = "luasocket",
password = "password"
}
expect = {
code = 200,
body = index
code = 200,
body = index
}
ignore = {
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)
@ -400,12 +400,12 @@ request = {
url = "http://" .. host .. cgiprefix .. "/cat-index-html"
}
expect = {
body = index,
code = 200
body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
status = 1,
headers = 1
}
check_request(request, expect, ignore)

View file

@ -19,11 +19,11 @@ local parse = mbox.parse
dofile("testsupport.lua")
local total = function()
local t = 0
for i = 1, table.getn(sent) do
t = t + sent[i].count
end
return t
local t = 0
for i = 1, table.getn(sent) do
t = t + sent[i].count
end
return t
end
local similar = function(s1, s2)
@ -39,14 +39,14 @@ local fail = function(s)
end
local readfile = function(name)
local f = io.open(name, "r")
if not f then
local f = io.open(name, "r")
if not f then
fail("unable to open file!")
return nil
end
local s = f:read("*a")
f:close()
return s
local s = f:read("*a")
f:close()
return s
end
local empty = function()
@ -62,7 +62,7 @@ end
local get = function()
local s = ""
for i,v in ipairs(files) do
s = s .. "\n" .. readfile(v)
s = s .. "\n" .. readfile(v)
end
return s
end
@ -82,40 +82,40 @@ local check_body = function(sent, got)
end
local check = function(sent, m)
io.write("checking ", m.headers.title, ": ")
for i = 1, table.getn(sent) do
local s = sent[i]
if s.title == m.headers.title and s.count > 0 then
check_headers(s.headers, m.headers)
check_body(s.body, m.body)
s.count = s.count - 1
print("ok")
return
end
end
fail("not found")
io.write("checking ", m.headers.title, ": ")
for i = 1, table.getn(sent) do
local s = sent[i]
if s.title == m.headers.title and s.count > 0 then
check_headers(s.headers, m.headers)
check_body(s.body, m.body)
s.count = s.count - 1
print("ok")
return
end
end
fail("not found")
end
local insert = function(sent, message)
if type(message.rcpt) == "table" then
message.count = table.getn(message.rcpt)
else message.count = 1 end
message.headers = message.headers or {}
message.headers.title = message.title
table.insert(sent, message)
if type(message.rcpt) == "table" then
message.count = table.getn(message.rcpt)
else message.count = 1 end
message.headers = message.headers or {}
message.headers.title = message.title
table.insert(sent, message)
end
local mark = function()
local time = socket.time()
local time = socket.time()
return { time = time }
end
local wait = function(sentinel, n)
local to
io.write("waiting for ", n, " messages: ")
io.write("waiting for ", n, " messages: ")
while 1 do
local mbox = parse(get())
if n == table.getn(mbox) then break end
local mbox = parse(get())
if n == table.getn(mbox) then break end
if socket.time() - sentinel.time > 50 then
to = 1
break
@ -124,8 +124,8 @@ local wait = function(sentinel, n)
io.write(".")
io.stdout:flush()
end
if to then fail("timeout")
else print("ok") end
if to then fail("timeout")
else print("ok") end
end
local stuffed_body = [[
@ -144,21 +144,21 @@ a lot of trouble.
insert(sent, {
from = from,
rcpt = {
"luasocket@localhost",
"luasock3@dell-diego.cs.princeton.edu",
"luasock1@dell-diego.cs.princeton.edu"
},
body = "multiple rcpt body",
title = "multiple rcpt",
"luasocket@localhost",
"luasock3@dell-diego.cs.princeton.edu",
"luasock1@dell-diego.cs.princeton.edu"
},
body = "multiple rcpt body",
title = "multiple rcpt",
})
insert(sent, {
from = from,
rcpt = {
"luasock2@localhost",
"luasock3",
"luasock1"
},
"luasock2@localhost",
"luasock3",
"luasock1"
},
headers = {
header1 = "header 1",
header2 = "header 2",
@ -210,24 +210,24 @@ insert(sent, {
io.write("testing host not found: ")
local c, e = socket.connect("wrong.host", 25)
local ret, err = socket.smtp.mail{
from = from,
rcpt = rcpt,
server = "wrong.host"
from = from,
rcpt = rcpt,
server = "wrong.host"
}
if ret or e ~= err then fail("wrong error message")
else print("ok") end
io.write("testing invalid from: ")
local ret, err = socket.smtp.mail{
from = ' " " (( _ * ',
rcpt = rcpt,
from = ' " " (( _ * ',
rcpt = rcpt,
}
if ret or not err then fail("wrong error message")
else print(err) end
io.write("testing no rcpt: ")
local ret, err = socket.smtp.mail{
from = from,
from = from,
}
if ret or not err then fail("wrong error message")
else print(err) end
@ -252,7 +252,7 @@ local mbox = parse(get())
print(table.getn(mbox) .. " messages found!")
for i = 1, table.getn(mbox) do
check(sent, mbox[i])
check(sent, mbox[i])
end
print("passed all tests")

View file

@ -3,15 +3,20 @@ local socket = require"socket"
host = host or "localhost"
port = port or "8383"
function pass(...)
function printf(...)
local s = string.format(unpack(arg))
io.stderr:write(s, "\n")
io.stderr:write(s)
end
function pass(...)
printf(...)
io.stderr:write("\n")
end
function fail(...)
local s = string.format(unpack(arg))
io.stderr:write("ERROR: ", s, "!\n")
socket.sleep(3)
io.stderr:write("ERROR: ")
printf(...)
io.stderr:write("!\n")
os.exit()
end
@ -80,7 +85,6 @@ io.stderr:write("----------------------------------------------\n",
start = socket.gettime()
function reconnect()
io.stderr:write("attempting data connection... ")
if data then data:close() end
remote [[
if data then data:close() data = nil end
@ -88,12 +92,11 @@ function reconnect()
data:setoption("tcp-nodelay", true)
]]
data, err = socket.connect(host, port)
if not data then fail(err)
else pass("connected!") end
if not data then fail(err) end
data:setoption("tcp-nodelay", true)
end
pass("attempting control connection...")
printf("attempting control connection...")
control, err = socket.connect(host, port)
if err then fail(err)
else pass("connected!") end
@ -112,6 +115,7 @@ end
------------------------------------------------------------------------
function test_mixed(len)
reconnect()
io.stderr:write("length " .. len .. ": ")
local inter = math.ceil(len/4)
local p1 = "unix " .. string.rep("x", inter) .. "line\n"
local p2 = "dos " .. string.rep("y", inter) .. "line\r\n"
@ -139,6 +143,7 @@ end
------------------------------------------------------------------------
function test_asciiline(len)
reconnect()
io.stderr:write("length " .. len .. ": ")
local str, str10, back, err
str = string.rep("x", math.mod(len, 10))
str10 = string.rep("aZb.c#dAe?", math.floor(len/10))
@ -156,6 +161,7 @@ end
------------------------------------------------------------------------
function test_rawline(len)
reconnect()
io.stderr:write("length " .. len .. ": ")
local str, str10, back, err
str = string.rep(string.char(47), math.mod(len, 10))
str10 = string.rep(string.char(120,21,77,4,5,0,7,36,44,100),
@ -174,6 +180,7 @@ end
------------------------------------------------------------------------
function test_raw(len)
reconnect()
io.stderr:write("length " .. len .. ": ")
local half = math.floor(len/2)
local s1, s2, back, err
s1 = string.rep("x", half)
@ -194,7 +201,7 @@ end
function test_totaltimeoutreceive(len, tm, sl)
reconnect()
local str, err, partial
pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl)
printf("%d bytes, %ds total timeout, %ds pause: ", len, tm, sl)
remote (string.format ([[
data:settimeout(%d)
str = string.rep('a', %d)
@ -215,7 +222,7 @@ end
function test_totaltimeoutsend(len, tm, sl)
reconnect()
local str, err, total
pass("%d bytes, %ds total timeout, %ds pause", len, tm, sl)
printf("%d bytes, %ds total timeout, %ds pause: ", len, tm, sl)
remote (string.format ([[
data:settimeout(%d)
str = data:receive(%d)
@ -235,7 +242,7 @@ end
function test_blockingtimeoutreceive(len, tm, sl)
reconnect()
local str, err, partial
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
printf("%d bytes, %ds blocking timeout, %ds pause: ", len, tm, sl)
remote (string.format ([[
data:settimeout(%d)
str = string.rep('a', %d)
@ -255,7 +262,7 @@ end
function test_blockingtimeoutsend(len, tm, sl)
reconnect()
local str, err, total
pass("%d bytes, %ds blocking timeout, %ds pause", len, tm, sl)
printf("%d bytes, %ds blocking timeout, %ds pause: ", len, tm, sl)
remote (string.format ([[
data:settimeout(%d)
str = data:receive(%d)
@ -273,6 +280,7 @@ end
------------------------------------------------------------------------
function empty_connect()
printf("empty connect: ")
reconnect()
if data then data:close() data = nil end
remote [[
@ -284,7 +292,7 @@ function empty_connect()
pass("ok")
data = socket.connect(host, port)
else
pass("gethostbyname returns localhost on empty string...")
pass("gethostbyname returns localhost on empty string...")
end
end
@ -311,7 +319,7 @@ function test_closed()
local back, partial, err
local str = 'little string'
reconnect()
pass("trying read detection")
printf("trying read detection: ")
remote (string.format ([[
data:send('%s')
data:close()
@ -324,7 +332,7 @@ function test_closed()
elseif str ~= partial then fail("didn't receive partial result.")
else pass("graceful 'closed' received") end
reconnect()
pass("trying write detection")
printf("trying write detection: ")
remote [[
data:close()
data = nil
@ -342,7 +350,6 @@ end
------------------------------------------------------------------------
function test_selectbugs()
local r, s, e = socket.select(nil, nil, 0.1)
print(r, s, e)
assert(type(r) == "table" and type(s) == "table" and
(e == "timeout" or e == "error"))
pass("both nil: ok")
@ -374,7 +381,7 @@ end
------------------------------------------------------------------------
function accept_timeout()
io.stderr:write("accept with timeout (if it hangs, it failed): ")
printf("accept with timeout (if it hangs, it failed): ")
local s, e = socket.bind("*", 0, 0)
assert(s, e)
local t = socket.gettime()
@ -390,23 +397,22 @@ end
------------------------------------------------------------------------
function connect_timeout()
io.stderr:write("connect with timeout (if it hangs, it failed!): ")
printf("connect with timeout (if it hangs, it failed!): ")
local t = socket.gettime()
local c, e = socket.tcp()
assert(c, e)
c:settimeout(0.1)
local t = socket.gettime()
local r, e = c:connect("10.0.0.1", 81)
print(r, e)
assert(not r, "should not connect")
assert(socket.gettime() - t < 2, "took too long to give up.")
c:close()
print("ok")
pass("ok")
end
------------------------------------------------------------------------
function accept_errors()
io.stderr:write("not listening: ")
printf("not listening: ")
local d, e = socket.bind("*", 0)
assert(d, e);
local c, e = socket.tcp();
@ -415,26 +421,26 @@ function accept_errors()
d:settimeout(2)
local r, e = d:accept()
assert(not r and e)
print("ok: ", e)
io.stderr:write("not supported: ")
pass("ok")
printf("not supported: ")
local c, e = socket.udp()
assert(c, e);
d:setfd(c:getfd())
local r, e = d:accept()
assert(not r and e)
print("ok: ", e)
pass("ok")
end
------------------------------------------------------------------------
function connect_errors()
io.stderr:write("connection refused: ")
printf("connection refused: ")
local c, e = socket.connect("localhost", 1);
assert(not c and e)
print("ok: ", e)
io.stderr:write("host not found: ")
pass("ok")
printf("host not found: ")
local c, e = socket.connect("host.is.invalid", 1);
assert(not c and e, e)
print("ok: ", e)
pass("ok")
end
------------------------------------------------------------------------
@ -447,7 +453,7 @@ function rebind_test()
r, e = s:bind("localhost", p)
assert(not r, "managed to rebind!")
assert(e)
print("ok: ", e)
pass("ok")
end
------------------------------------------------------------------------
@ -469,14 +475,14 @@ function getstats_test()
assert(s == t, "sent count failed" .. tostring(s)
.. "/" .. tostring(t))
end
print("ok")
pass("ok")
end
------------------------------------------------------------------------
function test_nonblocking(size)
reconnect()
print("Testing " .. 2*size .. " bytes")
printf("testing " .. 2*size .. " bytes: ")
remote(string.format([[
data:send(string.rep("a", %d))
socket.sleep(0.5)
@ -508,7 +514,7 @@ remote(string.format([[
data:settimeout(-1)
local back = data:receive(2*size)
assert(back == str, "'" .. back .. "' vs '" .. str .. "'")
print("ok")
pass("ok")
end
------------------------------------------------------------------------
@ -516,7 +522,7 @@ function test_readafterclose()
local back, partial, err
local str = 'little string'
reconnect()
pass("trying repeated '*a' pattern")
printf("trying repeated '*a' pattern")
remote (string.format ([[
data:send('%s')
data:close()
@ -526,9 +532,9 @@ function test_readafterclose()
assert(back == str, "unexpected data read")
back, err, partial = data:receive("*a")
assert(back == nil and err == "closed", "should have returned 'closed'")
print("ok")
pass("ok")
reconnect()
pass("trying active close before '*a'")
printf("trying active close before '*a'")
remote (string.format ([[
data:close()
data = nil
@ -536,9 +542,9 @@ function test_readafterclose()
data:close()
back, err, partial = data:receive("*a")
assert(back == nil and err == "closed", "should have returned 'closed'")
print("ok")
pass("ok")
reconnect()
pass("trying active close before '*l'")
printf("trying active close before '*l'")
remote (string.format ([[
data:close()
data = nil
@ -546,9 +552,9 @@ function test_readafterclose()
data:close()
back, err, partial = data:receive()
assert(back == nil and err == "closed", "should have returned 'closed'")
print("ok")
pass("ok")
reconnect()
pass("trying active close before raw 1")
printf("trying active close before raw 1")
remote (string.format ([[
data:close()
data = nil
@ -556,9 +562,9 @@ function test_readafterclose()
data:close()
back, err, partial = data:receive(1)
assert(back == nil and err == "closed", "should have returned 'closed'")
print("ok")
pass("ok")
reconnect()
pass("trying active close before raw 0")
printf("trying active close before raw 0")
remote (string.format ([[
data:close()
data = nil
@ -566,7 +572,7 @@ function test_readafterclose()
data:close()
back, err, partial = data:receive(0)
assert(back == nil and err == "closed", "should have returned 'closed'")
print("ok")
pass("ok")
end
------------------------------------------------------------------------
@ -581,13 +587,29 @@ function test_writeafterclose()
while not err do
sent, err, errsent, time = data:send(str)
end
print(sent, err, errsent, time)
print("ok")
assert(err == "closed", "should have returned 'closed'")
pass("ok")
end
------------------------------------------------------------------------
--test_writeafterclose()
function test_partialrecv()
local str = 'little string'
reconnect()
remote([[
data:send("7890")
]])
data:settimeout(1)
back, err = data:receive(10, "123456")
assert(back == "1234567890", "failed on exact mixed length")
back, err = data:receive(8, "87654321")
assert(back == "87654321", "failed on exact length")
back, err = data:receive(4, "87654321")
assert(back == "87654321", "failed on smaller length")
pass("ok")
end
------------------------------------------------------------------------
test("method registration")
test_methods(socket.tcp(), {
"accept",
@ -629,12 +651,18 @@ test_methods(socket.udp(), {
"settimeout"
})
test("partial receive")
test_partialrecv()
test("select function")
test_selectbugs()
test("testing read after close")
test("read after close")
test_readafterclose()
test("write after close")
test_writeafterclose()
test("connect function")
connect_timeout()
empty_connect()

View file

@ -1,13 +1,13 @@
function readfile(name)
local f = io.open(name, "rb")
if not f then return nil end
local s = f:read("*a")
f:close()
return s
local f = io.open(name, "rb")
if not f then return nil end
local s = f:read("*a")
f:close()
return s
end
function similar(s1, s2)
return string.lower(string.gsub(s1 or "", "%s", "")) ==
return string.lower(string.gsub(s1 or "", "%s", "")) ==
string.lower(string.gsub(s2 or "", "%s", ""))
end
@ -28,7 +28,7 @@ local set = rawset
local warn = print
local setglobal = function(table, key, value)
warn("changed " .. key)
warn("changed " .. key)
set(table, key, value)
end

View file

@ -3,439 +3,439 @@ socket.url = require("socket.url")
dofile("testsupport.lua")
local check_build_url = function(parsed)
local built = socket.url.build(parsed)
local built = socket.url.build(parsed)
if built ~= parsed.url then
print("built is different from expected")
print(built)
print(expected)
exit()
end
print("built is different from expected")
print(built)
print(expected)
exit()
end
end
local check_protect = function(parsed, path, unsafe)
local built = socket.url.build_path(parsed, unsafe)
if built ~= path then
print(built, path)
print("path composition failed.")
exit()
end
local built = socket.url.build_path(parsed, unsafe)
if built ~= path then
print(built, path)
print("path composition failed.")
exit()
end
end
local check_invert = function(url)
local parsed = socket.url.parse(url)
parsed.path = socket.url.build_path(socket.url.parse_path(parsed.path))
local rebuilt = socket.url.build(parsed)
if rebuilt ~= url then
print(url, rebuilt)
print("original and rebuilt are different")
exit()
end
local parsed = socket.url.parse(url)
parsed.path = socket.url.build_path(socket.url.parse_path(parsed.path))
local rebuilt = socket.url.build(parsed)
if rebuilt ~= url then
print(url, rebuilt)
print("original and rebuilt are different")
exit()
end
end
local check_parse_path = function(path, expect)
local parsed = socket.url.parse_path(path)
for i = 1, math.max(table.getn(parsed), table.getn(expect)) do
if parsed[i] ~= expect[i] then
print(path)
local parsed = socket.url.parse_path(path)
for i = 1, math.max(table.getn(parsed), table.getn(expect)) do
if parsed[i] ~= expect[i] then
print(path)
exit()
end
end
if expect.is_directory ~= parsed.is_directory then
print(path)
print("is_directory mismatch")
exit()
end
if expect.is_absolute ~= parsed.is_absolute then
print(path)
print("is_absolute mismatch")
exit()
end
local built = socket.url.build_path(expect)
if built ~= path then
print(built, path)
print("path composition failed.")
exit()
end
end
end
if expect.is_directory ~= parsed.is_directory then
print(path)
print("is_directory mismatch")
exit()
end
if expect.is_absolute ~= parsed.is_absolute then
print(path)
print("is_absolute mismatch")
exit()
end
local built = socket.url.build_path(expect)
if built ~= path then
print(built, path)
print("path composition failed.")
exit()
end
end
local check_absolute_url = function(base, relative, absolute)
local res = socket.url.absolute(base, relative)
if res ~= absolute then
io.write("absolute: In test for '", relative, "' expected '",
local res = socket.url.absolute(base, relative)
if res ~= absolute then
io.write("absolute: In test for '", relative, "' expected '",
absolute, "' but got '", res, "'\n")
exit()
end
exit()
end
end
local check_parse_url = function(gaba)
local url = gaba.url
gaba.url = nil
local parsed = socket.url.parse(url)
for i, v in pairs(gaba) do
if v ~= parsed[i] then
io.write("parse: In test for '", url, "' expected ", i, " = '",
v, "' but got '", tostring(parsed[i]), "'\n")
for i,v in pairs(parsed) do print(i,v) end
exit()
end
end
for i, v in pairs(parsed) do
if v ~= gaba[i] then
io.write("parse: In test for '", url, "' expected ", i, " = '",
tostring(gaba[i]), "' but got '", v, "'\n")
for i,v in pairs(parsed) do print(i,v) end
exit()
end
end
local url = gaba.url
gaba.url = nil
local parsed = socket.url.parse(url)
for i, v in pairs(gaba) do
if v ~= parsed[i] then
io.write("parse: In test for '", url, "' expected ", i, " = '",
v, "' but got '", tostring(parsed[i]), "'\n")
for i,v in pairs(parsed) do print(i,v) end
exit()
end
end
for i, v in pairs(parsed) do
if v ~= gaba[i] then
io.write("parse: In test for '", url, "' expected ", i, " = '",
tostring(gaba[i]), "' but got '", v, "'\n")
for i,v in pairs(parsed) do print(i,v) end
exit()
end
end
end
print("testing URL parsing")
check_parse_url{
url = "scheme://userinfo@host:port/path;params?query#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
url = "scheme://userinfo@host:port/path;params?query#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
}
check_parse_url{
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
authority = "user:password@host:port",
host = "host",
port = "port",
userinfo = "user:password",
user = "user",
password = "password",
path = "/path",
params = "params",
query = "query",
fragment = "fragment",
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
authority = "user:password@host:port",
host = "host",
port = "port",
userinfo = "user:password",
user = "user",
password = "password",
path = "/path",
params = "params",
query = "query",
fragment = "fragment",
}
check_parse_url{
url = "scheme://userinfo@host:port/path;params?query#",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "params",
query = "query",
fragment = ""
url = "scheme://userinfo@host:port/path;params?query#",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "params",
query = "query",
fragment = ""
}
check_parse_url{
url = "scheme://userinfo@host:port/path;params?#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "params",
query = "",
fragment = "fragment"
url = "scheme://userinfo@host:port/path;params?#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "params",
query = "",
fragment = "fragment"
}
check_parse_url{
url = "scheme://userinfo@host:port/path;params#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "params",
fragment = "fragment"
url = "scheme://userinfo@host:port/path;params#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "params",
fragment = "fragment"
}
check_parse_url{
url = "scheme://userinfo@host:port/path;?query#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "",
query = "query",
fragment = "fragment"
url = "scheme://userinfo@host:port/path;?query#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "",
query = "query",
fragment = "fragment"
}
check_parse_url{
url = "scheme://userinfo@host:port/path?query#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
query = "query",
fragment = "fragment"
url = "scheme://userinfo@host:port/path?query#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
query = "query",
fragment = "fragment"
}
check_parse_url{
url = "scheme://userinfo@host:port/;params?query#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/",
params = "params",
query = "query",
fragment = "fragment"
url = "scheme://userinfo@host:port/;params?query#fragment",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/",
params = "params",
query = "query",
fragment = "fragment"
}
check_parse_url{
url = "scheme://userinfo@host:port",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
url = "scheme://userinfo@host:port",
scheme = "scheme",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
}
check_parse_url{
url = "//userinfo@host:port/path;params?query#fragment",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
url = "//userinfo@host:port/path;params?query#fragment",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
}
check_parse_url{
url = "//userinfo@host:port/path",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
url = "//userinfo@host:port/path",
authority = "userinfo@host:port",
host = "host",
port = "port",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
}
check_parse_url{
url = "//userinfo@host/path",
authority = "userinfo@host",
host = "host",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
url = "//userinfo@host/path",
authority = "userinfo@host",
host = "host",
userinfo = "userinfo",
user = "userinfo",
path = "/path",
}
check_parse_url{
url = "//user:password@host/path",
authority = "user:password@host",
host = "host",
userinfo = "user:password",
password = "password",
user = "user",
path = "/path",
url = "//user:password@host/path",
authority = "user:password@host",
host = "host",
userinfo = "user:password",
password = "password",
user = "user",
path = "/path",
}
check_parse_url{
url = "//user:@host/path",
authority = "user:@host",
host = "host",
userinfo = "user:",
password = "",
user = "user",
path = "/path",
url = "//user:@host/path",
authority = "user:@host",
host = "host",
userinfo = "user:",
password = "",
user = "user",
path = "/path",
}
check_parse_url{
url = "//user@host:port/path",
authority = "user@host:port",
host = "host",
userinfo = "user",
user = "user",
port = "port",
path = "/path",
url = "//user@host:port/path",
authority = "user@host:port",
host = "host",
userinfo = "user",
user = "user",
port = "port",
path = "/path",
}
check_parse_url{
url = "//host:port/path",
authority = "host:port",
port = "port",
host = "host",
path = "/path",
url = "//host:port/path",
authority = "host:port",
port = "port",
host = "host",
path = "/path",
}
check_parse_url{
url = "//host/path",
authority = "host",
host = "host",
path = "/path",
url = "//host/path",
authority = "host",
host = "host",
path = "/path",
}
check_parse_url{
url = "//host",
authority = "host",
host = "host",
url = "//host",
authority = "host",
host = "host",
}
check_parse_url{
url = "/path",
path = "/path",
url = "/path",
path = "/path",
}
check_parse_url{
url = "path",
path = "path",
url = "path",
path = "path",
}
print("testing URL building")
check_build_url {
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
host = "host",
port = "port",
user = "user",
password = "password",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
host = "host",
port = "port",
user = "user",
password = "password",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
}
check_build_url {
url = "scheme://user:password@host/path;params?query#fragment",
scheme = "scheme",
host = "host",
user = "user",
password = "password",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
url = "scheme://user:password@host/path;params?query#fragment",
scheme = "scheme",
host = "host",
user = "user",
password = "password",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
}
check_build_url {
url = "scheme://user@host/path;params?query#fragment",
scheme = "scheme",
host = "host",
user = "user",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
url = "scheme://user@host/path;params?query#fragment",
scheme = "scheme",
host = "host",
user = "user",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
}
check_build_url {
url = "scheme://host/path;params?query#fragment",
scheme = "scheme",
host = "host",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
url = "scheme://host/path;params?query#fragment",
scheme = "scheme",
host = "host",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
}
check_build_url {
url = "scheme://host/path;params#fragment",
scheme = "scheme",
host = "host",
path = "/path",
params = "params",
fragment = "fragment"
url = "scheme://host/path;params#fragment",
scheme = "scheme",
host = "host",
path = "/path",
params = "params",
fragment = "fragment"
}
check_build_url {
url = "scheme://host/path#fragment",
scheme = "scheme",
host = "host",
path = "/path",
fragment = "fragment"
url = "scheme://host/path#fragment",
scheme = "scheme",
host = "host",
path = "/path",
fragment = "fragment"
}
check_build_url {
url = "scheme://host/path",
scheme = "scheme",
host = "host",
path = "/path",
url = "scheme://host/path",
scheme = "scheme",
host = "host",
path = "/path",
}
check_build_url {
url = "//host/path",
host = "host",
path = "/path",
url = "//host/path",
host = "host",
path = "/path",
}
check_build_url {
url = "/path",
path = "/path",
url = "/path",
path = "/path",
}
check_build_url {
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
host = "host",
port = "port",
user = "user",
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
host = "host",
port = "port",
user = "user",
userinfo = "not used",
password = "password",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
password = "password",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
}
check_build_url {
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
host = "host",
port = "port",
user = "user",
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
host = "host",
port = "port",
user = "user",
userinfo = "not used",
authority = "not used",
password = "password",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
password = "password",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
}
check_build_url {
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
host = "host",
port = "port",
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
host = "host",
port = "port",
userinfo = "user:password",
authority = "not used",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
}
check_build_url {
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
url = "scheme://user:password@host:port/path;params?query#fragment",
scheme = "scheme",
authority = "user:password@host:port",
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
path = "/path",
params = "params",
query = "query",
fragment = "fragment"
}
-- standard RFC tests
@ -488,11 +488,11 @@ print("testing path parsing and composition")
check_parse_path("/eu/tu/ele", { "eu", "tu", "ele"; is_absolute = 1 })
check_parse_path("/eu/", { "eu"; is_absolute = 1, is_directory = 1 })
check_parse_path("eu/tu/ele/nos/vos/eles/",
{ "eu", "tu", "ele", "nos", "vos", "eles"; is_directory = 1})
{ "eu", "tu", "ele", "nos", "vos", "eles"; is_directory = 1})
check_parse_path("/", { is_absolute = 1, is_directory = 1})
check_parse_path("", { })
check_parse_path("eu%01/%02tu/e%03l%04e/nos/vos%05/e%12les/",
{ "eu\1", "\2tu", "e\3l\4e", "nos", "vos\5", "e\18les"; is_directory = 1})
{ "eu\1", "\2tu", "e\3l\4e", "nos", "vos\5", "e\18les"; is_directory = 1})
check_parse_path("eu/tu", { "eu", "tu" })
print("testing path protection")

View file

@ -298,7 +298,7 @@ function empty_connect()
pass("ok")
data = socket.connect(host, port)
else
pass("gethostbyname returns localhost on empty string...")
pass("gethostbyname returns localhost on empty string...")
end
end