2019-05-13 14:28:54 +08:00
|
|
|
use t::APISix 'no_plan';
|
2019-05-06 14:36:18 +08:00
|
|
|
|
2019-05-06 16:02:28 +08:00
|
|
|
repeat_each(2);
|
2019-05-17 15:58:15 +08:00
|
|
|
no_long_string();
|
2019-06-13 12:01:36 +08:00
|
|
|
no_root_location();
|
2019-05-06 14:36:18 +08:00
|
|
|
run_tests;
|
|
|
|
|
|
|
|
__DATA__
|
|
|
|
|
2019-06-10 23:41:43 +08:00
|
|
|
=== TEST 1: sanity
|
2019-05-06 14:36:18 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
2019-05-13 14:28:54 +08:00
|
|
|
local plugin = require("apisix.plugins.example-plugin")
|
2019-06-13 19:11:15 +08:00
|
|
|
local ok, err = plugin.check_schema({i = 1, s = "s", t = {1}})
|
2019-05-06 14:36:18 +08:00
|
|
|
if not ok then
|
2019-06-10 23:41:43 +08:00
|
|
|
ngx.say(err)
|
2019-05-06 14:36:18 +08:00
|
|
|
end
|
|
|
|
|
2019-06-10 23:41:43 +08:00
|
|
|
ngx.say("done")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
done
|
2019-06-12 05:58:33 +08:00
|
|
|
--- no_error_log
|
|
|
|
[error]
|
2019-06-10 23:41:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 2: missing args
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local plugin = require("apisix.plugins.example-plugin")
|
|
|
|
|
2019-06-13 19:11:15 +08:00
|
|
|
local ok, err = plugin.check_schema({s = "s", t = {1}})
|
2019-06-10 23:41:43 +08:00
|
|
|
if not ok then
|
|
|
|
ngx.say(err)
|
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say("done")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
invalid "required" in docuement at pointer "#"
|
|
|
|
done
|
2019-06-12 05:58:33 +08:00
|
|
|
--- no_error_log
|
|
|
|
[error]
|
2019-06-10 23:41:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 3: small then minimum
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local plugin = require("apisix.plugins.example-plugin")
|
2019-06-13 19:11:15 +08:00
|
|
|
local ok, err = plugin.check_schema({i = -1, s = "s", t = {1, 2}})
|
2019-05-06 14:36:18 +08:00
|
|
|
if not ok then
|
2019-06-10 23:41:43 +08:00
|
|
|
ngx.say(err)
|
2019-05-06 14:36:18 +08:00
|
|
|
end
|
|
|
|
|
2019-06-10 23:41:43 +08:00
|
|
|
ngx.say("done")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
invalid "minimum" in docuement at pointer "#/i"
|
|
|
|
done
|
2019-06-12 05:58:33 +08:00
|
|
|
--- no_error_log
|
|
|
|
[error]
|
2019-06-10 23:41:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 4: wrong type of string
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local plugin = require("apisix.plugins.example-plugin")
|
2019-06-13 19:11:15 +08:00
|
|
|
local ok, err = plugin.check_schema({i = 1, s = 123, t = {1}})
|
2019-05-06 14:36:18 +08:00
|
|
|
if not ok then
|
2019-06-10 23:41:43 +08:00
|
|
|
ngx.say(err)
|
2019-05-06 14:36:18 +08:00
|
|
|
end
|
|
|
|
|
2019-06-10 23:41:43 +08:00
|
|
|
ngx.say("done")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
|
|
|
invalid "type" in docuement at pointer "#/s"
|
|
|
|
done
|
2019-06-12 05:58:33 +08:00
|
|
|
--- no_error_log
|
|
|
|
[error]
|
2019-06-10 23:41:43 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== TEST 5: the size of array < minItems
|
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
|
|
|
local plugin = require("apisix.plugins.example-plugin")
|
2019-06-13 19:11:15 +08:00
|
|
|
local ok, err = plugin.check_schema({i = 1, s = '123', t = {}})
|
2019-05-06 14:36:18 +08:00
|
|
|
if not ok then
|
2019-06-10 23:41:43 +08:00
|
|
|
ngx.say(err)
|
2019-05-06 14:36:18 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
ngx.say("done")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
--- request
|
|
|
|
GET /t
|
|
|
|
--- response_body
|
2019-06-10 23:41:43 +08:00
|
|
|
invalid "type" in docuement at pointer "#/t"
|
2019-05-06 14:36:18 +08:00
|
|
|
done
|
2019-06-12 05:58:33 +08:00
|
|
|
--- no_error_log
|
|
|
|
[error]
|
2019-05-06 16:02:28 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-06-10 23:41:43 +08:00
|
|
|
=== TEST 6: load plugins
|
2019-05-06 16:02:28 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
2019-05-21 23:24:19 +08:00
|
|
|
local plugins, err = require("apisix.plugin").load()
|
2019-05-06 16:02:28 +08:00
|
|
|
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-08 10:38:45 +08:00
|
|
|
--- yaml_config
|
2019-06-12 05:58:33 +08:00
|
|
|
etcd:
|
|
|
|
host: "http://127.0.0.1:2379" # etcd address
|
|
|
|
prefix: "/apisix" # apisix configurations prefix
|
|
|
|
timeout: 1
|
|
|
|
|
2019-05-08 10:38:45 +08:00
|
|
|
plugins:
|
|
|
|
- example-plugin
|
|
|
|
- not-exist-plugin
|
2019-06-12 05:58:33 +08:00
|
|
|
--- grep_error_log eval
|
|
|
|
qr/\[error\].*/
|
|
|
|
--- grep_error_log_out eval
|
|
|
|
qr/module 'apisix.plugins.not-exist-plugin' not found/
|
2019-05-06 18:17:29 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-06-10 23:41:43 +08:00
|
|
|
=== TEST 7: filter plugins
|
2019-05-06 18:17:29 +08:00
|
|
|
--- config
|
|
|
|
location /t {
|
|
|
|
content_by_lua_block {
|
2019-05-21 23:24:19 +08:00
|
|
|
local plugin = require("apisix.plugin")
|
2019-05-06 18:17:29 +08:00
|
|
|
|
|
|
|
local all_plugins, err = plugin.load()
|
|
|
|
if not all_plugins then
|
|
|
|
ngx.say("failed to load plugins: ", err)
|
|
|
|
end
|
|
|
|
|
2019-05-27 17:29:58 +08:00
|
|
|
local filter_plugins = plugin.filter({
|
2019-05-17 15:58:15 +08:00
|
|
|
value = {
|
2019-06-13 17:25:39 +08:00
|
|
|
plugins = {
|
2019-05-17 15:58:15 +08:00
|
|
|
["example-plugin"] = {i = 1, s = "s", t = {1, 2}},
|
|
|
|
["new-plugin"] = {a = "a"},
|
|
|
|
}
|
|
|
|
},
|
|
|
|
modifiedIndex = 1,
|
|
|
|
}, all_plugins)
|
2019-05-06 18:17:29 +08:00
|
|
|
|
|
|
|
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]}
|