aboutsummaryrefslogtreecommitdiff
path: root/test/httptest.lua
blob: 8c78192a63fe72e5df81009dcdf636a330b0e230 (plain)
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
-- 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 
-- /luasocket-test
-- needs ScriptAlias from /home/i/diego/public/html/luasocket/test/cgi-bin
-- to /luasocket-cgi-bin/

function join(s, e)
  return tostring(s) .. ":" .. tostring(e)
end

function status(s)
	local code
	_,_, code = strfind(s, "(%d%d%d)")
	return tonumber(code)
end

pdir = pdir or "/home/i/diego/public/html/luasocket/test/"
host = host or "localhost"

print("testing document retrieval")
url = "http://" .. host .. "/luasocket-test/index.html"
f, m, s, e = http_get(url)
assert(f and m and s and not e, join(s, e))
assert(compare(pdir .. "index.html", f), "documents differ")

print("testing HTTP redirection")
url = "http://" .. host .. "/luasocket-test"
f, m, s, e = http_get(url)
assert(f and m and s and not e, join(s, e))
assert(compare(pdir .. "index.html", f), "documents differ")

print("testing cgi output retrieval (probably chunked...)")
url = "http://" .. host .. "/luasocket-cgi-bin/cat-index-html"
f, m, s, e = http_get(url)
assert(f and m and s and not e, join(s, e))
assert(compare(pdir .. "index.html", f), "documents differ")

print("testing post method")
url = "http://" .. host .. "/luasocket-cgi-bin/cat"
rf = strrep("!@#$!@#%", 80000)
f, m, s, e = http_post(url, rf)
assert(f and m and s and not e)
assert(rf == f, "files differ")

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 host not found: ")
url = "http://wronghost/luasocket-test/index.html"
f, m, s, e = http_get(url)
assert(not f and not m and not s and e)
print(e)

write("testing auth failure: ")
url = "http://" .. 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)
print(s)

write("testing document not found: ")
url = "http://" .. host .. "/luasocket-test/wrongdocument.html"
f, m, s, e = http_get(url)
assert(f and m and s and not e and status(s) == 404)
print(s)

print("testing manual auth")
url = "http://" .. host .. "/luasocket-test/auth/index.html"
h = {authorization = "Basic " .. base64("luasocket:password")}
f, m, s, e = http_get(url, h)
assert(f and m and s and not e, join(s, e))
assert(compare(pdir .. "auth/index.html", f), "documents differ")

print("testing automatic auth")
url = "http://luasocket:password@" .. host .. "/luasocket-test/auth/index.html"
f, m, s, e = http_get(url)
assert(f and m and s and not e, join(s, e))
assert(compare(pdir .. "auth/index.html", f), "documents differ")

print("passed all tests")

print(format("done in %.2fs", _time() - t))