Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix response transformer hang. See issues #1260 and #1207 #1263

Merged
merged 1 commit into from
May 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion kong/plugins/response-transformer/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ local BasePlugin = require "kong.plugins.base_plugin"
local body_filter = require "kong.plugins.response-transformer.body_transformer"
local header_filter = require "kong.plugins.response-transformer.header_transformer"

local is_body_transform_set = header_filter.is_body_transform_set
local is_json_body = header_filter.is_json_body

local ResponseTransformerHandler = BasePlugin:extend()


function ResponseTransformerHandler:new()
ResponseTransformerHandler.super.new(self, "response-transformer")
end
Expand All @@ -20,7 +24,8 @@ end

function ResponseTransformerHandler:body_filter(conf)
ResponseTransformerHandler.super.body_filter(self)
if body_filter.is_json_body(ngx.header["content-type"]) then

if is_body_transform_set(conf) and is_json_body(ngx.header["content-type"]) then
local chunk, eof = ngx.arg[1], ngx.arg[2]
if eof then
local body = body_filter.transform_json_body(conf, ngx.ctx.buffer)
Expand Down
4 changes: 4 additions & 0 deletions kong/plugins/response-transformer/header_transformer.lua
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ local function is_body_transform_set(conf)
return #conf.add.json > 0 or #conf.remove.json > 0 or #conf.replace.json > 0 or #conf.append.json > 0
end

-- export utility functions
_M.is_json_body = is_json_body
_M.is_body_transform_set = is_body_transform_set

---
-- # Example:
-- ngx.headers = header_filter.transform_headers(conf, ngx.headers)
Expand Down
85 changes: 85 additions & 0 deletions spec/plugins/response-transformer/skip_body_spec.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
-- Related to issue https://github.com/Mashape/kong/issues/1207
-- unit test to check body remains unaltered
describe("response-transformer body-check", function()

local old_ngx, handler

setup(function()
old_ngx = ngx
_G.ngx = { -- busted requires explicit _G to access the global environment
log = function() end,
header = {
["content-type"] = "application/json",
},
arg = {},
ctx = {
buffer = "",
},
}
handler = require("kong.plugins.response-transformer.handler")
handler:new()
end)

teardown(function()
ngx = old_ngx
end)

it("check the body to remain unaltered if no transforms have been set", function()
-- only a header transform, no body changes
local conf = {
remove = {
headers = {"h1", "h2", "h3"},
json = {}
},
add = {
headers = {},
json = {},
},
append = {
headers = {},
json = {},
},
replace = {
headers = {},
json = {},
},
}
local body = [[
{
"id": 1,
"name": "Some One",
"username": "Bretchen",
"email": "Not@here.com",
"address": {
"street": "Down Town street",
"suite": "Apt. 23",
"city": "Gwendoline"
},
"phone": "1-783-729-8531 x56442",
"website": "hardwork.org",
"company": {
"name": "BestBuy",
"catchPhrase": "just a bunch of words",
"bs": "bullshit words"
}
}
]]

ngx.arg[1] = body
handler:body_filter(conf)
local result = ngx.arg[1]
ngx.arg[1] = ""
ngx.arg[2] = true -- end of body marker
handler:body_filter(conf)
result = result .. ngx.arg[1]

-- body filter should not execute, it would parse and reencode the json, removing
-- the whitespace. So check equality to make sure whitespace is still there, and hence
-- body was not touched.
assert.are.same(body, result)

end)

end)