diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-16 06:42:53 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-03-16 06:42:53 +0000 |
| commit | bcc0c2a9f0be2ca796ef5206a78e283fe15e6186 (patch) | |
| tree | 65c269d4854aa5ff4a0b2c8eede5cdb18d716033 /test | |
| parent | b6edaac2841137cf0ef5105f75358bbab4570d87 (diff) | |
| download | luasocket-bcc0c2a9f0be2ca796ef5206a78e283fe15e6186.tar.gz luasocket-bcc0c2a9f0be2ca796ef5206a78e283fe15e6186.tar.bz2 luasocket-bcc0c2a9f0be2ca796ef5206a78e283fe15e6186.zip | |
New filter scheme.
ltn12 and mime updated.
smtp/ftp broken.
Diffstat (limited to 'test')
| -rw-r--r-- | test/httptest.lua | 45 | ||||
| -rw-r--r-- | test/mimetest.lua | 31 | ||||
| -rw-r--r-- | test/stufftest.lua | 19 |
3 files changed, 60 insertions, 35 deletions
diff --git a/test/httptest.lua b/test/httptest.lua index c9a74a8..04c0ed0 100644 --- a/test/httptest.lua +++ b/test/httptest.lua | |||
| @@ -12,7 +12,7 @@ socket.http.TIMEOUT = 5 | |||
| 12 | 12 | ||
| 13 | local t = socket.time() | 13 | local t = socket.time() |
| 14 | 14 | ||
| 15 | host = host or "diego-interface2.student.dyn.CS.Princeton.EDU" | 15 | host = host or "diego.student.princeton.edu" |
| 16 | proxy = proxy or "http://localhost:3128" | 16 | proxy = proxy or "http://localhost:3128" |
| 17 | prefix = prefix or "/luasocket-test" | 17 | prefix = prefix or "/luasocket-test" |
| 18 | cgiprefix = cgiprefix or "/luasocket-test-cgi" | 18 | cgiprefix = cgiprefix or "/luasocket-test-cgi" |
| @@ -71,8 +71,8 @@ local check_request = function(request, expect, ignore) | |||
| 71 | check_result(response, expect, ignore) | 71 | check_result(response, expect, ignore) |
| 72 | end | 72 | end |
| 73 | 73 | ||
| 74 | local check_request_cb = function(request, response, expect, ignore) | 74 | local check_request_cb = function(request, expect, ignore) |
| 75 | local response = socket.http.request_cb(request, response) | 75 | local response = socket.http.request_cb(request) |
| 76 | check_result(response, expect, ignore) | 76 | check_result(response, expect, ignore) |
| 77 | end | 77 | end |
| 78 | 78 | ||
| @@ -83,7 +83,7 @@ local back, h, c, e = socket.http.get("http://" .. host .. forth) | |||
| 83 | if not back then fail(e) end | 83 | if not back then fail(e) end |
| 84 | back = socket.url.parse(back) | 84 | back = socket.url.parse(back) |
| 85 | if similar(back.query, "this+is+the+query+string") then print("ok") | 85 | if similar(back.query, "this+is+the+query+string") then print("ok") |
| 86 | else fail() end | 86 | else fail(back.query) end |
| 87 | 87 | ||
| 88 | ------------------------------------------------------------------------ | 88 | ------------------------------------------------------------------------ |
| 89 | io.write("testing query string correctness: ") | 89 | io.write("testing query string correctness: ") |
| @@ -168,31 +168,28 @@ back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index) | |||
| 168 | check(back == index) | 168 | check(back == index) |
| 169 | 169 | ||
| 170 | ------------------------------------------------------------------------ | 170 | ------------------------------------------------------------------------ |
| 171 | io.write("testing send.file and receive.file callbacks: ") | 171 | io.write("testing ltn12.(sink|source).file: ") |
| 172 | request = { | 172 | request = { |
| 173 | url = "http://" .. host .. cgiprefix .. "/cat", | 173 | url = "http://" .. host .. cgiprefix .. "/cat", |
| 174 | method = "POST", | 174 | method = "POST", |
| 175 | body_cb = socket.callback.send.file(io.open(index_file, "r")), | 175 | source = ltn12.source.file(io.open(index_file, "r")), |
| 176 | sink = ltn12.sink.file(io.open(index_file .. "-back", "w")), | ||
| 176 | headers = { ["content-length"] = string.len(index) } | 177 | headers = { ["content-length"] = string.len(index) } |
| 177 | } | 178 | } |
| 178 | response = { | ||
| 179 | body_cb = socket.callback.receive.file(io.open(index_file .. "-back", "w")) | ||
| 180 | } | ||
| 181 | expect = { | 179 | expect = { |
| 182 | code = 200 | 180 | code = 200 |
| 183 | } | 181 | } |
| 184 | ignore = { | 182 | ignore = { |
| 185 | body_cb = 1, | ||
| 186 | status = 1, | 183 | status = 1, |
| 187 | headers = 1 | 184 | headers = 1 |
| 188 | } | 185 | } |
| 189 | check_request_cb(request, response, expect, ignore) | 186 | check_request_cb(request, expect, ignore) |
| 190 | back = readfile(index_file .. "-back") | 187 | back = readfile(index_file .. "-back") |
| 191 | check(back == index) | 188 | check(back == index) |
| 192 | os.remove(index_file .. "-back") | 189 | os.remove(index_file .. "-back") |
| 193 | 190 | ||
| 194 | ------------------------------------------------------------------------ | 191 | ------------------------------------------------------------------------ |
| 195 | io.write("testing send.chain and receive.chain callbacks: ") | 192 | io.write("testing ltn12.(sink|source).chain and mime.(encode|decode): ") |
| 196 | 193 | ||
| 197 | local function b64length(len) | 194 | local function b64length(len) |
| 198 | local a = math.ceil(len/3)*4 | 195 | local a = math.ceil(len/3)*4 |
| @@ -200,26 +197,26 @@ local function b64length(len) | |||
| 200 | return a + l*2 | 197 | return a + l*2 |
| 201 | end | 198 | end |
| 202 | 199 | ||
| 203 | local req_cb = socket.callback.send.chain( | 200 | local source = ltn12.source.chain( |
| 204 | socket.callback.send.file(io.open(index_file, "r")), | 201 | ltn12.source.file(io.open(index_file, "r")), |
| 205 | socket.mime.chain( | 202 | ltn12.filter.chain( |
| 206 | socket.mime.encode("base64"), | 203 | mime.encode("base64"), |
| 207 | socket.mime.wrap("base64") | 204 | mime.wrap("base64") |
| 208 | ) | 205 | ) |
| 209 | ) | 206 | ) |
| 210 | 207 | ||
| 211 | local resp_cb = socket.callback.receive.chain( | 208 | local sink = ltn12.sink.chain( |
| 212 | socket.mime.decode("base64"), | 209 | mime.decode("base64"), |
| 213 | socket.callback.receive.file(io.open(index_file .. "-back", "w")) | 210 | ltn12.sink.file(io.open(index_file .. "-back", "w")) |
| 214 | ) | 211 | ) |
| 215 | 212 | ||
| 216 | request = { | 213 | request = { |
| 217 | url = "http://" .. host .. cgiprefix .. "/cat", | 214 | url = "http://" .. host .. cgiprefix .. "/cat", |
| 218 | method = "POST", | 215 | method = "POST", |
| 219 | body_cb = req_cb, | 216 | source = source, |
| 217 | sink = sink, | ||
| 220 | headers = { ["content-length"] = b64length(string.len(index)) } | 218 | headers = { ["content-length"] = b64length(string.len(index)) } |
| 221 | } | 219 | } |
| 222 | response = { body_cb = resp_cb } | ||
| 223 | expect = { | 220 | expect = { |
| 224 | code = 200 | 221 | code = 200 |
| 225 | } | 222 | } |
| @@ -228,7 +225,7 @@ ignore = { | |||
| 228 | status = 1, | 225 | status = 1, |
| 229 | headers = 1 | 226 | headers = 1 |
| 230 | } | 227 | } |
| 231 | check_request_cb(request, response, expect, ignore) | 228 | check_request_cb(request, expect, ignore) |
| 232 | back = readfile(index_file .. "-back") | 229 | back = readfile(index_file .. "-back") |
| 233 | check(back == index) | 230 | check(back == index) |
| 234 | os.remove(index_file .. "-back") | 231 | os.remove(index_file .. "-back") |
| @@ -362,7 +359,7 @@ io.write("testing manual basic auth: ") | |||
| 362 | request = { | 359 | request = { |
| 363 | url = "http://" .. host .. prefix .. "/auth/index.html", | 360 | url = "http://" .. host .. prefix .. "/auth/index.html", |
| 364 | headers = { | 361 | headers = { |
| 365 | authorization = "Basic " .. (socket.mime.b64("luasocket:password")) | 362 | authorization = "Basic " .. (mime.b64("luasocket:password")) |
| 366 | } | 363 | } |
| 367 | } | 364 | } |
| 368 | expect = { | 365 | expect = { |
diff --git a/test/mimetest.lua b/test/mimetest.lua index 1a7e427..4a0a20a 100644 --- a/test/mimetest.lua +++ b/test/mimetest.lua | |||
| @@ -31,18 +31,27 @@ local mao = [[ | |||
| 31 | assim, nem tudo o que dava exprimia grande confiança. | 31 | assim, nem tudo o que dava exprimia grande confiança. |
| 32 | ]] | 32 | ]] |
| 33 | 33 | ||
| 34 | local function random(handle, io_err) | ||
| 35 | if handle then | ||
| 36 | return function() | ||
| 37 | local chunk = handle:read(math.random(0, 1024)) | ||
| 38 | if not chunk then handle:close() end | ||
| 39 | return chunk | ||
| 40 | end | ||
| 41 | else source.empty(io_err or "unable to open file") end | ||
| 42 | end | ||
| 43 | |||
| 44 | local what = nil | ||
| 34 | local function transform(input, output, filter) | 45 | local function transform(input, output, filter) |
| 35 | local fi, err = io.open(input, "rb") | 46 | local source = random(io.open(input, "rb")) |
| 36 | if not fi then fail(err) end | 47 | local sink = ltn12.sink.file(io.open(output, "wb")) |
| 37 | local fo, err = io.open(output, "wb") | 48 | if what then |
| 38 | if not fo then fail(err) end | 49 | sink = ltn12.sink.chain(filter, sink) |
| 39 | while 1 do | 50 | else |
| 40 | local chunk = fi:read(math.random(0, 1024)) | 51 | source = ltn12.source.chain(source, filter) |
| 41 | fo:write(filter(chunk)) | 52 | end |
| 42 | if not chunk then break end | 53 | --what = not what |
| 43 | end | 54 | ltn12.pump(source, sink) |
| 44 | fi:close() | ||
| 45 | fo:close() | ||
| 46 | end | 55 | end |
| 47 | 56 | ||
| 48 | local function encode_qptest(mode) | 57 | local function encode_qptest(mode) |
diff --git a/test/stufftest.lua b/test/stufftest.lua new file mode 100644 index 0000000..5eb8005 --- /dev/null +++ b/test/stufftest.lua | |||
| @@ -0,0 +1,19 @@ | |||
| 1 | function test_dot(original, right) | ||
| 2 | local result, n = socket.smtp.dot(2, original) | ||
| 3 | assert(result == right, "->" .. result .. "<-") | ||
| 4 | print("ok") | ||
| 5 | end | ||
| 6 | |||
| 7 | function test_stuff(original, right) | ||
| 8 | local result, n = socket.smtp.dot(2, original) | ||
| 9 | assert(result == right, "->" .. result .. "<-") | ||
| 10 | print("ok") | ||
| 11 | end | ||
| 12 | |||
| 13 | test_dot("abc", "abc") | ||
| 14 | test_dot("", "") | ||
| 15 | test_dot("\r\n", "\r\n") | ||
| 16 | test_dot("\r\n.", "\r\n..") | ||
| 17 | test_dot(".\r\n.", "..\r\n..") | ||
| 18 | test_dot(".\r\n.", "..\r\n..") | ||
| 19 | test_dot("abcd.\r\n.", "abcd.\r\n..") | ||
