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 /test/httptest.lua | |
| parent | b319e6a1e86a54c23b4848c5be7c0670f349a63f (diff) | |
| download | luasocket-d36460a249261680a4a920f05767b7f7cf2868ba.tar.gz luasocket-d36460a249261680a4a920f05767b7f7cf2868ba.tar.bz2 luasocket-d36460a249261680a4a920f05767b7f7cf2868ba.zip | |
updated for luasocket 1.4
Diffstat (limited to 'test/httptest.lua')
| -rw-r--r-- | test/httptest.lua | 368 |
1 files changed, 296 insertions, 72 deletions
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 | ||
