aboutsummaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/httptest.lua36
-rw-r--r--test/smtptest.lua2
-rw-r--r--test/testsupport.lua37
-rw-r--r--test/urltest.lua2
4 files changed, 45 insertions, 32 deletions
diff --git a/test/httptest.lua b/test/httptest.lua
index ddeea50..4ec0cc1 100644
--- a/test/httptest.lua
+++ b/test/httptest.lua
@@ -3,7 +3,7 @@
3-- needs ScriptAlias from /home/c/diego/tec/luasocket/test/cgi 3-- needs ScriptAlias from /home/c/diego/tec/luasocket/test/cgi
4-- to "/luasocket-test-cgi" and "/luasocket-test-cgi/" 4-- to "/luasocket-test-cgi" and "/luasocket-test-cgi/"
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("testsupport.lua")
7 7
8local host, proxy, request, response, index_file 8local host, proxy, request, response, index_file
9local ignore, expect, index, prefix, cgiprefix, index_crlf 9local ignore, expect, index, prefix, cgiprefix, index_crlf
@@ -18,33 +18,9 @@ prefix = prefix or "/luasocket-test"
18cgiprefix = cgiprefix or "/luasocket-test-cgi" 18cgiprefix = cgiprefix or "/luasocket-test-cgi"
19index_file = "test/index.html" 19index_file = "test/index.html"
20 20
21local readfile = function(name)
22 local f = io.open(name, "r")
23 if not f then return nil end
24 local s = f:read("*a")
25 f:close()
26 return s
27end
28
29-- read index with CRLF convention 21-- read index with CRLF convention
30index = readfile(index_file) 22index = readfile(index_file)
31 23
32local similar = function(s1, s2)
33 return string.lower(string.gsub(s1 or "", "%s", "")) ==
34 string.lower(string.gsub(s2 or "", "%s", ""))
35end
36
37local fail = function(s)
38 s = s or "failed!"
39 print(s)
40 os.exit()
41end
42
43local check = function (v, e)
44 if v then print("ok")
45 else fail(e) end
46end
47
48local check_result = function(response, expect, ignore) 24local check_result = function(response, expect, ignore)
49 for i,v in response do 25 for i,v in response do
50 if not ignore[i] then 26 if not ignore[i] then
@@ -171,7 +147,7 @@ check_request(request, expect, ignore)
171------------------------------------------------------------------------ 147------------------------------------------------------------------------
172io.write("testing simple post function: ") 148io.write("testing simple post function: ")
173back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index) 149back = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index)
174check(back == index) 150assert(back == index)
175 151
176------------------------------------------------------------------------ 152------------------------------------------------------------------------
177io.write("testing ltn12.(sink|source).file: ") 153io.write("testing ltn12.(sink|source).file: ")
@@ -191,7 +167,7 @@ ignore = {
191} 167}
192check_request(request, expect, ignore) 168check_request(request, expect, ignore)
193back = readfile(index_file .. "-back") 169back = readfile(index_file .. "-back")
194check(back == index) 170assert(back == index)
195os.remove(index_file .. "-back") 171os.remove(index_file .. "-back")
196 172
197------------------------------------------------------------------------ 173------------------------------------------------------------------------
@@ -233,7 +209,7 @@ ignore = {
233} 209}
234check_request(request, expect, ignore) 210check_request(request, expect, ignore)
235back = readfile(index_file .. "-back") 211back = readfile(index_file .. "-back")
236check(back == index) 212assert(back == index)
237os.remove(index_file .. "-back") 213os.remove(index_file .. "-back")
238 214
239------------------------------------------------------------------------ 215------------------------------------------------------------------------
@@ -434,7 +410,7 @@ check_request(request, expect, ignore)
434local body 410local body
435io.write("testing simple get function: ") 411io.write("testing simple get function: ")
436body = socket.http.get("http://" .. host .. prefix .. "/index.html") 412body = socket.http.get("http://" .. host .. prefix .. "/index.html")
437check(body == index) 413assert(body == index)
438 414
439------------------------------------------------------------------------ 415------------------------------------------------------------------------
440io.write("testing HEAD method: ") 416io.write("testing HEAD method: ")
@@ -443,7 +419,7 @@ response = socket.http.request {
443 method = "HEAD", 419 method = "HEAD",
444 url = "http://www.cs.princeton.edu/~diego/" 420 url = "http://www.cs.princeton.edu/~diego/"
445} 421}
446check(response and response.headers) 422assert(response and response.headers)
447 423
448------------------------------------------------------------------------ 424------------------------------------------------------------------------
449print("passed all tests") 425print("passed all tests")
diff --git a/test/smtptest.lua b/test/smtptest.lua
index 8468408..e812737 100644
--- a/test/smtptest.lua
+++ b/test/smtptest.lua
@@ -16,7 +16,7 @@ local err
16 16
17dofile("mbox.lua") 17dofile("mbox.lua")
18local parse = mbox.parse 18local parse = mbox.parse
19dofile("noglobals.lua") 19dofile("testsupport.lua")
20 20
21local total = function() 21local total = function()
22 local t = 0 22 local t = 0
diff --git a/test/testsupport.lua b/test/testsupport.lua
new file mode 100644
index 0000000..ca3cd95
--- /dev/null
+++ b/test/testsupport.lua
@@ -0,0 +1,37 @@
1function readfile(name)
2 local f = io.open(name, "r")
3 if not f then return nil end
4 local s = f:read("*a")
5 f:close()
6 return s
7end
8
9function similar(s1, s2)
10 return string.lower(string.gsub(s1 or "", "%s", "")) ==
11 string.lower(string.gsub(s2 or "", "%s", ""))
12end
13
14function fail(msg)
15 msg = msg or "failed"
16 error(msg, 2)
17end
18
19function compare(input, output)
20 local original = readfile(input)
21 local recovered = readfile(output)
22 if original ~= recovered then fail("comparison failed")
23 else print("ok") end
24end
25
26local G = _G
27local set = rawset
28local warn = print
29
30local setglobal = function(table, key, value)
31 warn("changed " .. key)
32 set(table, key, value)
33end
34
35setmetatable(G, {
36 __newindex = setglobal
37})
diff --git a/test/urltest.lua b/test/urltest.lua
index 990a3e5..7e0e73f 100644
--- a/test/urltest.lua
+++ b/test/urltest.lua
@@ -1,4 +1,4 @@
1dofile("noglobals.lua") 1dofile("testsupport.lua")
2 2
3local check_build_url = function(parsed) 3local check_build_url = function(parsed)
4 local built = socket.url.build(parsed) 4 local built = socket.url.build(parsed)