1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
-- needs Alias from /home/c/diego/tec/luasocket/test to
-- /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
local similar = function(s1, s2)
return string.lower(string.gsub(s1 or "", "%s", "")) ==
string.lower(string.gsub(s2 or "", "%s", ""))
end
local fail = function(s)
s = s or "failed!"
print(s)
os.exit()
end
local readfile = function(name)
local f = io.open(name, "r")
if not f then return nil end
local s = f:read("*a")
f:close()
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 = socket.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
local t = socket.time()
host = host or "localhost"
prefix = prefix or "/luasocket"
cgiprefix = cgiprefix or "/luasocket/cgi"
index = readfile("test/index.html")
io.write("testing request uri correctness: ")
local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
local back = socket.http.get("http://" .. host .. forth)
if similar(back, forth) then print("ok")
else fail("failed!") end
io.write("testing query string correctness: ")
forth = "this+is+the+query+string"
back = socket.http.get("http://" .. host .. cgiprefix .. "/query-string?" .. forth)
if similar(back, forth) then print("ok")
else fail("failed!") end
io.write("testing document retrieval: ")
request = {
url = "http://" .. host .. prefix .. "/index.html"
}
expect = {
body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
}
check_request(request, expect, ignore)
io.write("testing http redirection: ")
request = {
url = "http://" .. host .. prefix
}
expect = {
body = index,
code = 200
}
ignore = {
status = 1,
headers = 1
}
check_request(request, expect, ignore)
io.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)
io.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)
io.write("testing host not found: ")
request = {
url = "http://wronghost/does/not/exist"
}
local c, e = socket.connect("wronghost", 80)
expect = {
error = e
}
ignore = {}
check_request(request, expect, ignore)
io.write("testing invalid url: ")
request = {
url = host .. prefix
}
local c, e = socket.connect("", 80)
expect = {
error = e
}
ignore = {}
check_request(request, expect, ignore)
io.write("testing document not found: ")
request = {
url = "http://" .. host .. "/wrongdocument.html"
}
expect = {
code = 404
}
ignore = {
body = 1,
status = 1,
headers = 1
}
check_request(request, expect, ignore)
io.write("testing auth failure: ")
request = {
url = "http://" .. host .. prefix .. "/auth/index.html"
}
expect = {
code = 401
}
ignore = {
body = 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 " .. socket.code.base64("luasocket:password")
}
}
expect = {
code = 200,
body = index
}
ignore = {
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"
}
expect = {
code = 200,
body = index
}
ignore = {
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"
}
expect = {
code = 200,
body = index
}
ignore = {
status = 1,
headers = 1
}
check_request(request, expect, ignore)
io.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)
io.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)
io.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)
io.write("testing wrong scheme: ")
request = {
url = "wrong://" .. host .. cgiprefix .. "/cat",
method = "GET"
}
expect = {
error = "unknown scheme 'wrong'"
}
ignore = {
}
check_request(request, expect, ignore)
local body
io.write("testing simple get function: ")
body = socket.http.get("http://" .. host .. prefix .. "/index.html")
check(body == index)
io.write("testing simple get function with table args: ")
body = socket.http.get {
url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
user = "luasocket",
password = "password"
}
check(body == index)
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)
io.write("testing HEAD method: ")
response = socket.http.request {
method = "HEAD",
url = "http://www.tecgraf.puc-rio.br/~diego/"
}
check(response and response.headers)
print("passed all tests")
print(string.format("done in %.2fs", socket.time() - t))
|