From 6ec08c77a57ff35173dc5491870574281e41b98a Mon Sep 17 00:00:00 2001 From: spacewander Date: Mon, 7 Dec 2020 19:42:58 +0800 Subject: [PATCH] feat(fault-injection): support Nginx variable in abort.body Fix #2827 --- apisix/core/utils.lua | 41 +++++++++++ apisix/plugins/fault-injection.lua | 16 ++-- doc/plugins/fault-injection.md | 6 +- doc/zh-cn/plugins/fault-injection.md | 6 +- t/core/utils.t | 40 ++++++++++ t/plugin/fault-injection.t | 106 +++++++++++++++++++++++++++ 6 files changed, 198 insertions(+), 17 deletions(-) diff --git a/apisix/core/utils.lua b/apisix/core/utils.lua index a7cad379e629..041bd7e8be46 100644 --- a/apisix/core/utils.lua +++ b/apisix/core/utils.lua @@ -14,6 +14,7 @@ -- See the License for the specific language governing permissions and -- limitations under the License. -- +local core_str = require("apisix.core.string") local table = require("apisix.core.table") local ngx_re = require("ngx.re") local resolver = require("resty.dns.resolver") @@ -25,6 +26,8 @@ local math = math local sub_str = string.sub local str_byte = string.byte local tonumber = tonumber +local tostring = tostring +local re_gsub = ngx.re.gsub local type = type local C = ffi.C local ffi_string = ffi.string @@ -219,4 +222,42 @@ end _M.sleep = sleep +local resolve_var +do + local _ctx + local pat = [[(?= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 21: hit route(body with var) +--- request +GET /hello +--- response_body +client addr: 127.0.0.1 +--- no_error_log +[error] + + + +=== TEST 22: set route(abort without body) +--- config + location /t { + content_by_lua_block { + local t = require("lib.test_admin").test + local code, body = t('/apisix/admin/routes/1', + ngx.HTTP_PUT, + [[{ + "plugins": { + "fault-injection": { + "abort": { + "http_status": 200 + } + }, + "proxy-rewrite": { + "uri": "/hello" + } + }, + "upstream": { + "nodes": { + "127.0.0.1:1980": 1 + }, + "type": "roundrobin" + }, + "uri": "/hello" + }]] + ) + + if code >= 300 then + ngx.status = code + end + ngx.say(body) + } + } +--- request +GET /t +--- response_body +passed +--- no_error_log +[error] + + + +=== TEST 23: hit route(abort without body) +--- request +GET /hello +--- response_body +--- no_error_log +[error]