-
Notifications
You must be signed in to change notification settings - Fork 4.8k
/
ngx_stub.lua
65 lines (59 loc) · 1.71 KB
/
ngx_stub.lua
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
--- Stub _G.ngx for unit testing.
-- Creates a stub for `ngx` for use by Kong's modules such as the DAO. It allows to use them
-- outside of the nginx context such as when using the CLI, or unit testing.
--
-- Monkeypatches the global `ngx` table.
local reg = require "rex_pcre"
_G.ngx = {
req = {},
ctx = {},
header = {},
exit = function() end,
say = function() end,
log = function() end,
socket = { tcp = {} },
now = function() return os.time() end,
time = function() return os.time() end,
timer = {
at = function() end
},
re = {
match = reg.match,
gsub = function(str, pattern, sub)
local res_str, _, sub_made = reg.gsub(str, pattern, sub)
return res_str, sub_made
end
},
encode_base64 = function(str)
return string.format("base64_%s", str)
end,
-- Builds a querystring from a table, separated by `&`
-- @param `tab` The key/value parameters
-- @param `key` The parent key if the value is multi-dimensional (optional)
-- @return `querystring` A string representing the built querystring
encode_args = function(tab, key)
local query = {}
local keys = {}
for k in pairs(tab) do
keys[#keys+1] = k
end
table.sort(keys)
for _, name in ipairs(keys) do
local value = tab[name]
if key then
name = string.format("%s[%s]", tostring(key), tostring(name))
end
if type(value) == "table" then
query[#query+1] = ngx.encode_args(value, name)
else
value = tostring(value)
if value ~= "" then
query[#query+1] = string.format("%s=%s", name, value)
else
query[#query+1] = name
end
end
end
return table.concat(query, "&")
end
}