Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(request-termination) allow null value for body or message #3784

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions kong/plugins/request-termination/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
80 changes: 78 additions & 2 deletions spec/03-plugins/27-request-termination/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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()
Expand Down