Skip to content

Commit

Permalink
Merge pull request Kong#1584 from Mashape/fix/galileo-body-size
Browse files Browse the repository at this point in the history
fix(galileo) better req/resp body length support
  • Loading branch information
thibaultcha authored Sep 2, 2016
2 parents 7f8b13a + 68c5968 commit 41bfe6c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
5 changes: 4 additions & 1 deletion kong/plugins/galileo/alf.lua
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ function _M:add_entry(_ngx, req_body_str, resp_body_str)
-- stick to what the request really contains, since it was
-- already read anyways.
local post_data, response_content
local req_body_size, resp_body_size = 0, 0
local req_body_size, resp_body_size

if self.log_bodies then
if req_body_str then
Expand All @@ -150,6 +150,9 @@ function _M:add_entry(_ngx, req_body_str, resp_body_str)
mimeType = resp_content_type
}
end
else
req_body_size = tonumber(request_content_len) or 0
resp_body_size = tonumber(resp_content_len) or 0
end

-- timings
Expand Down
62 changes: 58 additions & 4 deletions spec/03-plugins/05-galileo/01-alf_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ describe("ALF serializer", function()

local entry1 = assert(alf_with_body:add_entry(_ngx, body_str))
assert.is_table(entry1.request.postData)
assert.equal(#body_str, entry1.request.bodySize)
assert.same({
text = "base64_hello=world&foo=bar",
encoding = "base64",
Expand All @@ -209,7 +208,35 @@ describe("ALF serializer", function()

local entry2 = assert(alf_without_body:add_entry(_ngx, body_str))
assert.is_nil(entry2.request.postData)
assert.equal(0, entry2.request.bodySize)
end)
it("captures bodySize from Content-Length if not logging bodies", function()
_G.ngx.req.get_headers = function()
return {["content-length"] = "38"}
end
reload_alf_serializer()
local alf = alf_serializer.new() -- log_bodies disabled
local entry = assert(alf:add_entry(_ngx)) -- no body str
assert.equal(38, entry.request.bodySize)
end)
it("captures bodySize reading the body if logging bodies", function()
local body_str = "hello=world"

_G.ngx.req.get_headers = function()
return {["content-length"] = "3800"}
end
reload_alf_serializer()
local alf = alf_serializer.new(true) -- log_bodies enabled
local entry = assert(alf:add_entry(_ngx, body_str))
assert.equal(#body_str, entry.request.bodySize)
end)
it("zeroes bodySize if no body logging or Content-Length", function()
_G.ngx.req.get_headers = function()
return {}
end
reload_alf_serializer()
local alf = alf_serializer.new() -- log_bodies disabled
local entry = assert(alf:add_entry(_ngx)) -- no body str
assert.equal(0, entry.request.bodySize)
end)
it("ignores nil body string (no postData)", function()
local alf = alf_serializer.new(true)
Expand Down Expand Up @@ -301,7 +328,6 @@ describe("ALF serializer", function()

local entry1 = assert(alf_with_body:add_entry(_ngx, nil, body_str))
assert.is_table(entry1.response.content)
assert.equal(#body_str, entry1.response.bodySize)
assert.same({
text = "base64_message=hello",
encoding = "base64",
Expand All @@ -310,7 +336,35 @@ describe("ALF serializer", function()

local entry2 = assert(alf_without_body:add_entry(_ngx, body_str))
assert.is_nil(entry2.response.postData)
assert.equal(0, entry2.response.bodySize)
end)
it("captures bodySize from Content-Length if not logging bodies", function()
_G.ngx.resp.get_headers = function()
return {["content-length"] = "38"}
end
reload_alf_serializer()
local alf = alf_serializer.new() -- log_bodies disabled
local entry = assert(alf:add_entry(_ngx)) -- no body str
assert.equal(38, entry.response.bodySize)
end)
it("captures bodySize reading the body if logging bodies", function()
local body_str = "hello=world"

_G.ngx.resp.get_headers = function()
return {["content-length"] = "3800"}
end
reload_alf_serializer()
local alf = alf_serializer.new(true) -- log_bodies enabled
local entry = assert(alf:add_entry(_ngx, nil, body_str))
assert.equal(#body_str, entry.response.bodySize)
end)
it("zeroes bodySize if no body logging or Content-Length", function()
_G.ngx.resp.get_headers = function()
return {}
end
reload_alf_serializer()
local alf = alf_serializer.new() -- log_bodies disabled
local entry = assert(alf:add_entry(_ngx)) -- no body str
assert.equal(0, entry.response.bodySize)
end)
it("ignores nil body string (no response.content)", function()
local alf = alf_serializer.new(true)
Expand Down

0 comments on commit 41bfe6c

Please sign in to comment.