Skip to content

Commit

Permalink
Closing #1186
Browse files Browse the repository at this point in the history
  • Loading branch information
subnetmarco committed May 26, 2016
1 parent c0db9e0 commit b914db3
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
5 changes: 4 additions & 1 deletion kong/plugins/response-transformer/body_transformer.lua
Expand Up @@ -56,12 +56,13 @@ function _M.transform_json_body(conf, buffered_data)
json_body[name] = nil
end

-- replace key:value to body
-- replace key:value to body
for _, name, value in iter(conf.replace.json) do
local v = cjson.encode(value)
if stringy.startswith(v, "\"") and stringy.endswith(v, "\"") then
v = v:sub(2, v:len() - 1):gsub("\\\"", "\"") -- To prevent having double encoded quotes
end
v = v:gsub("\\/", "/") -- To prevent having double encoded slashes
if json_body[name] then
json_body[name] = v
end
Expand All @@ -73,6 +74,7 @@ function _M.transform_json_body(conf, buffered_data)
if stringy.startswith(v, "\"") and stringy.endswith(v, "\"") then
v = v:sub(2, v:len() - 1):gsub("\\\"", "\"") -- To prevent having double encoded quotes
end
v = v:gsub("\\/", "/") -- To prevent having double encoded slashes
if not json_body[name] then
json_body[name] = v
end
Expand All @@ -84,6 +86,7 @@ function _M.transform_json_body(conf, buffered_data)
if stringy.startswith(v, "\"") and stringy.endswith(v, "\"") then
v = v:sub(2, v:len() - 1):gsub("\\\"", "\"") -- To prevent having double encoded quotes
end
v = v:gsub("\\/", "/") -- To prevent having double encoded slashes
json_body[name] = append_value(json_body[name],v)
end

Expand Down
Expand Up @@ -29,7 +29,7 @@ describe("Response Transformer Plugin #proxy", function()
}
},
__api = 1
}
}
}
}
spec_helper.start_kong()
Expand Down
23 changes: 22 additions & 1 deletion spec/plugins/response-transformer/filter_spec.lua
Expand Up @@ -10,7 +10,8 @@ describe("Response Transformer Plugin #proxy", function()
spec_helper.prepare_db()
spec_helper.insert_fixtures {
api = {
{name = "tests-response-transformer", request_host = "response.com", upstream_url = "http://httpbin.org"}
{name = "tests-response-transformer", request_host = "response.com", upstream_url = "http://httpbin.org"},
{name = "tests-response-transformer-2", request_host = "response2.com", upstream_url = "http://httpbin.org"}
},
plugin = {
{
Expand All @@ -22,6 +23,15 @@ describe("Response Transformer Plugin #proxy", function()
}
},
__api = 1
},
{
name = "response-transformer",
config = {
replace = {
json = {"headers:/hello/world", "args:this is a / test", "url:\"wot\""}
}
},
__api = 2
}
}
}
Expand Down Expand Up @@ -49,4 +59,15 @@ describe("Response Transformer Plugin #proxy", function()
assert.falsy(headers["access-control-allow-origin"])
end)
end)

describe("Test replace", function()
it("should replace a body parameter on GET", function()
local response, status = http_client.get(STUB_GET_URL, {}, {host = "response2.com"})
assert.equal(200, status)
local body = cjson.decode(response)
assert.equals([[/hello/world]], body.headers)
assert.equals([[this is a / test]], body.args)
assert.equals([["wot"]], body.url)
end)
end)
end)

0 comments on commit b914db3

Please sign in to comment.