diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/httptest.lua | 80 | ||||
-rw-r--r-- | test/testclnt.lua | 2 | ||||
-rw-r--r-- | test/testmesg.lua | 95 | ||||
-rw-r--r-- | test/urltest.lua | 3 |
4 files changed, 86 insertions, 94 deletions
diff --git a/test/httptest.lua b/test/httptest.lua index a70aa87..61dc60a 100644 --- a/test/httptest.lua +++ b/test/httptest.lua | |||
@@ -4,14 +4,18 @@ | |||
4 | -- to "/luasocket-test-cgi" and "/luasocket-test-cgi/" | 4 | -- to "/luasocket-test-cgi" and "/luasocket-test-cgi/" |
5 | -- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth | 5 | -- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth |
6 | 6 | ||
7 | require("http") | 7 | socket = require("socket") |
8 | http = require("http") | ||
9 | mime = require("mime") | ||
10 | url = require("url") | ||
11 | ltn12 = require("ltn12") | ||
8 | 12 | ||
9 | dofile("testsupport.lua") | 13 | dofile("testsupport.lua") |
10 | 14 | ||
11 | local host, proxy, request, response, index_file | 15 | local host, proxy, request, response, index_file |
12 | local ignore, expect, index, prefix, cgiprefix, index_crlf | 16 | local ignore, expect, index, prefix, cgiprefix, index_crlf |
13 | 17 | ||
14 | socket.http.TIMEOUT = 10 | 18 | http.TIMEOUT = 10 |
15 | 19 | ||
16 | local t = socket.time() | 20 | local t = socket.time() |
17 | 21 | ||
@@ -56,7 +60,7 @@ local check_request = function(request, expect, ignore) | |||
56 | end | 60 | end |
57 | request.source = request.source or | 61 | request.source = request.source or |
58 | (request.body and ltn12.source.string(request.body)) | 62 | (request.body and ltn12.source.string(request.body)) |
59 | local response = socket.http.request(request) | 63 | local response = http.request(request) |
60 | if t and table.getn(t) > 0 then response.body = table.concat(t) end | 64 | if t and table.getn(t) > 0 then response.body = table.concat(t) end |
61 | check_result(response, expect, ignore) | 65 | check_result(response, expect, ignore) |
62 | end | 66 | end |
@@ -64,16 +68,16 @@ end | |||
64 | ------------------------------------------------------------------------ | 68 | ------------------------------------------------------------------------ |
65 | io.write("testing request uri correctness: ") | 69 | io.write("testing request uri correctness: ") |
66 | local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string" | 70 | local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string" |
67 | local back, h, c, e = socket.http.get("http://" .. host .. forth) | 71 | local back, h, c, e = http.get("http://" .. host .. forth) |
68 | if not back then fail(e) end | 72 | if not back then fail(e) end |
69 | back = socket.url.parse(back) | 73 | back = url.parse(back) |
70 | if similar(back.query, "this+is+the+query+string") then print("ok") | 74 | if similar(back.query, "this+is+the+query+string") then print("ok") |
71 | else fail(back.query) end | 75 | else fail(back.query) end |
72 | 76 | ||
73 | ------------------------------------------------------------------------ | 77 | ------------------------------------------------------------------------ |
74 | io.write("testing query string correctness: ") | 78 | io.write("testing query string correctness: ") |
75 | forth = "this+is+the+query+string" | 79 | forth = "this+is+the+query+string" |
76 | back = socket.http.get("http://" .. host .. cgiprefix .. | 80 | back = http.get("http://" .. host .. cgiprefix .. |
77 | "/query-string?" .. forth) | 81 | "/query-string?" .. forth) |
78 | if similar(back, forth) then print("ok") | 82 | if similar(back, forth) then print("ok") |
79 | else fail("failed!") end | 83 | else fail("failed!") end |
@@ -149,7 +153,7 @@ check_request(request, expect, ignore) | |||
149 | 153 | ||
150 | ------------------------------------------------------------------------ | 154 | ------------------------------------------------------------------------ |
151 | io.write("testing simple post function: ") | 155 | io.write("testing simple post function: ") |
152 | back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index) | 156 | back = http.post("http://" .. host .. cgiprefix .. "/cat", index) |
153 | assert(back == index) | 157 | assert(back == index) |
154 | 158 | ||
155 | ------------------------------------------------------------------------ | 159 | ------------------------------------------------------------------------ |
@@ -278,30 +282,6 @@ ignore = { | |||
278 | check_request(request, expect, ignore) | 282 | check_request(request, expect, ignore) |
279 | 283 | ||
280 | ------------------------------------------------------------------------ | 284 | ------------------------------------------------------------------------ |
281 | io.write("testing host not found: ") | ||
282 | request = { | ||
283 | url = "http://wronghost/does/not/exist" | ||
284 | } | ||
285 | local c, e = socket.connect("wronghost", 80) | ||
286 | expect = { | ||
287 | error = e | ||
288 | } | ||
289 | ignore = {} | ||
290 | check_request(request, expect, ignore) | ||
291 | |||
292 | ------------------------------------------------------------------------ | ||
293 | io.write("testing invalid url: ") | ||
294 | request = { | ||
295 | url = host .. prefix | ||
296 | } | ||
297 | local c, e = socket.connect("", 80) | ||
298 | expect = { | ||
299 | error = e | ||
300 | } | ||
301 | ignore = {} | ||
302 | check_request(request, expect, ignore) | ||
303 | |||
304 | ------------------------------------------------------------------------ | ||
305 | io.write("testing document not found: ") | 285 | io.write("testing document not found: ") |
306 | request = { | 286 | request = { |
307 | url = "http://" .. host .. "/wrongdocument.html" | 287 | url = "http://" .. host .. "/wrongdocument.html" |
@@ -397,29 +377,16 @@ ignore = { | |||
397 | check_request(request, expect, ignore) | 377 | check_request(request, expect, ignore) |
398 | 378 | ||
399 | ------------------------------------------------------------------------ | 379 | ------------------------------------------------------------------------ |
400 | io.write("testing wrong scheme: ") | ||
401 | request = { | ||
402 | url = "wrong://" .. host .. cgiprefix .. "/cat", | ||
403 | method = "GET" | ||
404 | } | ||
405 | expect = { | ||
406 | error = "unknown scheme 'wrong'" | ||
407 | } | ||
408 | ignore = { | ||
409 | } | ||
410 | check_request(request, expect, ignore) | ||
411 | |||
412 | ------------------------------------------------------------------------ | ||
413 | local body | 380 | local body |
414 | io.write("testing simple get function: ") | 381 | io.write("testing simple get function: ") |
415 | body = socket.http.get("http://" .. host .. prefix .. "/index.html") | 382 | body = http.get("http://" .. host .. prefix .. "/index.html") |
416 | assert(body == index) | 383 | assert(body == index) |
417 | print("ok") | 384 | print("ok") |
418 | 385 | ||
419 | ------------------------------------------------------------------------ | 386 | ------------------------------------------------------------------------ |
420 | io.write("testing HEAD method: ") | 387 | io.write("testing HEAD method: ") |
421 | socket.http.TIMEOUT = 1 | 388 | http.TIMEOUT = 1 |
422 | response = socket.http.request { | 389 | response = http.request { |
423 | method = "HEAD", | 390 | method = "HEAD", |
424 | url = "http://www.cs.princeton.edu/~diego/" | 391 | url = "http://www.cs.princeton.edu/~diego/" |
425 | } | 392 | } |
@@ -427,6 +394,25 @@ assert(response and response.headers) | |||
427 | print("ok") | 394 | print("ok") |
428 | 395 | ||
429 | ------------------------------------------------------------------------ | 396 | ------------------------------------------------------------------------ |
397 | io.write("testing host not found: ") | ||
398 | local c, e = socket.connect("wronghost", 80) | ||
399 | local r, re = http.request{url = "http://wronghost/does/not/exist"} | ||
400 | assert(r == nil and e == re) | ||
401 | r, re = http.get("http://wronghost/does/not/exist") | ||
402 | assert(r == nil and e == re) | ||
403 | print("ok") | ||
404 | |||
405 | ------------------------------------------------------------------------ | ||
406 | io.write("testing invalid url: ") | ||
407 | local c, e = socket.connect("", 80) | ||
408 | local r, re = http.request{url = host .. prefix} | ||
409 | assert(r == nil and e == re) | ||
410 | r, re = http.get(host .. prefix) | ||
411 | assert(r == nil and e == re) | ||
412 | print("ok") | ||
413 | |||
414 | ------------------------------------------------------------------------ | ||
430 | print("passed all tests") | 415 | print("passed all tests") |
416 | os.remove("err") | ||
431 | 417 | ||
432 | print(string.format("done in %.2fs", socket.time() - t)) | 418 | print(string.format("done in %.2fs", socket.time() - t)) |
diff --git a/test/testclnt.lua b/test/testclnt.lua index 1825007..14d3484 100644 --- a/test/testclnt.lua +++ b/test/testclnt.lua | |||
@@ -69,7 +69,7 @@ function check_timeout(tm, sl, elapsed, err, opp, mode, alldone) | |||
69 | end | 69 | end |
70 | end | 70 | end |
71 | 71 | ||
72 | if not socket.debug then | 72 | if not socket.DEBUG then |
73 | fail("Please define LUASOCKET_DEBUG and recompile LuaSocket") | 73 | fail("Please define LUASOCKET_DEBUG and recompile LuaSocket") |
74 | end | 74 | end |
75 | 75 | ||
diff --git a/test/testmesg.lua b/test/testmesg.lua index 15b0eb7..449f4c2 100644 --- a/test/testmesg.lua +++ b/test/testmesg.lua | |||
@@ -1,52 +1,57 @@ | |||
1 | require("smtp") | 1 | -- load the smtp support and its friends |
2 | require("mime") | 2 | local smtp = require("smtp") |
3 | local mime = require("mime") | ||
4 | local ltn12 = require("ltn12") | ||
3 | 5 | ||
4 | mesgt = { | 6 | -- creates a source to send a message with two parts. The first part is |
5 | headers = { | 7 | -- plain text, the second part is a PNG image, encoded as base64. |
6 | to = "D Burgess <db@werx4.com>", | 8 | source = smtp.message{ |
7 | subject = "Looking good! (please check headers)" | 9 | headers = { |
10 | -- Remember that headers are *ignored* by smtp.send. | ||
11 | from = "Sicrano <sicrano@tecgraf.puc-rio.br>", | ||
12 | to = "Fulano <fulano@tecgraf.puc-rio.br>", | ||
13 | subject = "Here is a message with attachments" | ||
14 | }, | ||
15 | body = { | ||
16 | preamble = "If your client doesn't understand attachments, \r\n" .. | ||
17 | "it will still display the preamble and the epilogue.\r\n", | ||
18 | "Preamble might show up even in a MIME enabled client.", | ||
19 | -- first part: No headers means plain text, us-ascii. | ||
20 | -- The mime.eol low-level filter normalizes end-of-line markers. | ||
21 | [1] = { | ||
22 | body = mime.eol(0, [[ | ||
23 | Lines in a message body should always end with CRLF. | ||
24 | The smtp module will *NOT* perform translation. It will | ||
25 | perform necessary stuffing, though. | ||
26 | ]]) | ||
8 | }, | 27 | }, |
9 | body = { | 28 | -- second part: Headers describe content the to be an image, |
10 | preamble = "Some attatched stuff", | 29 | -- sent under the base64 transfer content encoding. |
11 | [1] = { | 30 | -- Notice that nothing happens until the message is sent. Small |
12 | body = mime.eol(0, "Testing stuffing.\n.\nGot you.\n.Hehehe.\n") | 31 | -- chunks are loaded into memory and translation happens on the fly. |
13 | }, | 32 | [2] = { |
14 | [2] = { | 33 | headers = { |
15 | headers = { | 34 | ["content-type"] = 'image/png; name="image.png"', |
16 | ["content-type"] = 'application/octet-stream; name="testmesg.lua"', | 35 | ["content-disposition"] = 'attachment; filename="image.png"', |
17 | ["content-disposition"] = 'attachment; filename="testmesg.lua"', | 36 | ["content-description"] = 'a beautiful image', |
18 | ["content-transfer-encoding"] = "BASE64" | 37 | ["content-transfer-encoding"] = "BASE64" |
19 | }, | 38 | }, |
20 | body = ltn12.source.chain( | 39 | body = ltn12.source.chain( |
21 | ltn12.source.file(io.open("testmesg.lua", "rb")), | 40 | ltn12.source.file(io.open("image.png", "rb")), |
22 | ltn12.filter.chain( | 41 | ltn12.filter.chain( |
23 | mime.encode("base64"), | 42 | mime.encode("base64"), |
24 | mime.wrap() | 43 | mime.wrap() |
25 | ) | 44 | ) |
26 | ) | 45 | ) |
27 | }, | 46 | }, |
28 | [3] = { | 47 | epilogue = "This might also show up, but after the attachments" |
29 | headers = { | 48 | } |
30 | ["content-type"] = 'text/plain; name="testmesg.lua"', | ||
31 | ["content-disposition"] = 'attachment; filename="testmesg.lua"', | ||
32 | ["content-transfer-encoding"] = "QUOTED-PRINTABLE" | ||
33 | }, | ||
34 | body = ltn12.source.chain( | ||
35 | ltn12.source.file(io.open("testmesg.lua", "rb")), | ||
36 | ltn12.filter.chain( | ||
37 | mime.normalize(), | ||
38 | mime.encode("quoted-printable"), | ||
39 | mime.wrap("quoted-printable") | ||
40 | ) | ||
41 | ) | ||
42 | }, | ||
43 | epilogue = "Done attaching stuff", | ||
44 | } | ||
45 | } | 49 | } |
46 | 50 | ||
47 | print(socket.smtp.send { | 51 | -- finally send it |
52 | r, e = smtp.send{ | ||
48 | rcpt = "<diego@cs.princeton.edu>", | 53 | rcpt = "<diego@cs.princeton.edu>", |
49 | from = "<diego@cs.princeton.edu>", | 54 | from = "<diego@cs.princeton.edu>", |
50 | source = socket.smtp.message(mesgt), | 55 | source = source, |
51 | server = "mail.cs.princeton.edu" | 56 | server = "mail.cs.princeton.edu" |
52 | }) | 57 | } |
diff --git a/test/urltest.lua b/test/urltest.lua index 8c2ee88..92f2fae 100644 --- a/test/urltest.lua +++ b/test/urltest.lua | |||
@@ -1,4 +1,5 @@ | |||
1 | require"url" | 1 | socket = require("socket") |
2 | socket.url = require("url") | ||
2 | dofile("testsupport.lua") | 3 | dofile("testsupport.lua") |
3 | 4 | ||
4 | local check_build_url = function(parsed) | 5 | local check_build_url = function(parsed) |