Skip to content

Commit

Permalink
make 'echo downstream' configurable and false default
Browse files Browse the repository at this point in the history
- also revert busted:match, since it's not for regex
  • Loading branch information
opyate committed Mar 23, 2016
1 parent e90ee34 commit 4e12036
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 13 deletions.
10 changes: 7 additions & 3 deletions kong/plugins/correlation-id/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ function CorrelationIdHandler:access(conf)
req_set_header(conf.header_name, header_value)
end

-- For later use, to echo it back downstream
ngx.ctx.correlationid_header_value = header_value
if conf.echo_downstream then
-- For later use, to echo it back downstream
ngx.ctx.correlationid_header_value = header_value
end
end

function CorrelationIdHandler:header_filter(conf)
CorrelationIdHandler.super.header_filter(self)
ngx.header[conf.header_name] = ngx.ctx.correlationid_header_value
if conf.echo_downstream then
ngx.header[conf.header_name] = ngx.ctx.correlationid_header_value
end
end

return CorrelationIdHandler
4 changes: 4 additions & 0 deletions kong/plugins/correlation-id/schema.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ return {
type = "string",
default = "uuid#counter",
enum = {"uuid", "uuid#counter"}
},
echo_downstream = {
type = "boolean",
default = false
}
}
}
29 changes: 19 additions & 10 deletions spec/plugins/correlation-id/access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ describe("Correlation ID Plugin", function()
api = {
{request_host = "correlation1.com", upstream_url = "http://mockbin.com"},
{request_host = "correlation2.com", upstream_url = "http://mockbin.com"},
{request_host = "correlation3.com", upstream_url = "http://mockbin.com"}
{request_host = "correlation3.com", upstream_url = "http://mockbin.com"},
{request_host = "correlation4.com", upstream_url = "http://mockbin.com"}
},
plugin = {
{name = "correlation-id", config = {}, __api = 1},
{name = "correlation-id", config = {header_name = "Foo-Bar-Id"}, __api = 2},
{name = "correlation-id", config = {generator = "uuid"}, __api = 3}
{name = "correlation-id", config = {echo_downstream = true}, __api = 1},
{name = "correlation-id", config = {header_name = "Foo-Bar-Id", echo_downstream = true}, __api = 2},
{name = "correlation-id", config = {generator = "uuid", echo_downstream = true}, __api = 3},
{name = "correlation-id", config = {}, __api = 4},
}
}
spec_helper.start_kong()
Expand All @@ -31,15 +33,15 @@ describe("Correlation ID Plugin", function()
end)

local function test_with(host, header, pattern)
local response1, status1 = http_client.get(STUB_GET_URL, nil, {host = host})
local _, status1, headers1 = http_client.get(STUB_GET_URL, nil, {host = host})
assert.equal(200, status1)
local correlation_id1 = json.decode(response1).headers[header:lower()]
assert.match(correlation_id1, pattern)
local correlation_id1 = headers1[header:lower()]
assert.truthy(correlation_id1:match(pattern))

local response2, status2 = http_client.get(STUB_GET_URL, nil, {host = host})
local _, status2, headers2 = http_client.get(STUB_GET_URL, nil, {host = host})
assert.equal(200, status2)
local correlation_id2 = json.decode(response2).headers[header:lower()]
assert.match(correlation_id2, pattern)
local correlation_id2 = headers2[header:lower()]
assert.truthy(correlation_id2:match(pattern))

assert.are_not_equals(correlation_id1, correlation_id2)

Expand Down Expand Up @@ -79,4 +81,11 @@ describe("Correlation ID Plugin", function()
local correlation_id = json.decode(response).headers[DEFAULT_HEADER_NAME:lower()]
assert.equals(existing_correlation_id, correlation_id)
end)

it("should not echo back the correlation header", function()
local _, status, headers = http_client.get(STUB_GET_URL, nil, {host = "correlation4.com"})
assert.equal(200, status)
local correlation_id = headers[DEFAULT_HEADER_NAME:lower()]
assert.falsy(correlation_id)
end)
end)

0 comments on commit 4e12036

Please sign in to comment.