From 6d5a5551ecff26cc3aee0cfe4b2da3cca383b0ce Mon Sep 17 00:00:00 2001 From: Shashi Ranjan Date: Mon, 17 Sep 2018 15:57:48 -0700 Subject: [PATCH] fix(request-termination) allow null vlaue for body or message Allow user to unset either message or body. Changes: - schema self_check update to support null value - add relevant spec --- kong/plugins/request-termination/schema.lua | 8 +- .../27-request-termination/02-access_spec.lua | 80 ++++++++++++++++++- 2 files changed, 84 insertions(+), 4 deletions(-) diff --git a/kong/plugins/request-termination/schema.lua b/kong/plugins/request-termination/schema.lua index 351a6676a9a8..ebfa3f5f1736 100644 --- a/kong/plugins/request-termination/schema.lua +++ b/kong/plugins/request-termination/schema.lua @@ -14,8 +14,8 @@ return { end end - if plugin_t.message then - if plugin_t.content_type or plugin_t.body then + if plugin_t.message and plugin_t.message ~= ngx.null then + if plugin_t.content_type or (plugin_t.body and plugin_t.body ~= ngx.null) then return false, Errors.schema("message cannot be used with content_type or body") end else @@ -24,6 +24,10 @@ return { end end + if plugin_t.body == ngx.null and plugin_t.message == ngx.null then + return false, Errors.schema("either body or message must be set") + end + return true end } diff --git a/spec/03-plugins/27-request-termination/02-access_spec.lua b/spec/03-plugins/27-request-termination/02-access_spec.lua index 1be49fc37338..a3d1d0c77b9c 100644 --- a/spec/03-plugins/27-request-termination/02-access_spec.lua +++ b/spec/03-plugins/27-request-termination/02-access_spec.lua @@ -10,6 +10,7 @@ for _, strategy in helpers.each_strategy() do describe("Plugin: request-termination (access) [#" .. strategy .. "]", function() local proxy_client local admin_client + local plugin_message, plugin_body setup(function() local bp = helpers.get_db_utils(strategy) @@ -52,7 +53,7 @@ for _, strategy in helpers.each_strategy() do }, } - bp.plugins:insert { + plugin_message = bp.plugins:insert { name = "request-termination", route_id = route3.id, config = { @@ -79,7 +80,7 @@ for _, strategy in helpers.each_strategy() do }, } - bp.plugins:insert { + plugin_body = bp.plugins:insert { name = "request-termination", route_id = route6.id, config = { @@ -145,7 +146,35 @@ for _, strategy in helpers.each_strategy() do local json = cjson.decode(body) assert.same({ message = "Invalid" }, json) end) + it("patch config to use message", function() + local res = assert(admin_client:send { + method = "PATCH", + path = "/plugins/" .. plugin_message.id, + body = { + config = { + message = ngx.null, + body = '{"code": 1, "message": "Service unavailable"}', + } + }, + headers = { + ["Content-type"] = "application/json" + } + }) + local body = assert.res_status(200, res) + local plugin = cjson.decode(body) + assert.is_nil(plugin.config.message) + local res = assert(proxy_client:send { + method = "GET", + path = "/status/200", + headers = { + ["Host"] = "api3.request-termination.com" + } + }) + local body = assert.res_status(406, res) + local json = cjson.decode(body) + assert.same({ code = 1, message = "Service unavailable" }, json) + end) end) describe("status code and body", function() @@ -185,6 +214,53 @@ for _, strategy in helpers.each_strategy() do local json = cjson.decode(body) assert.same({ code = 1, message = "Service unavailable" }, json) end) + it("patch config to use message", function() + local res = assert(admin_client:send { + method = "PATCH", + path = "/plugins/" .. plugin_body.id, + body = { + config = { + message = "Invalid", + body = ngx.null + } + }, + headers = { + ["Content-type"] = "application/json" + } + }) + local body = assert.res_status(200, res) + local plugin = cjson.decode(body) + assert.is_nil(plugin.config.body) + + local res = assert(proxy_client:send { + method = "GET", + path = "/status/200", + headers = { + ["Host"] = "api6.request-termination.com" + } + }) + local body = assert.res_status(503, res) + local json = cjson.decode(body) + assert.same({ message = "Invalid" }, json) + end) + it("patch to set message and body both null", function() + local res = assert(admin_client:send { + method = "PATCH", + path = "/plugins/" .. plugin_body.id, + body = { + config = { + message = ngx.null, + body = ngx.null + } + }, + headers = { + ["Content-type"] = "application/json" + } + }) + local body = assert.res_status(400, res) + local json = cjson.decode(body) + assert.same({ message = "either body or message must be set" }, json) + end) end) it("returns server tokens with Server header", function()