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