Skip to content

Commit

Permalink
fix(responses) to respect headers configuration option
Browse files Browse the repository at this point in the history
  • Loading branch information
bungle authored and hishamhm committed Jun 14, 2018
1 parent b4817b3 commit 2089ec3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
16 changes: 14 additions & 2 deletions kong/tools/responses.lua
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
--
-- -- Raw send() helper:
-- return responses.send(418, "This is a teapot")

local singletons = require "kong.singletons"
local constants = require "kong.constants"
local cjson = require "cjson.safe"
local meta = require "kong.meta"

Expand Down Expand Up @@ -122,7 +123,18 @@ local function send_response(status_code)
end

ngx.status = status_code
ngx.header["Server"] = server_header

if singletons and singletons.configuration then
if singletons.configuration.enabled_headers[constants.HEADERS.SERVER] then
ngx.header[constants.HEADERS.SERVER] = server_header

else
ngx.header[constants.HEADERS.SERVER] = nil
end

else
ngx.header[constants.HEADERS.SERVER] = server_header
end

if headers then
for k, v in pairs(headers) do
Expand Down
29 changes: 29 additions & 0 deletions spec/01-unit/009-responses_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -192,4 +192,33 @@ describe("Response helpers", function()
assert.spy(s).was.called_with(ngx.ctx)
end)
end)

describe("server tokens", function()
it("are sent by default", function()
responses.send_HTTP_OK("OK")
assert.equal(ngx.status, responses.status_codes.HTTP_OK)
assert.equal(meta._SERVER_TOKENS, ngx.header["Server"])
end)
it("are sent when enabled", function()
local singletons = require "kong.singletons"
singletons.configuration = {
enabled_headers = {
["server_tokens"] = true
},
},
responses.send_HTTP_OK("OK")
assert.equal(ngx.status, responses.status_codes.HTTP_OK)
assert.equal(meta._SERVER_TOKENS, ngx.header["Server"])
end)
it("are not sent when disabled", function()
local singletons = require "kong.singletons"
singletons.configuration = {
enabled_headers = {
},
},
responses.send_HTTP_OK("OK")
assert.equal(ngx.status, responses.status_codes.HTTP_OK)
assert.is_nil(ngx.header["Server"])
end)
end)
end)

0 comments on commit 2089ec3

Please sign in to comment.