diff options
Diffstat (limited to 'test/httptest.lua')
| -rw-r--r-- | test/httptest.lua | 80 |
1 files changed, 33 insertions, 47 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)) |
