aproxy/test.lua

86 lines
1.7 KiB
Lua
Raw Normal View History

2022-12-06 18:53:20 +00:00
lu = require('luaunit')
local rex = require('rex_pcre2')
2022-12-07 18:26:11 +00:00
require('util')
2022-12-06 18:53:20 +00:00
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
-- only hold data here
ngx.var = {}
-- 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
-- regex api
ngx.re = {}
ngx.re.match = rex.match
ngx.re.search = rex.find
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')
2022-12-07 18:26:11 +00:00
local config = require('config')
function setupTest(module_require_path, input_config)
2022-12-06 18:53:20 +00:00
resetNgx()
local module = require(module_require_path)
2022-12-07 18:26:11 +00:00
local schema_errors = config.validateSchema(module.config, input_config)
local count = table.pprint(schema_errors)
lu.assertIs(count, 0)
2023-10-27 01:19:08 +00:00
local state = module.init(input_config)
2022-12-06 18:53:20 +00:00
ctx.compiled_chain = {
2022-12-07 18:26:11 +00:00
{module, input_config, state}
2022-12-06 18:53:20 +00:00
}
return module
end
function onRequest()
ctx:setWantedScripts()
2023-10-27 01:19:08 +00:00
local context = require('ctx')
2022-12-06 18:53:20 +00:00
do
2023-10-27 01:19:08 +00:00
context:onRequest()
2022-12-06 18:53:20 +00:00
end
end
require('tests.webfinger_allowlist')
2022-12-07 17:57:07 +00:00
require('tests.schema_validation')
2022-12-06 18:53:20 +00:00
os.exit(lu.LuaUnit.run())