diff --git a/kong/plugins/response-transformer/body_filter.lua b/kong/plugins/response-transformer/body_filter.lua index 8825a0775e7e..93dc95b5e94f 100644 --- a/kong/plugins/response-transformer/body_filter.lua +++ b/kong/plugins/response-transformer/body_filter.lua @@ -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" @@ -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 diff --git a/kong/plugins/response-transformer/header_filter.lua b/kong/plugins/response-transformer/header_filter.lua index 217b9c4b32aa..915330b58040 100644 --- a/kong/plugins/response-transformer/header_filter.lua +++ b/kong/plugins/response-transformer/header_filter.lua @@ -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" @@ -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) @@ -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 diff --git a/kong/plugins/response-transformer/schema.lua b/kong/plugins/response-transformer/schema.lua index db024d57f48f..89a5e438ea23 100644 --- a/kong/plugins/response-transformer/schema.lua +++ b/kong/plugins/response-transformer/schema.lua @@ -13,6 +13,12 @@ return { headers = { type = "array" } } } + }, + concat = { type = "table", schema = { + fields = { + headers = { type = "array" } + } + } } } } diff --git a/spec/plugins/response-transformer/filter_spec.lua b/spec/plugins/response-transformer/filter_spec.lua index 189a3d6c3f9f..283e058e4073 100644 --- a/spec/plugins/response-transformer/filter_spec.lua +++ b/spec/plugins/response-transformer/filter_spec.lua @@ -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 = { { @@ -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 + }, } } @@ -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)