aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-21 01:09:50 +0000
committerDiego Nehab <diego@tecgraf.puc-rio.br>2004-01-21 01:09:50 +0000
commit3a7ac1e04361e12ddfcbf344e9e1db82fb88157b (patch)
tree166d452836a474541e1054f77b0b586bbe68dd6e /test
parent0b61b577f5d65a9c8bd5e690c4010c1e28b70e66 (diff)
downloadluasocket-3a7ac1e04361e12ddfcbf344e9e1db82fb88157b.tar.gz
luasocket-3a7ac1e04361e12ddfcbf344e9e1db82fb88157b.tar.bz2
luasocket-3a7ac1e04361e12ddfcbf344e9e1db82fb88157b.zip
Changed the naming convention of the mime module.
Looks beautiful.
Diffstat (limited to 'test')
-rw-r--r--test/httptest.lua124
-rw-r--r--test/mimetest.lua30
2 files changed, 129 insertions, 25 deletions
diff --git a/test/httptest.lua b/test/httptest.lua
index 44266d5..4667605 100644
--- a/test/httptest.lua
+++ b/test/httptest.lua
@@ -5,8 +5,8 @@
5-- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth 5-- needs "AllowOverride AuthConfig" on /home/c/diego/tec/luasocket/test/auth
6dofile("noglobals.lua") 6dofile("noglobals.lua")
7 7
8local host, proxy, request, response 8local host, proxy, request, response, index_file
9local ignore, expect, index, prefix, cgiprefix 9local ignore, expect, index, prefix, cgiprefix, index_crlf
10 10
11socket.http.TIMEOUT = 5 11socket.http.TIMEOUT = 5
12 12
@@ -16,6 +16,7 @@ host = host or "diego.student.dyn.cs.princeton.edu"
16proxy = proxy or "http://localhost:3128" 16proxy = proxy or "http://localhost:3128"
17prefix = prefix or "/luasocket-test" 17prefix = prefix or "/luasocket-test"
18cgiprefix = cgiprefix or "/luasocket-test-cgi" 18cgiprefix = cgiprefix or "/luasocket-test-cgi"
19index_file = "test/index.html"
19 20
20local readfile = function(name) 21local readfile = function(name)
21 local f = io.open(name, "r") 22 local f = io.open(name, "r")
@@ -25,7 +26,8 @@ local readfile = function(name)
25 return s 26 return s
26end 27end
27 28
28index = readfile("test/index.html") 29-- read index with CRLF convention
30index = readfile(index_file)
29 31
30local similar = function(s1, s2) 32local similar = function(s1, s2)
31 return string.lower(string.gsub(s1 or "", "%s", "")) == 33 return string.lower(string.gsub(s1 or "", "%s", "")) ==
@@ -42,13 +44,13 @@ local check = function (v, e)
42 if v then print("ok") 44 if v then print("ok")
43 else fail(e) end 45 else fail(e) end
44end 46end
45 47
46local check_request = function(request, expect, ignore) 48local check_result = function(response, expect, ignore)
47 local response = socket.http.request(request)
48 for i,v in response do 49 for i,v in response do
49 if not ignore[i] then 50 if not ignore[i] then
50 if v ~= expect[i] then 51 if v ~= expect[i] then
51 if string.len(v) < 80 then print(v) end 52 v = string.sub(type(v) == "string" and v or "", 1, 70)
53 print(v)
52 fail(i .. " differs!") 54 fail(i .. " differs!")
53 end 55 end
54 end 56 end
@@ -56,7 +58,8 @@ local check_request = function(request, expect, ignore)
56 for i,v in expect do 58 for i,v in expect do
57 if not ignore[i] then 59 if not ignore[i] then
58 if v ~= response[i] then 60 if v ~= response[i] then
59 if string.len(v) < 80 then print(v) end 61 v = string.sub(type(v) == "string" and v or "", 1, 70)
62 print(v)
60 fail(i .. " differs!") 63 fail(i .. " differs!")
61 end 64 end
62 end 65 end
@@ -64,6 +67,17 @@ local check_request = function(request, expect, ignore)
64 print("ok") 67 print("ok")
65end 68end
66 69
70local check_request = function(request, expect, ignore)
71 local response = socket.http.request(request)
72 check_result(response, expect, ignore)
73end
74
75local check_request_cb = function(request, response, expect, ignore)
76 local response = socket.http.request_cb(request, response)
77 check_result(response, expect, ignore)
78end
79
80------------------------------------------------------------------------
67io.write("testing request uri correctness: ") 81io.write("testing request uri correctness: ")
68local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string" 82local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
69local back, h, c, e = socket.http.get("http://" .. host .. forth) 83local back, h, c, e = socket.http.get("http://" .. host .. forth)
@@ -72,12 +86,15 @@ back = socket.url.parse(back)
72if similar(back.query, "this+is+the+query+string") then print("ok") 86if similar(back.query, "this+is+the+query+string") then print("ok")
73else fail() end 87else fail() end
74 88
89------------------------------------------------------------------------
75io.write("testing query string correctness: ") 90io.write("testing query string correctness: ")
76forth = "this+is+the+query+string" 91forth = "this+is+the+query+string"
77back = socket.http.get("http://" .. host .. cgiprefix .. "/query-string?" .. forth) 92back = socket.http.get("http://" .. host .. cgiprefix ..
93 "/query-string?" .. forth)
78if similar(back, forth) then print("ok") 94if similar(back, forth) then print("ok")
79else fail("failed!") end 95else fail("failed!") end
80 96
97------------------------------------------------------------------------
81io.write("testing document retrieval: ") 98io.write("testing document retrieval: ")
82request = { 99request = {
83 url = "http://" .. host .. prefix .. "/index.html" 100 url = "http://" .. host .. prefix .. "/index.html"
@@ -92,6 +109,7 @@ ignore = {
92} 109}
93check_request(request, expect, ignore) 110check_request(request, expect, ignore)
94 111
112------------------------------------------------------------------------
95io.write("testing redirect loop: ") 113io.write("testing redirect loop: ")
96request = { 114request = {
97 url = "http://" .. host .. cgiprefix .. "/redirect-loop" 115 url = "http://" .. host .. cgiprefix .. "/redirect-loop"
@@ -106,6 +124,7 @@ ignore = {
106} 124}
107check_request(request, expect, ignore) 125check_request(request, expect, ignore)
108 126
127------------------------------------------------------------------------
109io.write("testing post method: ") 128io.write("testing post method: ")
110-- wanted to test chunked post, but apache doesn't support it... 129-- wanted to test chunked post, but apache doesn't support it...
111request = { 130request = {
@@ -125,6 +144,7 @@ ignore = {
125} 144}
126check_request(request, expect, ignore) 145check_request(request, expect, ignore)
127 146
147------------------------------------------------------------------------
128io.write("testing proxy with post method: ") 148io.write("testing proxy with post method: ")
129request = { 149request = {
130 url = "http://" .. host .. cgiprefix .. "/cat", 150 url = "http://" .. host .. cgiprefix .. "/cat",
@@ -143,10 +163,78 @@ ignore = {
143} 163}
144check_request(request, expect, ignore) 164check_request(request, expect, ignore)
145 165
166------------------------------------------------------------------------
146io.write("testing simple post function: ") 167io.write("testing simple post function: ")
147back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index) 168back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index)
148check(back == index) 169check(back == index)
149 170
171------------------------------------------------------------------------
172io.write("testing send.file and receive.file callbacks: ")
173request = {
174 url = "http://" .. host .. cgiprefix .. "/cat",
175 method = "POST",
176 body_cb = socket.callback.send.file(io.open(index_file, "r")),
177 headers = { ["content-length"] = string.len(index) }
178}
179response = {
180 body_cb = socket.callback.receive.file(io.open(index_file .. "-back", "w"))
181}
182expect = {
183 code = 200
184}
185ignore = {
186 body_cb = 1,
187 status = 1,
188 headers = 1
189}
190check_request_cb(request, response, expect, ignore)
191back = readfile(index_file .. "-back")
192check(back == index)
193os.remove(index_file .. "-back")
194
195------------------------------------------------------------------------
196io.write("testing send.chain and receive.chain callbacks: ")
197
198local function b64length(len)
199 local a = math.ceil(len/3)*4
200 local l = math.ceil(a/76)
201 return a + l*2
202end
203
204local req_cb = socket.callback.send.chain(
205 socket.callback.send.file(io.open(index_file, "r")),
206 socket.mime.chain(
207 socket.mime.encode("base64"),
208 socket.mime.wrap("base64")
209 )
210)
211
212local resp_cb = socket.callback.receive.chain(
213 socket.mime.decode("base64"),
214 socket.callback.receive.file(io.open(index_file .. "-back", "w"))
215)
216
217request = {
218 url = "http://" .. host .. cgiprefix .. "/cat",
219 method = "POST",
220 body_cb = req_cb,
221 headers = { ["content-length"] = b64length(string.len(index)) }
222}
223response = { body_cb = resp_cb }
224expect = {
225 code = 200
226}
227ignore = {
228 body_cb = 1,
229 status = 1,
230 headers = 1
231}
232check_request_cb(request, response, expect, ignore)
233back = readfile(index_file .. "-back")
234check(back == index)
235os.remove(index_file .. "-back")
236
237------------------------------------------------------------------------
150io.write("testing simple post function with table args: ") 238io.write("testing simple post function with table args: ")
151back = socket.http.post { 239back = socket.http.post {
152 url = "http://" .. host .. cgiprefix .. "/cat", 240 url = "http://" .. host .. cgiprefix .. "/cat",
@@ -154,6 +242,7 @@ back = socket.http.post {
154} 242}
155check(back == index) 243check(back == index)
156 244
245------------------------------------------------------------------------
157io.write("testing http redirection: ") 246io.write("testing http redirection: ")
158request = { 247request = {
159 url = "http://" .. host .. prefix 248 url = "http://" .. host .. prefix
@@ -168,6 +257,7 @@ ignore = {
168} 257}
169check_request(request, expect, ignore) 258check_request(request, expect, ignore)
170 259
260------------------------------------------------------------------------
171io.write("testing proxy with redirection: ") 261io.write("testing proxy with redirection: ")
172request = { 262request = {
173 url = "http://" .. host .. prefix, 263 url = "http://" .. host .. prefix,
@@ -183,7 +273,7 @@ ignore = {
183} 273}
184check_request(request, expect, ignore) 274check_request(request, expect, ignore)
185 275
186 276------------------------------------------------------------------------
187io.write("testing automatic auth failure: ") 277io.write("testing automatic auth failure: ")
188request = { 278request = {
189 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html" 279 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html"
@@ -198,6 +288,7 @@ ignore = {
198} 288}
199check_request(request, expect, ignore) 289check_request(request, expect, ignore)
200 290
291------------------------------------------------------------------------
201io.write("testing http redirection failure: ") 292io.write("testing http redirection failure: ")
202request = { 293request = {
203 url = "http://" .. host .. prefix, 294 url = "http://" .. host .. prefix,
@@ -213,6 +304,7 @@ ignore = {
213} 304}
214check_request(request, expect, ignore) 305check_request(request, expect, ignore)
215 306
307------------------------------------------------------------------------
216io.write("testing host not found: ") 308io.write("testing host not found: ")
217request = { 309request = {
218 url = "http://wronghost/does/not/exist" 310 url = "http://wronghost/does/not/exist"
@@ -224,6 +316,7 @@ expect = {
224ignore = {} 316ignore = {}
225check_request(request, expect, ignore) 317check_request(request, expect, ignore)
226 318
319------------------------------------------------------------------------
227io.write("testing invalid url: ") 320io.write("testing invalid url: ")
228request = { 321request = {
229 url = host .. prefix 322 url = host .. prefix
@@ -235,6 +328,7 @@ expect = {
235ignore = {} 328ignore = {}
236check_request(request, expect, ignore) 329check_request(request, expect, ignore)
237 330
331------------------------------------------------------------------------
238io.write("testing document not found: ") 332io.write("testing document not found: ")
239request = { 333request = {
240 url = "http://" .. host .. "/wrongdocument.html" 334 url = "http://" .. host .. "/wrongdocument.html"
@@ -249,6 +343,7 @@ ignore = {
249} 343}
250check_request(request, expect, ignore) 344check_request(request, expect, ignore)
251 345
346------------------------------------------------------------------------
252io.write("testing auth failure: ") 347io.write("testing auth failure: ")
253request = { 348request = {
254 url = "http://" .. host .. prefix .. "/auth/index.html" 349 url = "http://" .. host .. prefix .. "/auth/index.html"
@@ -263,6 +358,7 @@ ignore = {
263} 358}
264check_request(request, expect, ignore) 359check_request(request, expect, ignore)
265 360
361------------------------------------------------------------------------
266io.write("testing manual basic auth: ") 362io.write("testing manual basic auth: ")
267request = { 363request = {
268 url = "http://" .. host .. prefix .. "/auth/index.html", 364 url = "http://" .. host .. prefix .. "/auth/index.html",
@@ -280,6 +376,7 @@ ignore = {
280} 376}
281check_request(request, expect, ignore) 377check_request(request, expect, ignore)
282 378
379------------------------------------------------------------------------
283io.write("testing automatic basic auth: ") 380io.write("testing automatic basic auth: ")
284request = { 381request = {
285 url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html" 382 url = "http://luasocket:password@" .. host .. prefix .. "/auth/index.html"
@@ -294,6 +391,7 @@ ignore = {
294} 391}
295check_request(request, expect, ignore) 392check_request(request, expect, ignore)
296 393
394------------------------------------------------------------------------
297io.write("testing auth info overriding: ") 395io.write("testing auth info overriding: ")
298request = { 396request = {
299 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html", 397 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
@@ -310,6 +408,7 @@ ignore = {
310} 408}
311check_request(request, expect, ignore) 409check_request(request, expect, ignore)
312 410
411------------------------------------------------------------------------
313io.write("testing cgi output retrieval (probably chunked...): ") 412io.write("testing cgi output retrieval (probably chunked...): ")
314request = { 413request = {
315 url = "http://" .. host .. cgiprefix .. "/cat-index-html" 414 url = "http://" .. host .. cgiprefix .. "/cat-index-html"
@@ -324,6 +423,7 @@ ignore = {
324} 423}
325check_request(request, expect, ignore) 424check_request(request, expect, ignore)
326 425
426------------------------------------------------------------------------
327io.write("testing wrong scheme: ") 427io.write("testing wrong scheme: ")
328request = { 428request = {
329 url = "wrong://" .. host .. cgiprefix .. "/cat", 429 url = "wrong://" .. host .. cgiprefix .. "/cat",
@@ -336,11 +436,13 @@ ignore = {
336} 436}
337check_request(request, expect, ignore) 437check_request(request, expect, ignore)
338 438
439------------------------------------------------------------------------
339local body 440local body
340io.write("testing simple get function: ") 441io.write("testing simple get function: ")
341body = socket.http.get("http://" .. host .. prefix .. "/index.html") 442body = socket.http.get("http://" .. host .. prefix .. "/index.html")
342check(body == index) 443check(body == index)
343 444
445------------------------------------------------------------------------
344io.write("testing simple get function with table args: ") 446io.write("testing simple get function with table args: ")
345body = socket.http.get { 447body = socket.http.get {
346 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html", 448 url = "http://really:wrong@" .. host .. prefix .. "/auth/index.html",
@@ -349,6 +451,7 @@ body = socket.http.get {
349} 451}
350check(body == index) 452check(body == index)
351 453
454------------------------------------------------------------------------
352io.write("testing HEAD method: ") 455io.write("testing HEAD method: ")
353socket.http.TIMEOUT = 1 456socket.http.TIMEOUT = 1
354response = socket.http.request { 457response = socket.http.request {
@@ -357,6 +460,7 @@ response = socket.http.request {
357} 460}
358check(response and response.headers) 461check(response and response.headers)
359 462
463------------------------------------------------------------------------
360print("passed all tests") 464print("passed all tests")
361 465
362print(string.format("done in %.2fs", socket.time() - t)) 466print(string.format("done in %.2fs", socket.time() - t))
diff --git a/test/mimetest.lua b/test/mimetest.lua
index 5485db1..0adcb18 100644
--- a/test/mimetest.lua
+++ b/test/mimetest.lua
@@ -66,8 +66,8 @@ local function compare(input, output)
66end 66end
67 67
68local function encode_qptest(mode) 68local function encode_qptest(mode)
69 local encode = socket.mime.qprint.encode(mode) 69 local encode = socket.mime.encode("quoted-printable", mode)
70 local split = socket.mime.qprint.split() 70 local split = socket.mime.wrap("quoted-printable")
71 local chain = socket.mime.chain(encode, split) 71 local chain = socket.mime.chain(encode, split)
72 transform(qptest, eqptest, chain) 72 transform(qptest, eqptest, chain)
73end 73end
@@ -77,7 +77,7 @@ local function compare_qptest()
77end 77end
78 78
79local function decode_qptest() 79local function decode_qptest()
80 local decode = socket.mime.qprint.decode() 80 local decode = socket.mime.decode("quoted-printable")
81 transform(eqptest, dqptest, decode) 81 transform(eqptest, dqptest, decode)
82end 82end
83 83
@@ -151,23 +151,23 @@ local function cleanup_qptest()
151end 151end
152 152
153local function encode_b64test() 153local function encode_b64test()
154 local e1 = socket.mime.base64.encode() 154 local e1 = socket.mime.encode("base64")
155 local e2 = socket.mime.base64.encode() 155 local e2 = socket.mime.encode("base64")
156 local e3 = socket.mime.base64.encode() 156 local e3 = socket.mime.encode("base64")
157 local e4 = socket.mime.base64.encode() 157 local e4 = socket.mime.encode("base64")
158 local sp4 = socket.mime.split() 158 local sp4 = socket.mime.wrap("character")
159 local sp3 = socket.mime.split(59) 159 local sp3 = socket.mime.wrap("character", 59)
160 local sp2 = socket.mime.split(30) 160 local sp2 = socket.mime.wrap("character", 30)
161 local sp1 = socket.mime.split(27) 161 local sp1 = socket.mime.wrap("character", 27)
162 local chain = socket.mime.chain(e1, sp1, e2, sp2, e3, sp3, e4, sp4) 162 local chain = socket.mime.chain(e1, sp1, e2, sp2, e3, sp3, e4, sp4)
163 transform(b64test, eb64test, chain) 163 transform(b64test, eb64test, chain)
164end 164end
165 165
166local function decode_b64test() 166local function decode_b64test()
167 local d1 = socket.mime.base64.decode() 167 local d1 = socket.mime.decode("base64")
168 local d2 = socket.mime.base64.decode() 168 local d2 = socket.mime.decode("base64")
169 local d3 = socket.mime.base64.decode() 169 local d3 = socket.mime.decode("base64")
170 local d4 = socket.mime.base64.decode() 170 local d4 = socket.mime.decode("base64")
171 local chain = socket.mime.chain(d1, d2, d3, d4) 171 local chain = socket.mime.chain(d1, d2, d3, d4)
172 transform(eb64test, db64test, chain) 172 transform(eb64test, db64test, chain)
173end 173end