Skip to content

Commit

Permalink
[fix/response-transformer] concatenating value to response header
Browse files Browse the repository at this point in the history
Part of the fix for issue #393. New property `concat` added to support adding new value to existing response header. If header  does not exist, a new one will be added
  • Loading branch information
Shashi Ranjan committed Nov 18, 2015
1 parent 8cc02dc commit ee12492
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 5 deletions.
5 changes: 4 additions & 1 deletion kong/plugins/response-transformer/body_filter.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ local utils = require "kong.tools.utils"
local stringy = require "stringy"
local cjson = require "cjson"

local table_concat = table.concat
local pcall = pcall

local _M = {}

local APPLICATION_JSON = "application/json"
Expand All @@ -27,7 +30,7 @@ local function read_response_body()
ngx.arg[1] = nil
end
if eof then
local response_body = table.concat(buffered)
local response_body = table_concat(buffered)
return response_body
end
return nil
Expand Down
27 changes: 26 additions & 1 deletion kong/plugins/response-transformer/header_filter.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
local utils = require "kong.tools.utils"
local stringy = require "stringy"

local table_insert = table.insert
local type = type

local _M = {}

local APPLICATION_JSON = "application/json"
Expand All @@ -24,6 +27,19 @@ local function iterate_and_exec(val, cb)
end
end

local function concat_value(current_value, value)
local current_value_type = type(current_value)

if current_value_type == "string" then
return { current_value, value }
elseif current_value_type == "table" then
table_insert(current_value, value)
return current_value
else
return { value }
end
end

function _M.execute(conf)
local is_json_body = stringy.startswith(get_content_type(), APPLICATION_JSON)

Expand Down Expand Up @@ -58,7 +74,16 @@ function _M.execute(conf)
end

end


if conf.concat then

-- concat header
if conf.concat.headers then
iterate_and_exec(conf.concat.headers, function(name, value)
ngx.header[name] = concat_value(ngx.header[name], value)
end)
end
end
end

return _M
6 changes: 6 additions & 0 deletions kong/plugins/response-transformer/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ return {
headers = { type = "array" }
}
}
},
concat = { type = "table", schema = {
fields = {
headers = { type = "array" }
}
}
}
}
}
33 changes: 30 additions & 3 deletions spec/plugins/response-transformer/filter_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ describe("Response Transformer Plugin #proxy", function()
spec_helper.insert_fixtures {
api = {
{name = "tests-response-transformer", request_host = "response.com", upstream_url = "http://httpbin.org"},
{name = "tests-response-transformer2", request_host = "response2.com", upstream_url = "http://httpbin.org"}
{name = "tests-response-transformer2", request_host = "response2.com", upstream_url = "http://httpbin.org"},
{name = "tests-response-transformer3", request_host = "response3.com", upstream_url = "http://httpbin.org"}
},
plugin = {
{
Expand All @@ -38,7 +39,19 @@ describe("Response Transformer Plugin #proxy", function()
}
},
__api = 2
}
},
{
name = "response-transformer",
config = {
add = {
headers = {"h1:a1"}
},
concat = {
headers = {"h1:a2", "h1:a3", "h2:b1"}
}
},
__api = 3
},
}
}

Expand Down Expand Up @@ -96,5 +109,19 @@ describe("Response Transformer Plugin #proxy", function()
end)

end)


describe("Test concatenating parameters", function()

it("should create new header if not its missing", function()
local _, status, headers = http_client.get(STUB_GET_URL, {}, {host = "response3.com"})
assert.are.equal(200, status)
assert.are.equal("b1", headers["h2"])
end)

it("should concat value if header exists", function()
local _, status, headers = http_client.get(STUB_GET_URL, {}, {host = "response3.com"})
assert.are.equal(200, status)
assert.are.equal("a1, a2, a3", headers["h1"])
end)
end)
end)

0 comments on commit ee12492

Please sign in to comment.