diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2002-12-03 07:20:34 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2002-12-03 07:20:34 +0000 |
| commit | 7da19138e37c4e0123860f1fecbceb80c3d2627d (patch) | |
| tree | 8453f003a9ba212807d9c9590c2f2b850d323f0f /test/httptest.lua | |
| parent | d7e80592a69c076991ed4f4cc15d5390e14d1f0b (diff) | |
| download | luasocket-7da19138e37c4e0123860f1fecbceb80c3d2627d.tar.gz luasocket-7da19138e37c4e0123860f1fecbceb80c3d2627d.tar.bz2 luasocket-7da19138e37c4e0123860f1fecbceb80c3d2627d.zip | |
Faltam testes de ftp e smtp. O resto passa.
Diffstat (limited to 'test/httptest.lua')
| -rw-r--r-- | test/httptest.lua | 88 |
1 files changed, 43 insertions, 45 deletions
diff --git a/test/httptest.lua b/test/httptest.lua index 85c8bd8..2941390 100644 --- a/test/httptest.lua +++ b/test/httptest.lua | |||
| @@ -7,21 +7,21 @@ | |||
| 7 | dofile("noglobals.lua") | 7 | dofile("noglobals.lua") |
| 8 | 8 | ||
| 9 | local similar = function(s1, s2) | 9 | local similar = function(s1, s2) |
| 10 | return strlower(gsub(s1 or "", "%s", "")) == | 10 | return string.lower(string.gsub(s1 or "", "%s", "")) == |
| 11 | strlower(gsub(s2 or "", "%s", "")) | 11 | string.lower(string.gsub(s2 or "", "%s", "")) |
| 12 | end | 12 | end |
| 13 | 13 | ||
| 14 | local fail = function(s) | 14 | local fail = function(s) |
| 15 | s = s or "failed!" | 15 | s = s or "failed!" |
| 16 | print(s) | 16 | print(s) |
| 17 | exit() | 17 | os.exit() |
| 18 | end | 18 | end |
| 19 | 19 | ||
| 20 | local readfile = function(name) | 20 | local readfile = function(name) |
| 21 | local f = readfrom(name) | 21 | local f = io.open(name, "r") |
| 22 | if not f then return nil end | 22 | if not f then return nil end |
| 23 | local s = read("*a") | 23 | local s = f:read("*a") |
| 24 | readfrom() | 24 | f:close() |
| 25 | return s | 25 | return s |
| 26 | end | 26 | end |
| 27 | 27 | ||
| @@ -31,7 +31,7 @@ local check = function (v, e) | |||
| 31 | end | 31 | end |
| 32 | 32 | ||
| 33 | local check_request = function(request, expect, ignore) | 33 | local check_request = function(request, expect, ignore) |
| 34 | local response = HTTP.request(request) | 34 | local response = http.request(request) |
| 35 | for i,v in response do | 35 | for i,v in response do |
| 36 | if not ignore[i] then | 36 | if not ignore[i] then |
| 37 | if v ~= expect[i] then %fail(i .. " differs!") end | 37 | if v ~= expect[i] then %fail(i .. " differs!") end |
| @@ -45,30 +45,28 @@ local check_request = function(request, expect, ignore) | |||
| 45 | print("ok") | 45 | print("ok") |
| 46 | end | 46 | end |
| 47 | 47 | ||
| 48 | dofile("../src/modules/http.lua") | ||
| 49 | |||
| 50 | local request, response, ignore, expect, index, prefix, cgiprefix | 48 | local request, response, ignore, expect, index, prefix, cgiprefix |
| 51 | 49 | ||
| 52 | local t = _time() | 50 | local t = socket._time() |
| 53 | 51 | ||
| 54 | HOST = HOST or "localhost" | 52 | HOST = HOST or "localhost" |
| 55 | prefix = prefix or "/luasocket-test" | 53 | prefix = prefix or "/luasocket" |
| 56 | cgiprefix = cgiprefix or "/luasocket-test-cgi" | 54 | cgiprefix = cgiprefix or "/luasocket/cgi" |
| 57 | index = readfile("index.html") | 55 | index = readfile("test/index.html") |
| 58 | 56 | ||
| 59 | write("testing request uri correctness: ") | 57 | io.write("testing request uri correctness: ") |
| 60 | local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string" | 58 | local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string" |
| 61 | local back = HTTP.get("http://" .. HOST .. forth) | 59 | local back = http.get("http://" .. HOST .. forth) |
| 62 | if similar(back, forth) then print("ok") | 60 | if similar(back, forth) then print("ok") |
| 63 | else fail("failed!") end | 61 | else fail("failed!") end |
| 64 | 62 | ||
| 65 | write("testing query string correctness: ") | 63 | io.write("testing query string correctness: ") |
| 66 | forth = "this+is+the+query+string" | 64 | forth = "this+is+the+query+string" |
| 67 | back = HTTP.get("http://" .. HOST .. cgiprefix .. "/query-string?" .. forth) | 65 | back = http.get("http://" .. HOST .. cgiprefix .. "/query-string?" .. forth) |
| 68 | if similar(back, forth) then print("ok") | 66 | if similar(back, forth) then print("ok") |
| 69 | else fail("failed!") end | 67 | else fail("failed!") end |
| 70 | 68 | ||
| 71 | write("testing document retrieval: ") | 69 | io.write("testing document retrieval: ") |
| 72 | request = { | 70 | request = { |
| 73 | url = "http://" .. HOST .. prefix .. "/index.html" | 71 | url = "http://" .. HOST .. prefix .. "/index.html" |
| 74 | } | 72 | } |
| @@ -82,7 +80,7 @@ ignore = { | |||
| 82 | } | 80 | } |
| 83 | check_request(request, expect, ignore) | 81 | check_request(request, expect, ignore) |
| 84 | 82 | ||
| 85 | write("testing HTTP redirection: ") | 83 | io.write("testing http redirection: ") |
| 86 | request = { | 84 | request = { |
| 87 | url = "http://" .. HOST .. prefix | 85 | url = "http://" .. HOST .. prefix |
| 88 | } | 86 | } |
| @@ -97,7 +95,7 @@ ignore = { | |||
| 97 | check_request(request, expect, ignore) | 95 | check_request(request, expect, ignore) |
| 98 | 96 | ||
| 99 | 97 | ||
| 100 | write("testing automatic auth failure: ") | 98 | io.write("testing automatic auth failure: ") |
| 101 | request = { | 99 | request = { |
| 102 | url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html" | 100 | url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html" |
| 103 | } | 101 | } |
| @@ -111,7 +109,7 @@ ignore = { | |||
| 111 | } | 109 | } |
| 112 | check_request(request, expect, ignore) | 110 | check_request(request, expect, ignore) |
| 113 | 111 | ||
| 114 | write("testing HTTP redirection failure: ") | 112 | io.write("testing http redirection failure: ") |
| 115 | request = { | 113 | request = { |
| 116 | url = "http://" .. HOST .. prefix, | 114 | url = "http://" .. HOST .. prefix, |
| 117 | stay = 1 | 115 | stay = 1 |
| @@ -126,29 +124,29 @@ ignore = { | |||
| 126 | } | 124 | } |
| 127 | check_request(request, expect, ignore) | 125 | check_request(request, expect, ignore) |
| 128 | 126 | ||
| 129 | write("testing host not found: ") | 127 | io.write("testing host not found: ") |
| 130 | request = { | 128 | request = { |
| 131 | url = "http://wronghost/does/not/exist" | 129 | url = "http://wronghost/does/not/exist" |
| 132 | } | 130 | } |
| 133 | local c, e = connect("wronghost", 80) | 131 | local c, e = socket.connect("wronghost", 80) |
| 134 | expect = { | 132 | expect = { |
| 135 | error = e | 133 | error = e |
| 136 | } | 134 | } |
| 137 | ignore = {} | 135 | ignore = {} |
| 138 | check_request(request, expect, ignore) | 136 | check_request(request, expect, ignore) |
| 139 | 137 | ||
| 140 | write("testing invalid url: ") | 138 | io.write("testing invalid url: ") |
| 141 | request = { | 139 | request = { |
| 142 | url = HOST .. prefix | 140 | url = HOST .. prefix |
| 143 | } | 141 | } |
| 144 | local c, e = connect("", 80) | 142 | local c, e = socket.connect("", 80) |
| 145 | expect = { | 143 | expect = { |
| 146 | error = e | 144 | error = e |
| 147 | } | 145 | } |
| 148 | ignore = {} | 146 | ignore = {} |
| 149 | check_request(request, expect, ignore) | 147 | check_request(request, expect, ignore) |
| 150 | 148 | ||
| 151 | write("testing document not found: ") | 149 | io.write("testing document not found: ") |
| 152 | request = { | 150 | request = { |
| 153 | url = "http://" .. HOST .. "/wrongdocument.html" | 151 | url = "http://" .. HOST .. "/wrongdocument.html" |
| 154 | } | 152 | } |
| @@ -162,7 +160,7 @@ ignore = { | |||
| 162 | } | 160 | } |
| 163 | check_request(request, expect, ignore) | 161 | check_request(request, expect, ignore) |
| 164 | 162 | ||
| 165 | write("testing auth failure: ") | 163 | io.write("testing auth failure: ") |
| 166 | request = { | 164 | request = { |
| 167 | url = "http://" .. HOST .. prefix .. "/auth/index.html" | 165 | url = "http://" .. HOST .. prefix .. "/auth/index.html" |
| 168 | } | 166 | } |
| @@ -176,7 +174,7 @@ ignore = { | |||
| 176 | } | 174 | } |
| 177 | check_request(request, expect, ignore) | 175 | check_request(request, expect, ignore) |
| 178 | 176 | ||
| 179 | write("testing manual basic auth: ") | 177 | io.write("testing manual basic auth: ") |
| 180 | request = { | 178 | request = { |
| 181 | url = "http://" .. HOST .. prefix .. "/auth/index.html", | 179 | url = "http://" .. HOST .. prefix .. "/auth/index.html", |
| 182 | headers = { | 180 | headers = { |
| @@ -193,7 +191,7 @@ ignore = { | |||
| 193 | } | 191 | } |
| 194 | check_request(request, expect, ignore) | 192 | check_request(request, expect, ignore) |
| 195 | 193 | ||
| 196 | write("testing automatic basic auth: ") | 194 | io.write("testing automatic basic auth: ") |
| 197 | request = { | 195 | request = { |
| 198 | url = "http://luasocket:password@" .. HOST .. prefix .. "/auth/index.html" | 196 | url = "http://luasocket:password@" .. HOST .. prefix .. "/auth/index.html" |
| 199 | } | 197 | } |
| @@ -207,7 +205,7 @@ ignore = { | |||
| 207 | } | 205 | } |
| 208 | check_request(request, expect, ignore) | 206 | check_request(request, expect, ignore) |
| 209 | 207 | ||
| 210 | write("testing auth info overriding: ") | 208 | io.write("testing auth info overriding: ") |
| 211 | request = { | 209 | request = { |
| 212 | url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html", | 210 | url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html", |
| 213 | user = "luasocket", | 211 | user = "luasocket", |
| @@ -223,7 +221,7 @@ ignore = { | |||
| 223 | } | 221 | } |
| 224 | check_request(request, expect, ignore) | 222 | check_request(request, expect, ignore) |
| 225 | 223 | ||
| 226 | write("testing cgi output retrieval (probably chunked...): ") | 224 | io.write("testing cgi output retrieval (probably chunked...): ") |
| 227 | request = { | 225 | request = { |
| 228 | url = "http://" .. HOST .. cgiprefix .. "/cat-index-html" | 226 | url = "http://" .. HOST .. cgiprefix .. "/cat-index-html" |
| 229 | } | 227 | } |
| @@ -237,7 +235,7 @@ ignore = { | |||
| 237 | } | 235 | } |
| 238 | check_request(request, expect, ignore) | 236 | check_request(request, expect, ignore) |
| 239 | 237 | ||
| 240 | write("testing redirect loop: ") | 238 | io.write("testing redirect loop: ") |
| 241 | request = { | 239 | request = { |
| 242 | url = "http://" .. HOST .. cgiprefix .. "/redirect-loop" | 240 | url = "http://" .. HOST .. cgiprefix .. "/redirect-loop" |
| 243 | } | 241 | } |
| @@ -251,7 +249,7 @@ ignore = { | |||
| 251 | } | 249 | } |
| 252 | check_request(request, expect, ignore) | 250 | check_request(request, expect, ignore) |
| 253 | 251 | ||
| 254 | write("testing post method: ") | 252 | io.write("testing post method: ") |
| 255 | request = { | 253 | request = { |
| 256 | url = "http://" .. HOST .. cgiprefix .. "/cat", | 254 | url = "http://" .. HOST .. cgiprefix .. "/cat", |
| 257 | method = "POST", | 255 | method = "POST", |
| @@ -267,7 +265,7 @@ ignore = { | |||
| 267 | } | 265 | } |
| 268 | check_request(request, expect, ignore) | 266 | check_request(request, expect, ignore) |
| 269 | 267 | ||
| 270 | write("testing wrong scheme: ") | 268 | io.write("testing wrong scheme: ") |
| 271 | request = { | 269 | request = { |
| 272 | url = "wrong://" .. HOST .. cgiprefix .. "/cat", | 270 | url = "wrong://" .. HOST .. cgiprefix .. "/cat", |
| 273 | method = "GET" | 271 | method = "GET" |
| @@ -280,31 +278,31 @@ ignore = { | |||
| 280 | check_request(request, expect, ignore) | 278 | check_request(request, expect, ignore) |
| 281 | 279 | ||
| 282 | local body | 280 | local body |
| 283 | write("testing simple get function: ") | 281 | io.write("testing simple get function: ") |
| 284 | body = HTTP.get("http://" .. HOST .. prefix .. "/index.html") | 282 | body = http.get("http://" .. HOST .. prefix .. "/index.html") |
| 285 | check(body == index) | 283 | check(body == index) |
| 286 | 284 | ||
| 287 | write("testing simple get function with table args: ") | 285 | io.write("testing simple get function with table args: ") |
| 288 | body = HTTP.get { | 286 | body = http.get { |
| 289 | url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html", | 287 | url = "http://really:wrong@" .. HOST .. prefix .. "/auth/index.html", |
| 290 | user = "luasocket", | 288 | user = "luasocket", |
| 291 | password = "password" | 289 | password = "password" |
| 292 | } | 290 | } |
| 293 | check(body == index) | 291 | check(body == index) |
| 294 | 292 | ||
| 295 | write("testing simple post function: ") | 293 | io.write("testing simple post function: ") |
| 296 | body = HTTP.post("http://" .. HOST .. cgiprefix .. "/cat", index) | 294 | body = http.post("http://" .. HOST .. cgiprefix .. "/cat", index) |
| 297 | check(body == index) | 295 | check(body == index) |
| 298 | 296 | ||
| 299 | write("testing simple post function with table args: ") | 297 | io.write("testing simple post function with table args: ") |
| 300 | body = HTTP.post { | 298 | body = http.post { |
| 301 | url = "http://" .. HOST .. cgiprefix .. "/cat", | 299 | url = "http://" .. HOST .. cgiprefix .. "/cat", |
| 302 | body = index | 300 | body = index |
| 303 | } | 301 | } |
| 304 | check(body == index) | 302 | check(body == index) |
| 305 | 303 | ||
| 306 | write("testing HEAD method: ") | 304 | io.write("testing HEAD method: ") |
| 307 | response = HTTP.request { | 305 | response = http.request { |
| 308 | method = "HEAD", | 306 | method = "HEAD", |
| 309 | url = "http://www.tecgraf.puc-rio.br/~diego/" | 307 | url = "http://www.tecgraf.puc-rio.br/~diego/" |
| 310 | } | 308 | } |
| @@ -312,4 +310,4 @@ check(response and response.headers) | |||
| 312 | 310 | ||
| 313 | print("passed all tests") | 311 | print("passed all tests") |
| 314 | 312 | ||
| 315 | print(format("done in %.2fs", _time() - t)) | 313 | print(string.format("done in %.2fs", socket._time() - t)) |
