Skip to content

Commit

Permalink
fix(request-transformer) header transf called before body (#4)
Browse files Browse the repository at this point in the history
Header transform must be executed before body transform, as the content-type
must be correctly set before changing body.

This change fixes issue #1
  • Loading branch information
locao authored and gszr committed Jun 25, 2019
1 parent 1cae6fb commit 4a5827d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 3 deletions.
9 changes: 6 additions & 3 deletions kong/plugins/request-transformer/access.lua
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,11 @@ local function transform_json_body(conf, body, content_length)
local removed, renamed, replaced, added, appended = false, false, false, false, false
local content_length = (body and #body) or 0
local parameters = parse_json(body)
if parameters == nil and content_length > 0 then
return false, nil
if parameters == nil then
if content_length > 0 then
return false, nil
end
parameters = {}
end

if content_length > 0 and #conf.remove.body > 0 then
Expand Down Expand Up @@ -484,8 +487,8 @@ function _M.execute(conf)
clear_environment()
transform_uri(conf)
transform_method(conf)
transform_body(conf)
transform_headers(conf)
transform_body(conf)
transform_querystrings(conf)
end

Expand Down
33 changes: 33 additions & 0 deletions spec/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ describe("Plugin: request-transformer(access) [#" .. strategy .. "]", function()
paths = { "/requests/user1/(?<user1>\\w+)/user2/(?<user2>\\S+)" },
strip_path = false
})
local route20 = bp.routes:insert({
hosts = { "test20.com" }
})

bp.plugins:insert {
route = { id = route1.id },
Expand Down Expand Up @@ -307,6 +310,20 @@ describe("Plugin: request-transformer(access) [#" .. strategy .. "]", function()
}
}

bp.plugins:insert {
route = { id = route20.id },
name = "request-transformer",
config = {
http_method = "POST",
add = {
headers = {
"Content-Type:application/json"
},
body = { "body:somecontent" }
},
}
}

assert(helpers.start_kong({
database = strategy,
plugins = "bundled, request-transformer",
Expand Down Expand Up @@ -359,6 +376,22 @@ describe("Plugin: request-transformer(access) [#" .. strategy .. "]", function()
assert.equal("world", json.uri_args.hello)
assert.equal("marco", json.uri_args.name)
end)
it("changes the HTTP method from GET to POST and adds JSON body", function()
local r = assert(client:send {
method = "GET",
path = "/request",
headers = {
host = "test20.com",
}
})
assert.response(r).has.status(200)
local json = assert.response(r).has.jsonbody()
assert.request(r).has.jsonbody()
assert.equal("POST", json.vars.request_method)
assert.is_nil(json.post_data.error)
local header_content_type = assert.request(r).has.header("Content-Type")
assert.equals("application/json", header_content_type)
end)
end)
describe("remove", function()
it("specified header", function()
Expand Down

0 comments on commit 4a5827d

Please sign in to comment.