aboutsummaryrefslogtreecommitdiff
path: root/test/httptest.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/httptest.lua')
-rw-r--r--test/httptest.lua89
1 files changed, 52 insertions, 37 deletions
diff --git a/test/httptest.lua b/test/httptest.lua
index 030974c..9d9fa25 100644
--- a/test/httptest.lua
+++ b/test/httptest.lua
@@ -1,8 +1,8 @@
1-- needs Alias from /home/c/diego/tec/luasocket/test to 1-- needs Alias from /home/c/diego/tec/luasocket/test to
2-- /luasocket-test 2-- "/luasocket-test" and "/luasocket-test/"
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 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
6local similar = function(s1, s2) 6local similar = function(s1, s2)
7 return string.lower(string.gsub(s1 or "", "%s", "")) == 7 return string.lower(string.gsub(s1 or "", "%s", "")) ==
8 string.lower(string.gsub(s2 or "", "%s", "")) 8 string.lower(string.gsub(s2 or "", "%s", ""))
@@ -31,12 +31,18 @@ local check_request = function(request, expect, ignore)
31 local response = socket.http.request(request) 31 local response = socket.http.request(request)
32 for i,v in response do 32 for i,v in response do
33 if not ignore[i] then 33 if not ignore[i] then
34 if v ~= expect[i] then fail(i .. " differs!") end 34 if v ~= expect[i] then
35 if string.len(v) < 80 then print(v) end
36 fail(i .. " differs!")
37 end
35 end 38 end
36 end 39 end
37 for i,v in expect do 40 for i,v in expect do
38 if not ignore[i] then 41 if not ignore[i] then
39 if v ~= response[i] then fail(i .. " differs!") end 42 if v ~= response[i] then
43 if string.len(v) < 80 then print(v) end
44 fail(i .. " differs!")
45 end
40 end 46 end
41 end 47 end
42 print("ok") 48 print("ok")
@@ -47,15 +53,18 @@ local host, request, response, ignore, expect, index, prefix, cgiprefix
47local t = socket.time() 53local t = socket.time()
48 54
49host = host or "localhost" 55host = host or "localhost"
50prefix = prefix or "/luasocket" 56prefix = prefix or "/luasocket-test"
51cgiprefix = cgiprefix or "/luasocket/cgi" 57cgiprefix = cgiprefix or "/luasocket-test-cgi"
52index = readfile("test/index.html") 58index = readfile("test/index.html")
53 59
54io.write("testing request uri correctness: ") 60io.write("testing request uri correctness: ")
55local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string" 61local forth = cgiprefix .. "/request-uri?" .. "this+is+the+query+string"
56local back = socket.http.get("http://" .. host .. forth) 62local back, h, c, e = socket.http.get("http://" .. host .. forth)
57if similar(back, forth) then print("ok") 63if similar(back, forth) then print("ok")
58else fail("failed!") end 64else
65print(h, c, e)
66fail()
67end
59 68
60io.write("testing query string correctness: ") 69io.write("testing query string correctness: ")
61forth = "this+is+the+query+string" 70forth = "this+is+the+query+string"
@@ -77,6 +86,38 @@ ignore = {
77} 86}
78check_request(request, expect, ignore) 87check_request(request, expect, ignore)
79 88
89socket.http.get("http://" .. host .. prefix .. "/lixo.html")
90
91io.write("testing post method: ")
92-- wanted to test chunked post, but apache doesn't support it...
93request = {
94 url = "http://" .. host .. cgiprefix .. "/cat",
95 method = "POST",
96 body = index,
97 -- remove content-length header to send chunked body
98 headers = { ["content-length"] = string.len(index) }
99}
100expect = {
101 body = index,
102 code = 200
103}
104ignore = {
105 status = 1,
106 headers = 1
107}
108check_request(request, expect, ignore)
109
110io.write("testing simple post function: ")
111body = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index)
112check(body == index)
113
114io.write("testing simple post function with table args: ")
115body = socket.http.post {
116 url = "http://" .. host .. cgiprefix .. "/cat",
117 body = index
118}
119check(body == index)
120
80io.write("testing http redirection: ") 121io.write("testing http redirection: ")
81request = { 122request = {
82 url = "http://" .. host .. prefix 123 url = "http://" .. host .. prefix
@@ -175,7 +216,8 @@ io.write("testing manual basic auth: ")
175request = { 216request = {
176 url = "http://" .. host .. prefix .. "/auth/index.html", 217 url = "http://" .. host .. prefix .. "/auth/index.html",
177 headers = { 218 headers = {
178 authorization = "Basic " .. socket.code.base64("luasocket:password") 219 authorization = "Basic " ..
220 socket.code.base64.encode("luasocket:password")
179 } 221 }
180} 222}
181expect = { 223expect = {
@@ -246,22 +288,6 @@ ignore = {
246} 288}
247check_request(request, expect, ignore) 289check_request(request, expect, ignore)
248 290
249io.write("testing post method: ")
250request = {
251 url = "http://" .. host .. cgiprefix .. "/cat",
252 method = "POST",
253 body = index
254}
255expect = {
256 body = index,
257 code = 200
258}
259ignore = {
260 status = 1,
261 headers = 1
262}
263check_request(request, expect, ignore)
264
265io.write("testing wrong scheme: ") 291io.write("testing wrong scheme: ")
266request = { 292request = {
267 url = "wrong://" .. host .. cgiprefix .. "/cat", 293 url = "wrong://" .. host .. cgiprefix .. "/cat",
@@ -287,17 +313,6 @@ body = socket.http.get {
287} 313}
288check(body == index) 314check(body == index)
289 315
290io.write("testing simple post function: ")
291body = socket.http.post("http://" .. host .. cgiprefix .. "/cat", index)
292check(body == index)
293
294io.write("testing simple post function with table args: ")
295body = socket.http.post {
296 url = "http://" .. host .. cgiprefix .. "/cat",
297 body = index
298}
299check(body == index)
300
301io.write("testing HEAD method: ") 316io.write("testing HEAD method: ")
302response = socket.http.request { 317response = socket.http.request {
303 method = "HEAD", 318 method = "HEAD",