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

feat(plugin/aws-lambda)accept string type statusCode as return when working in proxy integration mode #8765

Merged
merged 6 commits into from
May 17, 2022
Merged
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
27 changes: 24 additions & 3 deletions kong/plugins/aws-lambda/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ local cjson = require "cjson.safe"
local meta = require "kong.meta"
local constants = require "kong.constants"
local request_util = require "kong.plugins.aws-lambda.request-util"

local kong = kong

local VIA_HEADER = constants.HEADERS.VIA
local VIA_HEADER_VALUE = meta._NAME .. "/" .. meta._VERSION
Expand Down Expand Up @@ -63,6 +63,27 @@ local function get_now()
end


local function validate_http_status_code(status_code)
if not status_code then
return false
end

if type(status_code) == "string" then
status_code = tonumber(status_code)

if not status_code then
return false
end
end

if status_code >= 100 and status_code <= 599 then
return status_code
end

return false
end


--[[
Response format should be
{
Expand All @@ -72,8 +93,8 @@ end
}
--]]
local function validate_custom_response(response)
if type(response.statusCode) ~= "number" then
return nil, "statusCode must be a number"
if not validate_http_status_code(response.statusCode) then
return nil, "statusCode validation failed"
end

if response.headers ~= nil and type(response.headers) ~= "table" then
Expand Down
58 changes: 56 additions & 2 deletions spec/03-plugins/27-aws-lambda/99-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -805,7 +805,7 @@ for _, strategy in helpers.each_strategy() do
-- and the tests pass.
-- see: https://github.com/Kong/kong/commit/c6f9e4558b5a654e78ca96b2ba4309e527053403#diff-9d13d8efc852de84b07e71bf419a2c4d

it("sets proper status code on custom response from Lambda", function()
it("sets proper status code (type = number) on custom response from Lambda", function()
local res = assert(proxy_client:send {
method = "POST",
path = "/post",
Expand All @@ -823,6 +823,24 @@ for _, strategy in helpers.each_strategy() do
assert.equal("", body)
end)

it("sets proper status code (type = string) on custom response from Lambda", function()
local res = assert(proxy_client:send {
method = "POST",
path = "/post",
headers = {
["Host"] = "lambda11.com",
["Content-Type"] = "application/json"
},
body = {
statusCode = "201",
}
})
local body = assert.res_status(201, res)
assert.equal(0, tonumber(res.headers["Content-Length"]))
assert.equal(nil, res.headers["X-Custom-Header"])
assert.equal("", body)
end)

it("sets proper status code/headers/body on custom response from Lambda", function()
-- the lambda function must return a string
-- for the custom response "body" property
Expand Down Expand Up @@ -878,7 +896,7 @@ for _, strategy in helpers.each_strategy() do
assert.equal("bar", res.headers["x-amzn-RequestId"])
end)

it("returns HTTP 502 when 'status' property of custom response is not a number", function()
it("returns HTTP 502 when 'status' property of custom response contains non-numeric character", function()
local res = assert(proxy_client:send {
method = "POST",
path = "/post",
Expand All @@ -896,6 +914,42 @@ for _, strategy in helpers.each_strategy() do
assert.equal("Bad Gateway", b.message)
end)

it("returns HTTP 502 when 'status' property of custom response is not a valid HTTP status code", function()
local res = assert(proxy_client:send {
method = "POST",
path = "/post",
headers = {
["Host"] = "lambda11.com",
["Content-Type"] = "application/json",
},
body = {
statusCode = "99",
}
})

assert.res_status(502, res)
local b = assert.response(res).has.jsonbody()
assert.equal("Bad Gateway", b.message)
end)

it("returns HTTP 502 when 'status' property of custom response is not a valid HTTP status code", function()
local res = assert(proxy_client:send {
method = "POST",
path = "/post",
headers = {
["Host"] = "lambda11.com",
["Content-Type"] = "application/json",
},
body = {
statusCode = "600",
}
})

assert.res_status(502, res)
local b = assert.response(res).has.jsonbody()
assert.equal("Bad Gateway", b.message)
end)

it("returns HTTP 502 when 'headers' property of custom response is not a table", function()
local res = assert(proxy_client:send {
method = "POST",
Expand Down