aproxy/test.lua
2025-11-23 17:48:57 -03:00

163 lines
3.6 KiB
Lua

lu = require('luaunit')
local rex = require('rex_pcre2')
require('util')
-- Preload mock resty libraries for testing
package.preload['resty.sha256'] = function()
return require('tests.mock_resty_sha256')
end
package.preload['resty.string'] = function()
return require('tests.mock_resty_string')
end
package.preload['resty.random'] = function()
return require('tests.mock_resty_random')
end
-- Create a mock shared dictionary
local function createMockSharedDict()
local storage = {}
return {
get = function(self, key)
local item = storage[key]
if not item then return nil end
if item.expiry and item.expiry < ngx.time() then
storage[key] = nil
return nil
end
return item.value
end,
set = function(self, key, value, exptime)
storage[key] = {
value = value,
expiry = exptime and (ngx.time() + exptime) or nil
}
return true, nil
end,
delete = function(self, key)
storage[key] = nil
end
}
end
-- Create a proxy table that dynamically creates mock dictionaries on access
local function createSharedProxy()
local dicts = {}
return setmetatable({}, {
__index = function(_, key)
if not dicts[key] then
dicts[key] = createMockSharedDict()
end
return dicts[key]
end
})
end
function createNgx()
local ngx = {
status = nil
}
local function mockedThing(self, property)
return function(value)
self['_'..property] = value
end
end
ngx.say = mockedThing(ngx, "say")
ngx.exit = mockedThing(ngx, "exit")
ngx.log = function (_, msg)
print(msg)
end
-- Log level constants
ngx.DEBUG = 7
ngx.INFO = 6
ngx.WARN = 4
ngx.ERR = 3
-- only hold data here
ngx.var = {
request_method = 'GET'
}
-- request params api
ngx.req = {}
ngx.req.get_uri_args = function ()
return ngx._uri_args
end
ngx.req.set_uri_args = function (val)
ngx._uri_args = val
end
ngx.req.read_body = function() end
ngx.req.get_post_args = function()
return ngx._post_args or {}
end
ngx.req.get_headers = function()
return ngx._headers or {}
end
-- response headers
ngx.header = {}
-- regex api
ngx.re = {}
ngx.re.match = rex.match
ngx.re.search = rex.find
-- shared memory dictionaries for testing (dynamically created on access)
ngx.shared = createSharedProxy()
-- time function for testing
ngx.time = function() return 1000000 end
return ngx
end
function resetNgx()
ngx = createNgx()
end
teardownNgx = resetNgx
function setupFakeRequest(path, options)
ngx.var.uri = path
if options.params then
ngx.req.set_uri_args(options.params)
end
end
local ctx = require('ctx')
local config = require('config')
function setupTest(module_require_path, input_config)
resetNgx()
local module = require(module_require_path)
local schema_errors = config.validateSchema(module.config, input_config)
local count = table.pprint(schema_errors)
lu.assertIs(count, 0)
local state = module.init(input_config)
ctx.compiled_chain = {
{module, input_config, state}
}
return module
end
function onRequest()
ctx:setWantedScripts()
local context = require('ctx')
do
context:onRequest()
end
end
require('tests.webfinger_allowlist')
require('tests.schema_validation')
require('tests.ddos_protection_challenge')
os.exit(lu.LuaUnit.run())