diff options
Diffstat (limited to 'test/httptest.lua')
-rw-r--r-- | test/httptest.lua | 188 |
1 files changed, 94 insertions, 94 deletions
diff --git a/test/httptest.lua b/test/httptest.lua index 9d50a14..614acf3 100644 --- a/test/httptest.lua +++ b/test/httptest.lua | |||
@@ -34,28 +34,28 @@ index_file = "index.html" | |||
34 | index = readfile(index_file) | 34 | index = readfile(index_file) |
35 | 35 | ||
36 | local check_result = function(response, expect, ignore) | 36 | local check_result = function(response, expect, ignore) |
37 | for i,v in pairs(response) do | 37 | for i,v in pairs(response) do |
38 | if not ignore[i] then | 38 | if not ignore[i] then |
39 | if v ~= expect[i] then | 39 | if v ~= expect[i] then |
40 | local f = io.open("err", "w") | 40 | local f = io.open("err", "w") |
41 | f:write(tostring(v), "\n\n versus\n\n", tostring(expect[i])) | 41 | f:write(tostring(v), "\n\n versus\n\n", tostring(expect[i])) |
42 | f:close() | 42 | f:close() |
43 | fail(i .. " differs!") | 43 | fail(i .. " differs!") |
44 | end | 44 | end |
45 | end | 45 | end |
46 | end | 46 | end |
47 | for i,v in pairs(expect) do | 47 | for i,v in pairs(expect) do |
48 | if not ignore[i] then | 48 | if not ignore[i] then |
49 | if v ~= response[i] then | 49 | if v ~= response[i] then |
50 | local f = io.open("err", "w") | 50 | local f = io.open("err", "w") |
51 | f:write(tostring(response[i]), "\n\n versus\n\n", tostring(v)) | 51 | f:write(tostring(response[i]), "\n\n versus\n\n", tostring(v)) |
52 | v = string.sub(type(v) == "string" and v or "", 1, 70) | 52 | v = string.sub(type(v) == "string" and v or "", 1, 70) |
53 | f:close() | 53 | f:close() |
54 | fail(i .. " differs!") | 54 | fail(i .. " differs!") |
55 | end | 55 | end |
56 | end | 56 | end |
57 | end | 57 | end |
58 | print("ok") | 58 | print("ok") |
59 | end | 59 | end |
60 | 60 | ||
61 | local check_request = function(request, expect, ignore) | 61 | local check_request = function(request, expect, ignore) |
@@ -63,7 +63,7 @@ local check_request = function(request, expect, ignore) | |||
63 | if not request.sink then request.sink, t = ltn12.sink.table() end | 63 | if not request.sink then request.sink, t = ltn12.sink.table() end |
64 | request.source = request.source or | 64 | request.source = request.source or |
65 | (request.body and ltn12.source.string(request.body)) | 65 | (request.body and ltn12.source.string(request.body)) |
66 | local response = {} | 66 | local response = {} |
67 | response.code, response.headers, response.status = | 67 | response.code, response.headers, response.status = |
68 | socket.skip(1, http.request(request)) | 68 | socket.skip(1, http.request(request)) |
69 | if t and table.getn(t) > 0 then response.body = table.concat(t) end | 69 | if t and table.getn(t) > 0 then response.body = table.concat(t) end |
@@ -90,15 +90,15 @@ else fail("failed!") end | |||
90 | ------------------------------------------------------------------------ | 90 | ------------------------------------------------------------------------ |
91 | io.write("testing document retrieval: ") | 91 | io.write("testing document retrieval: ") |
92 | request = { | 92 | request = { |
93 | url = "http://" .. host .. prefix .. "/index.html" | 93 | url = "http://" .. host .. prefix .. "/index.html" |
94 | } | 94 | } |
95 | expect = { | 95 | expect = { |
96 | body = index, | 96 | body = index, |
97 | code = 200 | 97 | code = 200 |
98 | } | 98 | } |
99 | ignore = { | 99 | ignore = { |
100 | status = 1, | 100 | status = 1, |
101 | headers = 1 | 101 | headers = 1 |
102 | } | 102 | } |
103 | check_request(request, expect, ignore) | 103 | check_request(request, expect, ignore) |
104 | 104 | ||
@@ -111,9 +111,9 @@ expect = { | |||
111 | code = 302 | 111 | code = 302 |
112 | } | 112 | } |
113 | ignore = { | 113 | ignore = { |
114 | status = 1, | 114 | status = 1, |
115 | headers = 1, | 115 | headers = 1, |
116 | body = 1 | 116 | body = 1 |
117 | } | 117 | } |
118 | check_request(request, expect, ignore) | 118 | check_request(request, expect, ignore) |
119 | 119 | ||
@@ -144,19 +144,19 @@ check_request(request, expect, ignore) | |||
144 | io.write("testing post method: ") | 144 | io.write("testing post method: ") |
145 | -- wanted to test chunked post, but apache doesn't support it... | 145 | -- wanted to test chunked post, but apache doesn't support it... |
146 | request = { | 146 | request = { |
147 | url = "http://" .. host .. cgiprefix .. "/cat", | 147 | url = "http://" .. host .. cgiprefix .. "/cat", |
148 | method = "POST", | 148 | method = "POST", |
149 | body = index, | 149 | body = index, |
150 | -- remove content-length header to send chunked body | 150 | -- remove content-length header to send chunked body |
151 | headers = { ["content-length"] = string.len(index) } | 151 | headers = { ["content-length"] = string.len(index) } |
152 | } | 152 | } |
153 | expect = { | 153 | expect = { |
154 | body = index, | 154 | body = index, |
155 | code = 200 | 155 | code = 200 |
156 | } | 156 | } |
157 | ignore = { | 157 | ignore = { |
158 | status = 1, | 158 | status = 1, |
159 | headers = 1 | 159 | headers = 1 |
160 | } | 160 | } |
161 | check_request(request, expect, ignore) | 161 | check_request(request, expect, ignore) |
162 | 162 | ||
@@ -164,19 +164,19 @@ check_request(request, expect, ignore) | |||
164 | --[[ | 164 | --[[ |
165 | io.write("testing proxy with post method: ") | 165 | io.write("testing proxy with post method: ") |
166 | request = { | 166 | request = { |
167 | url = "http://" .. host .. cgiprefix .. "/cat", | 167 | url = "http://" .. host .. cgiprefix .. "/cat", |
168 | method = "POST", | 168 | method = "POST", |
169 | body = index, | 169 | body = index, |
170 | headers = { ["content-length"] = string.len(index) }, | 170 | headers = { ["content-length"] = string.len(index) }, |
171 | proxy= proxy | 171 | proxy= proxy |
172 | } | 172 | } |
173 | expect = { | 173 | expect = { |
174 | body = index, | 174 | body = index, |
175 | code = 200 | 175 | code = 200 |
176 | } | 176 | } |
177 | ignore = { | 177 | ignore = { |
178 | status = 1, | 178 | status = 1, |
179 | headers = 1 | 179 | headers = 1 |
180 | } | 180 | } |
181 | check_request(request, expect, ignore) | 181 | check_request(request, expect, ignore) |
182 | ]] | 182 | ]] |
@@ -190,18 +190,18 @@ print("ok") | |||
190 | ------------------------------------------------------------------------ | 190 | ------------------------------------------------------------------------ |
191 | io.write("testing ltn12.(sink|source).file: ") | 191 | io.write("testing ltn12.(sink|source).file: ") |
192 | request = { | 192 | request = { |
193 | url = "http://" .. host .. cgiprefix .. "/cat", | 193 | url = "http://" .. host .. cgiprefix .. "/cat", |
194 | method = "POST", | 194 | method = "POST", |
195 | source = ltn12.source.file(io.open(index_file, "rb")), | 195 | source = ltn12.source.file(io.open(index_file, "rb")), |
196 | sink = ltn12.sink.file(io.open(index_file .. "-back", "wb")), | 196 | sink = ltn12.sink.file(io.open(index_file .. "-back", "wb")), |
197 | headers = { ["content-length"] = string.len(index) } | 197 | headers = { ["content-length"] = string.len(index) } |
198 | } | 198 | } |
199 | expect = { | 199 | expect = { |
200 | code = 200 | 200 | code = 200 |
201 | } | 201 | } |
202 | ignore = { | 202 | ignore = { |
203 | status = 1, | 203 | status = 1, |
204 | headers = 1 | 204 | headers = 1 |
205 | } | 205 | } |
206 | check_request(request, expect, ignore) | 206 | check_request(request, expect, ignore) |
207 | back = readfile(index_file .. "-back") | 207 | back = readfile(index_file .. "-back") |
@@ -231,19 +231,19 @@ local sink = ltn12.sink.chain( | |||
231 | ) | 231 | ) |
232 | 232 | ||
233 | request = { | 233 | request = { |
234 | url = "http://" .. host .. cgiprefix .. "/cat", | 234 | url = "http://" .. host .. cgiprefix .. "/cat", |
235 | method = "POST", | 235 | method = "POST", |
236 | source = source, | 236 | source = source, |
237 | sink = sink, | 237 | sink = sink, |
238 | headers = { ["content-length"] = b64length(string.len(index)) } | 238 | headers = { ["content-length"] = b64length(string.len(index)) } |
239 | } | 239 | } |
240 | expect = { | 240 | expect = { |
241 | code = 200 | 241 | code = 200 |
242 | } | 242 | } |
243 | ignore = { | 243 | ignore = { |
244 | body_cb = 1, | 244 | body_cb = 1, |
245 | status = 1, | 245 | status = 1, |
246 | headers = 1 | 246 | headers = 1 |
247 | } | 247 | } |
248 | check_request(request, expect, ignore) | 248 | check_request(request, expect, ignore) |
249 | back = readfile(index_file .. "-back") | 249 | back = readfile(index_file .. "-back") |
@@ -253,15 +253,15 @@ os.remove(index_file .. "-back") | |||
253 | ------------------------------------------------------------------------ | 253 | ------------------------------------------------------------------------ |
254 | io.write("testing http redirection: ") | 254 | io.write("testing http redirection: ") |
255 | request = { | 255 | request = { |
256 | url = "http://" .. host .. prefix | 256 | url = "http://" .. host .. prefix |
257 | } | 257 | } |
258 | expect = { | 258 | expect = { |
259 | body = index, | 259 | body = index, |
260 | code = 200 | 260 | code = 200 |
261 | } | 261 | } |
262 | ignore = { | 262 | ignore = { |
263 | status = 1, | 263 | status = 1, |
264 | headers = 1 | 264 | headers = 1 |
265 | } | 265 | } |
266 | check_request(request, expect, ignore) | 266 | check_request(request, expect, ignore) |
267 | 267 | ||
@@ -269,16 +269,16 @@ check_request(request, expect, ignore) | |||
269 | --[[ | 269 | --[[ |
270 | io.write("testing proxy with redirection: ") | 270 | io.write("testing proxy with redirection: ") |
271 | request = { | 271 | request = { |
272 | url = "http://" .. host .. prefix, | 272 | url = "http://" .. host .. prefix, |
273 | proxy = proxy | 273 | proxy = proxy |
274 | } | 274 | } |
275 | expect = { | 275 | expect = { |
276 | body = index, | 276 | body = index, |
277 | code = 200 | 277 | code = 200 |
278 | } | 278 | } |
279 | ignore = { | 279 | ignore = { |
280 | status = 1, | 280 | status = 1, |
281 | headers = 1 | 281 | headers = 1 |
282 | } | 282 | } |
283 | check_request(request, expect, ignore) | 283 | check_request(request, expect, ignore) |
284 | ]] | 284 | ]] |
@@ -293,104 +293,104 @@ expect = { | |||
293 | } | 293 | } |
294 | ignore = { | 294 | ignore = { |
295 | body = 1, | 295 | body = 1, |
296 | status = 1, | 296 | status = 1, |
297 | headers = 1 | 297 | headers = 1 |
298 | } | 298 | } |
299 | check_request(request, expect, ignore) | 299 | check_request(request, expect, ignore) |
300 | 300 | ||
301 | ------------------------------------------------------------------------ | 301 | ------------------------------------------------------------------------ |
302 | io.write("testing http redirection failure: ") | 302 | io.write("testing http redirection failure: ") |
303 | request = { | 303 | request = { |
304 | url = "http://" .. host .. prefix, | 304 | url = "http://" .. host .. prefix, |
305 | redirect = false | 305 | redirect = false |
306 | } | 306 | } |
307 | expect = { | 307 | expect = { |
308 | code = 301 | 308 | code = 301 |
309 | } | 309 | } |
310 | ignore = { | 310 | ignore = { |
311 | body = 1, | 311 | body = 1, |
312 | status = 1, | 312 | status = 1, |
313 | headers = 1 | 313 | headers = 1 |
314 | } | 314 | } |
315 | check_request(request, expect, ignore) | 315 | check_request(request, expect, ignore) |
316 | 316 | ||
317 | ------------------------------------------------------------------------ | 317 | ------------------------------------------------------------------------ |
318 | io.write("testing document not found: ") | 318 | io.write("testing document not found: ") |
319 | request = { | 319 | request = { |
320 | url = "http://" .. host .. "/wrongdocument.html" | 320 | url = "http://" .. host .. "/wrongdocument.html" |
321 | } | 321 | } |
322 | expect = { | 322 | expect = { |
323 | code = 404 | 323 | code = 404 |
324 | } | 324 | } |
325 | ignore = { | 325 | ignore = { |
326 | body = 1, | 326 | body = 1, |
327 | status = 1, | 327 | status = 1, |
328 | headers = 1 | 328 | headers = 1 |
329 | } | 329 | } |
330 | check_request(request, expect, ignore) | 330 | check_request(request, expect, ignore) |
331 | 331 | ||
332 | ------------------------------------------------------------------------ | 332 | ------------------------------------------------------------------------ |
333 | io.write("testing auth failure: ") | 333 | io.write("testing auth failure: ") |
334 | request = { | 334 | request = { |
335 | url = "http://" .. host .. prefix .. "/auth/index.html" | 335 | url = "http://" .. host .. prefix .. "/auth/index.html" |
336 | } | 336 | } |
337 | expect = { | 337 | expect = { |
338 | code = 401 | 338 | code = 401 |
339 | } | 339 | } |
340 | ignore = { | 340 | ignore = { |
341 | body = 1, | 341 | body = 1, |
342 | status = 1, | 342 | status = 1, |
343 | headers = 1 | 343 | headers = 1 |
344 | } | 344 | } |
345 | check_request(request, expect, ignore) | 345 | check_request(request, expect, ignore) |
346 | 346 | ||
347 | ------------------------------------------------------------------------ | 347 | ------------------------------------------------------------------------ |
348 | io.write("testing manual basic auth: ") | 348 | io.write("testing manual basic auth: ") |
349 | request = { | 349 | request = { |
350 | url = "http://" .. host .. prefix .. "/auth/index.html", | 350 | url = "http://" .. host .. prefix .. "/auth/index.html", |
351 | headers = { | 351 | headers = { |
352 | authorization = "Basic " .. (mime.b64("luasocket:password")) | 352 | authorization = "Basic " .. (mime.b64("luasocket:password")) |
353 | } | 353 | } |
354 | } | 354 | } |
355 | expect = { | 355 | expect = { |
356 | code = 200, | 356 | code = 200, |
357 | body = index | 357 | body = index |
358 | } | 358 | } |
359 | ignore = { | 359 | ignore = { |
360 | status = 1, | 360 | status = 1, |
361 | headers = 1 | 361 | headers = 1 |
362 | } | 362 | } |
363 | check_request(request, expect, ignore) | 363 | check_request(request, expect, ignore) |
364 | 364 | ||
365 | ------------------------------------------------------------------------ | 365 | ------------------------------------------------------------------------ |
366 | io.write("testing automatic basic auth: ") | 366 | io.write("testing automatic basic auth: ") |
367 | request = { | 367 | request = { |
368 | url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html" | 368 | url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html" |
369 | } | 369 | } |
370 | expect = { | 370 | expect = { |
371 | code = 200, | 371 | code = 200, |
372 | body = index | 372 | body = index |
373 | } | 373 | } |
374 | ignore = { | 374 | ignore = { |
375 | status = 1, | 375 | status = 1, |
376 | headers = 1 | 376 | headers = 1 |
377 | } | 377 | } |
378 | check_request(request, expect, ignore) | 378 | check_request(request, expect, ignore) |
379 | 379 | ||
380 | ------------------------------------------------------------------------ | 380 | ------------------------------------------------------------------------ |
381 | io.write("testing auth info overriding: ") | 381 | io.write("testing auth info overriding: ") |
382 | request = { | 382 | request = { |
383 | url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html", | 383 | url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html", |
384 | user = "luasocket", | 384 | user = "luasocket", |
385 | password = "password" | 385 | password = "password" |
386 | } | 386 | } |
387 | expect = { | 387 | expect = { |
388 | code = 200, | 388 | code = 200, |
389 | body = index | 389 | body = index |
390 | } | 390 | } |
391 | ignore = { | 391 | ignore = { |
392 | status = 1, | 392 | status = 1, |
393 | headers = 1 | 393 | headers = 1 |
394 | } | 394 | } |
395 | check_request(request, expect, ignore) | 395 | check_request(request, expect, ignore) |
396 | 396 | ||
@@ -400,12 +400,12 @@ request = { | |||
400 | url = "http://" .. host .. cgiprefix .. "/cat-index-html" | 400 | url = "http://" .. host .. cgiprefix .. "/cat-index-html" |
401 | } | 401 | } |
402 | expect = { | 402 | expect = { |
403 | body = index, | 403 | body = index, |
404 | code = 200 | 404 | code = 200 |
405 | } | 405 | } |
406 | ignore = { | 406 | ignore = { |
407 | status = 1, | 407 | status = 1, |
408 | headers = 1 | 408 | headers = 1 |
409 | } | 409 | } |
410 | check_request(request, expect, ignore) | 410 | check_request(request, expect, ignore) |
411 | 411 | ||