From b914db3eb3610f4ecdf78f3b3681f439066e1eb3 Mon Sep 17 00:00:00 2001 From: thefosk Date: Thu, 26 May 2016 15:04:08 -0700 Subject: [PATCH] Closing #1186 --- .../response-transformer/body_transformer.lua | 5 +++- ...dy_spec.lua => big_response_body_spec.lua} | 2 +- .../response-transformer/filter_spec.lua | 23 ++++++++++++++++++- 3 files changed, 27 insertions(+), 3 deletions(-) rename spec/plugins/response-transformer/{big_resonse_body_spec.lua => big_response_body_spec.lua} (99%) diff --git a/kong/plugins/response-transformer/body_transformer.lua b/kong/plugins/response-transformer/body_transformer.lua index 0f7a1ab680f..9db0d2c4337 100644 --- a/kong/plugins/response-transformer/body_transformer.lua +++ b/kong/plugins/response-transformer/body_transformer.lua @@ -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 @@ -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 @@ -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 diff --git a/spec/plugins/response-transformer/big_resonse_body_spec.lua b/spec/plugins/response-transformer/big_response_body_spec.lua similarity index 99% rename from spec/plugins/response-transformer/big_resonse_body_spec.lua rename to spec/plugins/response-transformer/big_response_body_spec.lua index 20d2230cecd..a8410de9f96 100644 --- a/spec/plugins/response-transformer/big_resonse_body_spec.lua +++ b/spec/plugins/response-transformer/big_response_body_spec.lua @@ -29,7 +29,7 @@ describe("Response Transformer Plugin #proxy", function() } }, __api = 1 - } + } } } spec_helper.start_kong() diff --git a/spec/plugins/response-transformer/filter_spec.lua b/spec/plugins/response-transformer/filter_spec.lua index b9698219de7..6566f030ac4 100644 --- a/spec/plugins/response-transformer/filter_spec.lua +++ b/spec/plugins/response-transformer/filter_spec.lua @@ -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 = { { @@ -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 } } } @@ -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)