aboutsummaryrefslogtreecommitdiff
path: root/test/httptest.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/httptest.lua')
-rw-r--r--test/httptest.lua188
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"
34index = readfile(index_file) 34index = readfile(index_file)
35 35
36local check_result = function(response, expect, ignore) 36local 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")
59end 59end
60 60
61local check_request = function(request, expect, ignore) 61local 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------------------------------------------------------------------------
91io.write("testing document retrieval: ") 91io.write("testing document retrieval: ")
92request = { 92request = {
93 url = "http://" .. host .. prefix .. "/index.html" 93 url = "http://" .. host .. prefix .. "/index.html"
94} 94}
95expect = { 95expect = {
96 body = index, 96 body = index,
97 code = 200 97 code = 200
98} 98}
99ignore = { 99ignore = {
100 status = 1, 100 status = 1,
101 headers = 1 101 headers = 1
102} 102}
103check_request(request, expect, ignore) 103check_request(request, expect, ignore)
104 104
@@ -111,9 +111,9 @@ expect = {
111 code = 302 111 code = 302
112} 112}
113ignore = { 113ignore = {
114 status = 1, 114 status = 1,
115 headers = 1, 115 headers = 1,
116 body = 1 116 body = 1
117} 117}
118check_request(request, expect, ignore) 118check_request(request, expect, ignore)
119 119
@@ -144,19 +144,19 @@ check_request(request, expect, ignore)
144io.write("testing post method: ") 144io.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...
146request = { 146request = {
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}
153expect = { 153expect = {
154 body = index, 154 body = index,
155 code = 200 155 code = 200
156} 156}
157ignore = { 157ignore = {
158 status = 1, 158 status = 1,
159 headers = 1 159 headers = 1
160} 160}
161check_request(request, expect, ignore) 161check_request(request, expect, ignore)
162 162
@@ -164,19 +164,19 @@ check_request(request, expect, ignore)
164--[[ 164--[[
165io.write("testing proxy with post method: ") 165io.write("testing proxy with post method: ")
166request = { 166request = {
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}
173expect = { 173expect = {
174 body = index, 174 body = index,
175 code = 200 175 code = 200
176} 176}
177ignore = { 177ignore = {
178 status = 1, 178 status = 1,
179 headers = 1 179 headers = 1
180} 180}
181check_request(request, expect, ignore) 181check_request(request, expect, ignore)
182]] 182]]
@@ -190,18 +190,18 @@ print("ok")
190------------------------------------------------------------------------ 190------------------------------------------------------------------------
191io.write("testing ltn12.(sink|source).file: ") 191io.write("testing ltn12.(sink|source).file: ")
192request = { 192request = {
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}
199expect = { 199expect = {
200 code = 200 200 code = 200
201} 201}
202ignore = { 202ignore = {
203 status = 1, 203 status = 1,
204 headers = 1 204 headers = 1
205} 205}
206check_request(request, expect, ignore) 206check_request(request, expect, ignore)
207back = readfile(index_file .. "-back") 207back = readfile(index_file .. "-back")
@@ -231,19 +231,19 @@ local sink = ltn12.sink.chain(
231) 231)
232 232
233request = { 233request = {
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}
240expect = { 240expect = {
241 code = 200 241 code = 200
242} 242}
243ignore = { 243ignore = {
244 body_cb = 1, 244 body_cb = 1,
245 status = 1, 245 status = 1,
246 headers = 1 246 headers = 1
247} 247}
248check_request(request, expect, ignore) 248check_request(request, expect, ignore)
249back = readfile(index_file .. "-back") 249back = readfile(index_file .. "-back")
@@ -253,15 +253,15 @@ os.remove(index_file .. "-back")
253------------------------------------------------------------------------ 253------------------------------------------------------------------------
254io.write("testing http redirection: ") 254io.write("testing http redirection: ")
255request = { 255request = {
256 url = "http://" .. host .. prefix 256 url = "http://" .. host .. prefix
257} 257}
258expect = { 258expect = {
259 body = index, 259 body = index,
260 code = 200 260 code = 200
261} 261}
262ignore = { 262ignore = {
263 status = 1, 263 status = 1,
264 headers = 1 264 headers = 1
265} 265}
266check_request(request, expect, ignore) 266check_request(request, expect, ignore)
267 267
@@ -269,16 +269,16 @@ check_request(request, expect, ignore)
269--[[ 269--[[
270io.write("testing proxy with redirection: ") 270io.write("testing proxy with redirection: ")
271request = { 271request = {
272 url = "http://" .. host .. prefix, 272 url = "http://" .. host .. prefix,
273 proxy = proxy 273 proxy = proxy
274} 274}
275expect = { 275expect = {
276 body = index, 276 body = index,
277 code = 200 277 code = 200
278} 278}
279ignore = { 279ignore = {
280 status = 1, 280 status = 1,
281 headers = 1 281 headers = 1
282} 282}
283check_request(request, expect, ignore) 283check_request(request, expect, ignore)
284]] 284]]
@@ -293,104 +293,104 @@ expect = {
293} 293}
294ignore = { 294ignore = {
295 body = 1, 295 body = 1,
296 status = 1, 296 status = 1,
297 headers = 1 297 headers = 1
298} 298}
299check_request(request, expect, ignore) 299check_request(request, expect, ignore)
300 300
301------------------------------------------------------------------------ 301------------------------------------------------------------------------
302io.write("testing http redirection failure: ") 302io.write("testing http redirection failure: ")
303request = { 303request = {
304 url = "http://" .. host .. prefix, 304 url = "http://" .. host .. prefix,
305 redirect = false 305 redirect = false
306} 306}
307expect = { 307expect = {
308 code = 301 308 code = 301
309} 309}
310ignore = { 310ignore = {
311 body = 1, 311 body = 1,
312 status = 1, 312 status = 1,
313 headers = 1 313 headers = 1
314} 314}
315check_request(request, expect, ignore) 315check_request(request, expect, ignore)
316 316
317------------------------------------------------------------------------ 317------------------------------------------------------------------------
318io.write("testing document not found: ") 318io.write("testing document not found: ")
319request = { 319request = {
320 url = "http://" .. host .. "/wrongdocument.html" 320 url = "http://" .. host .. "/wrongdocument.html"
321} 321}
322expect = { 322expect = {
323 code = 404 323 code = 404
324} 324}
325ignore = { 325ignore = {
326 body = 1, 326 body = 1,
327 status = 1, 327 status = 1,
328 headers = 1 328 headers = 1
329} 329}
330check_request(request, expect, ignore) 330check_request(request, expect, ignore)
331 331
332------------------------------------------------------------------------ 332------------------------------------------------------------------------
333io.write("testing auth failure: ") 333io.write("testing auth failure: ")
334request = { 334request = {
335 url = "http://" .. host .. prefix .. "/auth/index.html" 335 url = "http://" .. host .. prefix .. "/auth/index.html"
336} 336}
337expect = { 337expect = {
338 code = 401 338 code = 401
339} 339}
340ignore = { 340ignore = {
341 body = 1, 341 body = 1,
342 status = 1, 342 status = 1,
343 headers = 1 343 headers = 1
344} 344}
345check_request(request, expect, ignore) 345check_request(request, expect, ignore)
346 346
347------------------------------------------------------------------------ 347------------------------------------------------------------------------
348io.write("testing manual basic auth: ") 348io.write("testing manual basic auth: ")
349request = { 349request = {
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}
355expect = { 355expect = {
356 code = 200, 356 code = 200,
357 body = index 357 body = index
358} 358}
359ignore = { 359ignore = {
360 status = 1, 360 status = 1,
361 headers = 1 361 headers = 1
362} 362}
363check_request(request, expect, ignore) 363check_request(request, expect, ignore)
364 364
365------------------------------------------------------------------------ 365------------------------------------------------------------------------
366io.write("testing automatic basic auth: ") 366io.write("testing automatic basic auth: ")
367request = { 367request = {
368 url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html" 368 url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html"
369} 369}
370expect = { 370expect = {
371 code = 200, 371 code = 200,
372 body = index 372 body = index
373} 373}
374ignore = { 374ignore = {
375 status = 1, 375 status = 1,
376 headers = 1 376 headers = 1
377} 377}
378check_request(request, expect, ignore) 378check_request(request, expect, ignore)
379 379
380------------------------------------------------------------------------ 380------------------------------------------------------------------------
381io.write("testing auth info overriding: ") 381io.write("testing auth info overriding: ")
382request = { 382request = {
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}
387expect = { 387expect = {
388 code = 200, 388 code = 200,
389 body = index 389 body = index
390} 390}
391ignore = { 391ignore = {
392 status = 1, 392 status = 1,
393 headers = 1 393 headers = 1
394} 394}
395check_request(request, expect, ignore) 395check_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}
402expect = { 402expect = {
403 body = index, 403 body = index,
404 code = 200 404 code = 200
405} 405}
406ignore = { 406ignore = {
407 status = 1, 407 status = 1,
408 headers = 1 408 headers = 1
409} 409}
410check_request(request, expect, ignore) 410check_request(request, expect, ignore)
411 411