2019-05-06 14:36:18 +08:00
|
|
|
use t::APIMeta 'no_plan';
|
|
|
|
|
2019-05-06 16:02:28 +08:00
|
|
|
repeat_each(2);
|
2019-05-06 14:36:18 +08:00
|
|
|
no_long_string();
|
|
|
|
log_level('info');
|
|
|
|
|
|
|
|
run_tests;
|
|
|
|
|
|
|
|
__DATA__
|
|
|
|
|
2019-05-06 16:02:28 +08:00
|
|
|
=== TEST 1: check arguments
|
2019-05-06 14:36:18 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
2019-05-07 15:58:44 +08:00
|
|
|
local plugin = require("apimeta.plugins.example-plugin")
|
2019-05-06 14:36:18 +08:00
|
|
|
local ok, err = plugin.check_args({i = 1, s = "s", t = {}})
|
|
|
|
if not ok then
|
|
|
|
ngx.say("failed to check args: ", err)
|
|
|
|
end
|
|
|
|
|
|
|
|
ok, err = plugin.check_args({s = "s", t = {}})
|
|
|
|
if not ok then
|
|
|
|
ngx.say("failed to check args: ", err)
|
|
|
|
end
|
|
|
|
|
|
|
|
ok, err = plugin.check_args({i = 1, s = 3, t = {}})
|
|
|
|
if not ok then
|
|
|
|
ngx.say("failed to check args: ", err)
|
|
|
|
end
|
|
|
|
|
|
|
|
ok, err = plugin.check_args({i = 1, s = "s", t = ""})
|
|
|
|
if not ok then
|
|
|
|
ngx.say("failed to check args: ", err)
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say("done")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
failed to check args: args.i expect int value but got: [nil]
|
|
|
|
failed to check args: args.s expect string value but got: [3]
|
|
|
|
failed to check args: args.t expect table value but got: []
|
|
|
|
done
|
2019-05-06 16:02:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 2: load plugins
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local plugins, err = require("apimeta.plugin").load()
|
|
|
|
if not plugins then
|
|
|
|
ngx.say("failed to load plugins: ", err)
|
|
|
|
end
|
|
|
|
|
|
|
|
local encode_json = require "cjson.safe" .encode
|
|
|
|
for _, plugin in ipairs(plugins) do
|
|
|
|
ngx.say("plugin name: ", plugin.name,
|
|
|
|
" priority: ", plugin.priority)
|
|
|
|
|
|
|
|
plugin.rewrite()
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
2019-05-07 15:58:44 +08:00
|
|
|
plugin name: example-plugin priority: 1000
|
2019-05-06 16:02:28 +08:00
|
|
|
--- error_log
|
2019-05-07 15:58:44 +08:00
|
|
|
failed to load plugin not-exist-plugin err: module 'apimeta.plugins.not-exist-plugin' not found
|
2019-05-06 16:02:28 +08:00
|
|
|
rewrite(): plugin rewrite phase
|
2019-05-06 18:17:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 3: filter plugins
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local plugin = require("apimeta.plugin")
|
|
|
|
|
|
|
|
local all_plugins, err = plugin.load()
|
|
|
|
if not all_plugins then
|
|
|
|
ngx.say("failed to load plugins: ", err)
|
|
|
|
end
|
|
|
|
|
|
|
|
local filter_plugins = plugin.filter_plugin({
|
2019-05-07 15:58:44 +08:00
|
|
|
["example-plugin"] = {i = 1, s = "s", t = {1, 2}},
|
|
|
|
["new-plugin"] = {a = "a"},
|
2019-05-06 18:17:29 +08:00
|
|
|
}, all_plugins)
|
|
|
|
|
|
|
|
local encode_json = require "cjson.safe" .encode
|
|
|
|
for i = 1, #filter_plugins, 2 do
|
|
|
|
local plugin = filter_plugins[i]
|
|
|
|
local plugin_conf = filter_plugins[i + 1]
|
|
|
|
ngx.say("plugin [", plugin.name, "] config: ",
|
|
|
|
encode_json(plugin_conf))
|
|
|
|
end
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
2019-05-07 15:58:44 +08:00
|
|
|
plugin [example-plugin] config: {"i":1,"s":"s","t":[1,2]}
|