aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2001-09-25 21:34:43 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2001-09-25 21:34:43 +0000
commitd36460a249261680a4a920f05767b7f7cf2868ba (patch)
tree363b45dc2a34ffc33496df8268af350f389db026
parentb319e6a1e86a54c23b4848c5be7c0670f349a63f (diff)
downloadluasocket-d36460a249261680a4a920f05767b7f7cf2868ba.tar.gz
luasocket-d36460a249261680a4a920f05767b7f7cf2868ba.tar.bz2
luasocket-d36460a249261680a4a920f05767b7f7cf2868ba.zip
updated for luasocket 1.4
-rw-r--r--test/ftptest.lua149
-rw-r--r--test/httptest.lua368
2 files changed, 414 insertions, 103 deletions
diff --git a/test/ftptest.lua b/test/ftptest.lua
index 41f314d..7b0d3ab 100644
--- a/test/ftptest.lua
+++ b/test/ftptest.lua
@@ -1,34 +1,121 @@
1function mysetglobal (varname, oldvalue, newvalue)
2 print("changing " .. varname)
3 %rawset(%globals(), varname, newvalue)
4end
5function mygetglobal (varname, newvalue)
6 print("checking " .. varname)
7 return %rawget(%globals(), varname)
8end
9settagmethod(tag(nil), "setglobal", mysetglobal)
10settagmethod(tag(nil), "getglobal", mygetglobal)
11
1assert(dofile("../lua/ftp.lua")) 12assert(dofile("../lua/ftp.lua"))
2assert(dofile("../lua/buffer.lua")) 13assert(dofile("../lua/url.lua"))
3assert(dofile("auxiliar.lua")) 14assert(dofile("../lua/concat.lua"))
4 15assert(dofile("../lua/code.lua"))
5pdir = "/home/i/diego/public/html/luasocket/test/" 16
6ldir = "/home/luasocket/" 17local similar = function(s1, s2)
7adir = "/home/ftp/test/" 18 return strlower(gsub(s1, "%s", "")) == strlower(gsub(s2, "%s", ""))
8 19end
9-- needs an accound luasocket:password 20
10-- and a copy of /home/i/diego/public/html/luasocket/test in ~ftp/test 21local capture = function(cmd)
11 22 readfrom("| " .. cmd)
12print("testing authenticated upload") 23 local s = read("*a")
13bf = readfile(pdir .. "index.html") 24 readfrom()
14e = ftp_put("ftp://luasocket:password@localhost/index.html", bf, "b") 25 return s
15assert(not e, e) 26end
16assert(compare(ldir .. "index.html", bf), "files differ") 27
17remove(ldir .. "index.html") 28local readfile = function(name)
18 29 local f = readfrom(name)
19print("testing authenticated download") 30 if not f then return nil end
20f, e = ftp_get("ftp://luasocket:password@localhost/test/index.html", "b") 31 local s = read("*a")
21assert(f, e) 32 readfrom()
22assert(compare(pdir .. "index.html", f), "files differ") 33 return s
23 34end
24print("testing anonymous download") 35
25f, e = ftp_get("ftp://localhost/test/index.html", "b") 36local check = function(v, e, o)
26assert(f, e) 37 e = e or "failed!"
27assert(compare(adir .. "index.html", f), "files differ") 38 o = o or "ok"
28 39 if v then print(o)
29print("testing directory listing") 40 else print(e) exit() end
30f, e = ftp_get("ftp://localhost/test/") 41end
31assert(f, e) 42
32assert(f == "index.html\r\n", "files differ") 43-- needs an account luasocket:password
44-- and some directories and files in ~ftp
45
46local index, err, saved, back, expected
47
48local t = _time()
49
50index = readfile("index.html")
51
52write("testing file upload: ")
53remove("/home/ftp/dir1/index.up.html")
54err = FTP.put("ftp://localhost/dir1/index.up.html;type=i", index)
55saved = readfile("/home/ftp/dir1/index.up.html")
56check(not err and saved == index, err)
57
58write("testing file download: ")
59back, err = FTP.get("ftp://localhost/dir1/index.up.html;type=i")
60check(not err and back == index, err)
61
62write("testing no directory changes: ")
63back, err = FTP.get("ftp://localhost/index.html;type=i")
64check(not err and back == index, err)
65
66write("testing multiple directory changes: ")
67back, err = FTP.get("ftp://localhost/dir1/dir2/dir3/dir4/dir5/dir6/index.html;type=i")
68check(not err and back == index, err)
69
70write("testing authenticated upload: ")
71remove("/home/luasocket/index.up.html")
72err = FTP.put("ftp://luasocket:password@localhost/index.up.html;type=i", index)
73saved = readfile("/home/luasocket/index.up.html")
74check(not err and saved == index, err)
75
76write("testing authenticated download: ")
77back, err = FTP.get("ftp://luasocket:password@localhost/index.up.html;type=i")
78check(not err and back == index, err)
79
80write("testing weird-character translation: ")
81back, err = FTP.get("ftp://luasocket:password@localhost/%2fhome/ftp/dir1/index.html;type=i")
82check(not err and back == index, err)
83
84write("testing parameter overriding: ")
85back, err = FTP.get {
86 url = "//stupid:mistake@localhost/dir1/index.html",
87 user = "luasocket",
88 password = "password",
89 type = "i"
90}
91check(not err and back == index, err)
92
93write("testing invalid url: ")
94back, err = FTP.get("localhost/dir1/index.html;type=i")
95local c, e = connect("", 21)
96check(not back and err == e, err)
97
98write("testing directory listing: ")
99expected = capture("ls -F /home/ftp/dir1 | grep -v /")
100back, err = FTP.get("ftp://localhost/dir1;type=d")
101check(similar(back, expected))
102
103write("testing home directory listing: ")
104expected = capture("ls -F /home/ftp | grep -v /")
105back, err = FTP.get("ftp://localhost/")
106check(back and similar(back, expected), nil, err)
107
108write("testing upload denial: ")
109err = FTP.put("ftp://localhost/index.up.html;type=a", index)
110check(err, err)
111
112write("testing authentication failure: ")
113err = FTP.put("ftp://luasocket:wrong@localhost/index.html;type=a", index)
114check(err, err)
115
116write("testing wrong file: ")
117back, err = FTP.get("ftp://localhost/index.wrong.html;type=a")
118check(err, err)
33 119
34print("passed all tests") 120print("passed all tests")
121print(format("done in %.2fs", _time() - t))
diff --git a/test/httptest.lua b/test/httptest.lua
index 8c78192..0b61729 100644
--- a/test/httptest.lua
+++ b/test/httptest.lua
@@ -1,89 +1,313 @@
1-- load http
2assert(dofile("../lua/http.lua"))
3assert(dofile("../lua/base64.lua"))
4assert(dofile("../lua/buffer.lua"))
5assert(dofile("auxiliar.lua"))
6
7t = _time()
8
9-- needs Alias from /home/i/diego/public/html/luasocket/test to 1-- needs Alias from /home/i/diego/public/html/luasocket/test to
10-- /luasocket-test 2-- /luasocket-test
11-- needs ScriptAlias from /home/i/diego/public/html/luasocket/test/cgi-bin 3-- needs ScriptAlias from /home/i/diego/public/html/luasocket/test/cgi
12-- to /luasocket-cgi-bin/ 4-- to /luasocket-test/cgi
5
6function mysetglobal (varname, oldvalue, newvalue)
7 print("changing " .. varname)
8 %rawset(%globals(), varname, newvalue)
9end
10function mygetglobal (varname, newvalue)
11 print("checking " .. varname)
12 return %rawget(%globals(), varname)
13end
14settagmethod(tag(nil), "setglobal", mysetglobal)
15settagmethod(tag(nil), "getglobal", mygetglobal)
16
17local similar = function(s1, s2)
18 return strlower(gsub(s1, "%s", "")) == strlower(gsub(s2, "%s", ""))
19end
13 20
14function join(s, e) 21local fail = function(s)
15 return tostring(s) .. ":" .. tostring(e) 22 s = s or "failed!"
23 print(s)
24 exit()
16end 25end
17 26
18function status(s) 27local readfile = function(name)
19 local code 28 local f = readfrom(name)
20 _,_, code = strfind(s, "(%d%d%d)") 29 if not f then return nil end
21 return tonumber(code) 30 local s = read("*a")
31 readfrom()
32 return s
22end 33end
23 34
24pdir = pdir or "/home/i/diego/public/html/luasocket/test/" 35local check = function (v, e)
36 if v then print("ok")
37 else %fail(e) end
38end
39
40local check_request = function(request, expect, ignore)
41 local response = HTTP.request(request)
42 for i,v in response do
43 if not ignore[i] then
44 if v ~= expect[i] then %fail(i .. " differs!") end
45 end
46 end
47 for i,v in expect do
48 if not ignore[i] then
49 if v ~= response[i] then %fail(i .. " differs!") end
50 end
51 end
52 print("ok")
53end
54
55local host, request, response, ignore, expect, index, prefix, cgiprefix
56
57-- load http
58assert(dofile("../lua/http.lua"))
59assert(dofile("../lua/code.lua"))
60assert(dofile("../lua/concat.lua"))
61assert(dofile("../lua/url.lua"))
62
63local t = _time()
64
25host = host or "localhost" 65host = host or "localhost"
66prefix = prefix or "/luasocket-test"
67cgiprefix = cgiprefix or "/luasocket-test-cgi"
68index = readfile("index.html")
69
70write("testing request uri correctness: ")
71local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
72local back = HTTP.get("http://" .. host .. forth)
73if similar(back, forth) then print("ok")
74else fail("failed!") end
26 75
27print("testing document retrieval") 76write("testing query string correctness: ")
28url = "http://" .. host .. "/luasocket-test/index.html" 77forth = "this+is+the+query+string"
29f, m, s, e = http_get(url) 78back = HTTP.get("http://" .. host .. cgiprefix .. "/query-string?" .. forth)
30assert(f and m and s and not e, join(s, e)) 79if similar(back, forth) then print("ok")
31assert(compare(pdir .. "index.html", f), "documents differ") 80else fail("failed!") end
32
33print("testing HTTP redirection")
34url = "http://" .. host .. "/luasocket-test"
35f, m, s, e = http_get(url)
36assert(f and m and s and not e, join(s, e))
37assert(compare(pdir .. "index.html", f), "documents differ")
38
39print("testing cgi output retrieval (probably chunked...)")
40url = "http://" .. host .. "/luasocket-cgi-bin/cat-index-html"
41f, m, s, e = http_get(url)
42assert(f and m and s and not e, join(s, e))
43assert(compare(pdir .. "index.html", f), "documents differ")
44
45print("testing post method")
46url = "http://" .. host .. "/luasocket-cgi-bin/cat"
47rf = strrep("!@#$!@#%", 80000)
48f, m, s, e = http_post(url, rf)
49assert(f and m and s and not e)
50assert(rf == f, "files differ")
51
52print("testing automatic auth failure")
53url = "http://really:wrong@" .. host .. "/luasocket-test/auth/index.html"
54f, m, s, e = http_get(url)
55assert(f and m and s and not e and status(s) == 401)
56 81
82write("testing document retrieval: ")
83request = {
84 url = "http://" .. host .. prefix .. "/index.html"
85}
86expect = {
87 body = index,
88 code = 200
89}
90ignore = {
91 status = 1,
92 headers = 1
93}
94check_request(request, expect, ignore)
95
96write("testing HTTP redirection: ")
97request = {
98 url = "http://" .. host .. prefix
99}
100expect = {
101 body = index,
102 code = 200
103}
104ignore = {
105 status = 1,
106 headers = 1
107}
108check_request(request, expect, ignore)
109
110
111write("testing automatic auth failure: ")
112request = {
113 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html"
114}
115expect = {
116 code = 401
117}
118ignore = {
119 body = 1,
120 status = 1,
121 headers = 1
122}
123check_request(request, expect, ignore)
124
125write("testing HTTP redirection failure: ")
126request = {
127 url = "http://" .. host .. prefix,
128 stay = 1
129}
130expect = {
131 code = 301
132}
133ignore = {
134 body = 1,
135 status = 1,
136 headers = 1
137}
138check_request(request, expect, ignore)
139
57write("testing host not found: ") 140write("testing host not found: ")
58url = "http://wronghost/luasocket-test/index.html" 141request = {
59f, m, s, e = http_get(url) 142 url = "http://wronghost/does/not/exist"
60assert(not f and not m and not s and e) 143}
61print(e) 144local c, e = connect("wronghost", 80)
145expect = {
146 error = e
147}
148ignore = {}
149check_request(request, expect, ignore)
62 150
63write("testing auth failure: ") 151write("testing invalid url: ")
64url = "http://" .. host .. "/luasocket-test/auth/index.html" 152request = {
65f, m, s, e = http_get(url) 153 url = host .. prefix
66assert(f and m and s and not e and status(s) == 401) 154}
67print(s) 155local c, e = connect("", 80)
156expect = {
157 error = e
158}
159ignore = {}
160check_request(request, expect, ignore)
68 161
69write("testing document not found: ") 162write("testing document not found: ")
70url = "http://" .. host .. "/luasocket-test/wrongdocument.html" 163request = {
71f, m, s, e = http_get(url) 164 url = "http://" .. host .. "/wrongdocument.html"
72assert(f and m and s and not e and status(s) == 404) 165}
73print(s) 166expect = {
74 167 code = 404
75print("testing manual auth") 168}
76url = "http://" .. host .. "/luasocket-test/auth/index.html" 169ignore = {
77h = {authorization = "Basic " .. base64("luasocket:password")} 170 body = 1,
78f, m, s, e = http_get(url, h) 171 status = 1,
79assert(f and m and s and not e, join(s, e)) 172 headers = 1
80assert(compare(pdir .. "auth/index.html", f), "documents differ") 173}
81 174check_request(request, expect, ignore)
82print("testing automatic auth") 175
83url = "http://luasocket:password@" .. host .. "/luasocket-test/auth/index.html" 176write("testing auth failure: ")
84f, m, s, e = http_get(url) 177request = {
85assert(f and m and s and not e, join(s, e)) 178 url = "http://" .. host .. prefix .. "/auth/index.html"
86assert(compare(pdir .. "auth/index.html", f), "documents differ") 179}
180expect = {
181 code = 401
182}
183ignore = {
184 body = 1,
185 status = 1,
186 headers = 1
187}
188check_request(request, expect, ignore)
189
190write("testing manual basic auth: ")
191request = {
192 url = "http://" .. host .. prefix .. "/auth/index.html",
193 headers = {
194 authorization = "Basic " .. Code.base64("luasocket:password")
195 }
196}
197expect = {
198 code = 200,
199 body = index
200}
201ignore = {
202 status = 1,
203 headers = 1
204}
205check_request(request, expect, ignore)
206
207write("testing automatic basic auth: ")
208request = {
209 url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html"
210}
211expect = {
212 code = 200,
213 body = index
214}
215ignore = {
216 status = 1,
217 headers = 1
218}
219check_request(request, expect, ignore)
220
221write("testing auth info overriding: ")
222request = {
223 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
224 user = "luasocket",
225 password = "password"
226}
227expect = {
228 code = 200,
229 body = index
230}
231ignore = {
232 status = 1,
233 headers = 1
234}
235check_request(request, expect, ignore)
236
237write("testing cgi output retrieval (probably chunked...): ")
238request = {
239 url = "http://" .. host .. cgiprefix .. "/cat-index-html"
240}
241expect = {
242 body = index,
243 code = 200
244}
245ignore = {
246 status = 1,
247 headers = 1
248}
249check_request(request, expect, ignore)
250
251write("testing redirect loop: ")
252request = {
253 url = "http://" .. host .. cgiprefix .. "/redirect-loop"
254}
255expect = {
256 code = 302
257}
258ignore = {
259 status = 1,
260 headers = 1,
261 body = 1
262}
263check_request(request, expect, ignore)
264
265write("testing post method: ")
266request = {
267 url = "http://" .. host .. cgiprefix .. "/cat",
268 method = "POST",
269 body = index
270}
271expect = {
272 body = index,
273 code = 200
274}
275ignore = {
276 status = 1,
277 headers = 1
278}
279check_request(request, expect, ignore)
280
281local body
282write("testing simple get function: ")
283body = HTTP.get("http://" .. host .. prefix .. "/index.html")
284check(body == index)
285
286write("testing simple get function with table args: ")
287body = HTTP.get {
288 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
289 user = "luasocket",
290 password = "password"
291}
292check(body == index)
293
294write("testing simple post function: ")
295body = HTTP.post("http://" .. host .. cgiprefix .. "/cat", index)
296check(body == index)
297
298write("testing simple post function with table args: ")
299body = HTTP.post {
300 url = "http://" .. host .. cgiprefix .. "/cat",
301 body = index
302}
303check(body == index)
304
305write("testing HEAD method: ")
306response = HTTP.request {
307 method = "HEAD",
308 url = "http://www.tecgraf.puc-rio.br/~diego/"
309}
310check(response and response.headers)
87 311
88print("passed all tests") 312print("passed all tests")
89 313