Skip to content

Commit

Permalink
Merge pull request #712 from cgarvis/569-request-transform-json-next
Browse files Browse the repository at this point in the history
JSON Request Transformation
  • Loading branch information
thibaultcha committed Nov 10, 2015
2 parents 330e6e2 + c355ed2 commit 9aeb778
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
37 changes: 37 additions & 0 deletions kong/plugins/request-transformer/access.lua
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
local utils = require "kong.tools.utils"
local stringy = require "stringy"
local Multipart = require "multipart"
local cjson = require "cjson"

local _M = {}

local APPLICATION_JSON = "application/json"
local CONTENT_LENGTH = "content-length"
local FORM_URLENCODED = "application/x-www-form-urlencoded"
local MULTIPART_DATA = "multipart/form-data"
Expand Down Expand Up @@ -78,6 +80,26 @@ function _M.execute(conf)

end

if conf.add.json then
local content_type = get_content_type()
if content_type and stringy.startswith(get_content_type(), APPLICATION_JSON) then
ngx.req.read_body()
local parameters = cjson.decode(ngx.req.get_body_data())

iterate_and_exec(conf.add.json, function(name, value)
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
parameters[name] = v
end)

local new_data = cjson.encode(parameters)
ngx.req.set_header(CONTENT_LENGTH, string.len(new_data))
ngx.req.set_body_data(new_data)
end
end

if conf.remove then

-- Remove headers
Expand Down Expand Up @@ -122,6 +144,21 @@ function _M.execute(conf)
end
end

if conf.remove.json then
local content_type = get_content_type()
if content_type and stringy.startswith(get_content_type(), APPLICATION_JSON) then
ngx.req.read_body()
local parameters = cjson.decode(ngx.req.get_body_data())

iterate_and_exec(conf.remove.json, function(name)
parameters[name] = nil
end)

local new_data = cjson.encode(parameters)
ngx.req.set_header(CONTENT_LENGTH, string.len(new_data))
ngx.req.set_body_data(new_data)
end
end
end
end

Expand Down
6 changes: 4 additions & 2 deletions kong/plugins/request-transformer/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ return {
fields = {
form = { type = "array" },
headers = { type = "array" },
querystring = { type = "array" }
querystring = { type = "array" },
json = { type = "array" }
}
}
},
Expand All @@ -14,7 +15,8 @@ return {
fields = {
form = { type = "array" },
headers = { type = "array" },
querystring = { type = "array" }
querystring = { type = "array" },
json = { type = "array" }
}
}
}
Expand Down
32 changes: 30 additions & 2 deletions spec/plugins/request-transformer/access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ describe("Request Transformer", function()
add = {
headers = {"x-added:true", "x-added2:true" },
querystring = {"newparam:value"},
form = {"newformparam:newvalue"}
form = {"newformparam:newvalue"},
json = {"newjsonparam:newvalue"}
},
remove = {
headers = { "x-to-remove" },
querystring = { "toremovequery" },
form = { "toremoveform" }
form = { "toremoveform" },
json = { "toremovejson" }
}
},
__api = 1
Expand Down Expand Up @@ -90,6 +92,23 @@ describe("Request Transformer", function()
assert.are.equal("newvalue", body.postData.params["newformparam"])
end)

it("should add new paramters on json POST", function()
local response, status = http_client.post(STUB_POST_URL, {}, {host = "test1.com", ["content-type"] = "application/json"})
local raw = cjson.decode(response)
local body = cjson.decode(raw.postData.text)
assert.are.equal(200, status)
assert.are.equal("newvalue", body["newjsonparam"])
end)

it("should add new paramters on json POST when existing params exist", function()
local response, status = http_client.post(STUB_POST_URL, {hello = "world"}, {host = "test1.com", ["content-type"] = "application/json"})
local raw = cjson.decode(response)
local body = cjson.decode(raw.postData.text)
assert.are.equal(200, status)
assert.are.equal("world", body["hello"])
assert.are.equal("newvalue", body["newjsonparam"])
end)

it("should add new parameters on GET", function()
local response, status = http_client.get(STUB_GET_URL, {}, {host = "test1.com"})
local body = cjson.decode(response)
Expand Down Expand Up @@ -131,6 +150,15 @@ describe("Request Transformer", function()
assert.are.same("yes", body.postData.params["nottoremove"])
end)

it("should remove parameters on json POST", function()
local response, status = http_client.post(STUB_POST_URL, {["toremovejson"] = "yes", ["nottoremove"] = "yes"}, {host = "test1.com", ["content-type"] = "application/json"})
local raw = cjson.decode(response)
local body = cjson.decode(raw.postData.text)
assert.are.equal(200, status)
assert.falsy(body["toremovejson"])
assert.are.same("yes", body["nottoremove"])
end)

it("should remove parameters on GET", function()
local response, status = http_client.get(STUB_GET_URL, {["toremovequery"] = "yes", ["nottoremove"] = "yes"}, {host = "test1.com"})
local body = cjson.decode(response)
Expand Down

0 comments on commit 9aeb778

Please sign in to comment.