1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
pcall(require, "luarocks.require")
require "wsapi.request"
local function make_env_get(qs)
return {
REQUEST_METHOD = "GET",
QUERY_STRING = qs or "",
CONTENT_LENGTH = 0,
PATH_INFO = "/",
SCRIPT_NAME = "",
CONTENT_TYPE = "x-www-form-urlencoded",
input = {
read = function () return nil end
}
}
end
local function make_env_post(pd, type, qs)
pd = pd or ""
return {
REQUEST_METHOD = "POST",
QUERY_STRING = qs or "",
CONTENT_LENGTH = #pd,
PATH_INFO = "/",
CONTENT_TYPE = type or "x-www-form-urlencoded",
SCRIPT_NAME = "",
input = {
post_data = pd,
current = 1,
read = function (self, len)
if self.current > #self.post_data then return nil end
local s = self.post_data:sub(self.current, len)
self.current = self.current + len
return s
end
}
}
end
local function encode_multipart(boundary, fields)
local parts = { "--" .. boundary }
for _, t in ipairs(fields) do
parts[#parts+1] = '\r\nContent-Disposition: form-data; name="' .. t[1] .. '"\r\n\r\n' .. t[2] .. '\r\n--' .. boundary
end
return table.concat(parts)
end
local function is_empty_table(t)
for k, v in pairs(t) do return false, k, v end
return true
end
print("Test empty GET")
local env = make_env_get()
local req = wsapi.request.new(env)
assert(req.path_info == env.PATH_INFO)
assert(req.method == env.REQUEST_METHOD)
assert(req.script_name == env.SCRIPT_NAME)
assert(req.query_string == env.QUERY_STRING)
assert(is_empty_table(req.GET))
assert(is_empty_table(req.POST))
print("Test one-parameter GET")
local env = make_env_get("foo=bar")
local req = wsapi.request.new(env)
assert(req.GET["foo"] == "bar")
assert(req.params["foo"] == "bar")
assert(is_empty_table(req.POST))
print("Test one-parameter POST")
local env = make_env_post("foo=bar")
local req = wsapi.request.new(env)
assert(is_empty_table(req.GET))
assert(req.POST["foo"] == "bar")
assert(req.params["foo"] == "bar")
print("Test empty POST that is not form-encoded")
local env = make_env_post(nil, "application/json")
local req = wsapi.request.new(env)
assert(is_empty_table(req.GET))
assert(req.POST["post_data"] == "")
assert(req.params["post_data"] == "")
print("Test POST with content that is not form-encoded")
local env = make_env_post("{ foo: bar }", "application/json")
local req = wsapi.request.new(env)
assert(is_empty_table(req.GET))
assert(req.POST["post_data"] == "{ foo: bar }")
assert(req.params["post_data"] == "{ foo: bar }")
print("Test two-parameter GET")
local env = make_env_get("foo=bar&baz=boo")
local req = wsapi.request.new(env)
assert(req.GET["foo"] == "bar")
assert(req.GET["baz"] == "boo")
assert(req.params["foo"] == "bar")
assert(req.params["baz"] == "boo")
assert(is_empty_table(req.POST))
print("Test two-parameter POST")
local env = make_env_post("foo=bar&baz=boo")
local req = wsapi.request.new(env)
assert(is_empty_table(req.GET))
assert(req.POST["foo"] == "bar")
assert(req.POST["baz"] == "boo")
assert(req.params["foo"] == "bar")
assert(req.params["baz"] == "boo")
assert(not req:parse_post())
print("Test POST with GET")
local env = make_env_post("baz=boo", nil, "foo=bar")
local req = wsapi.request.new(env)
assert(req.GET["foo"] == "bar")
assert(req.POST["baz"] == "boo")
assert(req.params["foo"] == "bar")
assert(req.params["baz"] == "boo")
print("Test one-parameter POST")
local env = make_env_post("foo=bar")
local req = wsapi.request.new(env)
assert(is_empty_table(req.GET))
assert(req.POST["foo"] == "bar")
assert(req.params["foo"] == "bar")
print("Test multipart/form-data")
local boundary = "hello"
local env = make_env_post(encode_multipart(boundary, { { "foo", "bar\nbar" }, { "baz", "boo" } }),
"multipart/form-data; boundary=" .. boundary)
local req = wsapi.request.new(env)
assert(is_empty_table(req.GET))
assert(req.POST["foo"] == "bar\nbar")
assert(req.POST["baz"] == "boo")
assert(req.params["foo"] == "bar\nbar")
assert(req.params["baz"] == "boo")
print("Test POST with repeat parameters")
local env = make_env_post("foo=bar&foo=boo")
local req = wsapi.request.new(env)
assert(is_empty_table(req.GET))
assert(#req.POST["foo"] == 2)
assert(req.POST["foo"][1] == "bar")
assert(req.POST["foo"][2] == "boo")
print("Test GET with repeat parameters")
local env = make_env_get("foo=bar&foo=boo")
local req = wsapi.request.new(env)
assert(is_empty_table(req.POST))
assert(#req.GET["foo"] == 2)
assert(req.GET["foo"][1] == "bar")
assert(req.GET["foo"][2] == "boo")
print("Test POST with repeat parameters and overwrite enabled")
local env = make_env_post("foo=bar&foo=boo")
local req = wsapi.request.new(env, { overwrite = true })
assert(is_empty_table(req.GET))
assert(req.POST["foo"] == "boo")
print("Test GET with repeat parameters and overwrite enabled")
local env = make_env_get("foo=bar&foo=boo")
local req = wsapi.request.new(env, { overwrite = true })
assert(is_empty_table(req.POST))
assert(req.GET["foo"] == "boo")
print("Test multipart/form-data with repeat parameters")
local boundary = "hello"
local env = make_env_post(encode_multipart(boundary, { { "foo", "bar\nbar" }, { "foo", "boo" } }),
"multipart/form-data; boundary=" .. boundary)
local req = wsapi.request.new(env)
assert(is_empty_table(req.GET))
assert(#req.POST["foo"] == 2)
assert(req.POST["foo"][1] == "bar\nbar")
assert(req.POST["foo"][2] == "boo")
print("Test multipart/form-data with repeat parameters and overwrite")
local boundary = "hello"
local env = make_env_post(encode_multipart(boundary, { { "foo", "bar\nbar" }, { "foo", "boo" } }),
"multipart/form-data; boundary=" .. boundary)
local req = wsapi.request.new(env, { overwrite = true })
assert(is_empty_table(req.GET))
assert(req.POST["foo"] == "boo")
print("Test delayed POST")
local env = make_env_post("foo=bar&baz=boo")
local req = wsapi.request.new(env, { delay_post = true })
assert(is_empty_table(req.GET))
assert(is_empty_table(req.POST))
assert(req:parse_post())
assert(req.POST["foo"] == "bar")
assert(req.POST["baz"] == "boo")
assert(req.params["foo"] == "bar")
assert(req.params["baz"] == "boo")
assert(not req:parse_post())
|