-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core) add
X-Kong-Upstream-Status
header
While Kong returns the same status code as that of the upstream when it proxies a request, it is useful to record the real status code returned by the upstream. This can be used for monitoring Kong itself if it is returning the same response as the upstream.
- Loading branch information
Showing
6 changed files
with
194 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
180 changes: 180 additions & 0 deletions
180
spec/02-integration/05-proxy/14-upstream-status-header_spec.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,180 @@ | ||
local helpers = require "spec.helpers" | ||
local constants = require "kong.constants" | ||
|
||
describe(constants.HEADERS.UPSTREAM_STATUS .. " header", function() | ||
|
||
setup(function() | ||
local bp, db, dao = helpers.get_db_utils(strategy) | ||
assert(helpers.dao:run_migrations()) | ||
|
||
local service = bp.services:insert { | ||
host = helpers.mock_upstream_host, | ||
port = helpers.mock_upstream_port, | ||
protocol = helpers.mock_upstream_protocol, | ||
} | ||
assert(service) | ||
|
||
local route1 = bp.routes:insert { | ||
protocols = { "http" }, | ||
service = service, | ||
paths = { "/foo" }, | ||
} | ||
assert(route1) | ||
|
||
local route2 = bp.routes:insert { | ||
protocols = { "http" }, | ||
service = service, | ||
paths = { "/bar" }, | ||
} | ||
assert(route2) | ||
|
||
bp.plugins:insert({ | ||
name = "dummy", | ||
route_id = route2.id, | ||
config = { | ||
resp_code = 500, | ||
} | ||
}) | ||
end) | ||
|
||
|
||
describe("should be same as upstream status code", function() | ||
local client | ||
setup(function() | ||
assert(helpers.start_kong { | ||
headers = "server_tokens,latency_tokens,x-kong-upstream-status", | ||
custom_plugins = "dummy", | ||
nginx_conf = "spec/fixtures/custom_nginx.template", | ||
}) | ||
client = helpers.proxy_client() | ||
end) | ||
|
||
teardown(function() | ||
if client then | ||
client:close() | ||
end | ||
helpers.stop_kong() | ||
end) | ||
|
||
it("when no plugin changes status code", function() | ||
local res = assert(client:send { | ||
method = "GET", | ||
path = "/foo", | ||
headers = { | ||
host = helpers.mock_upstream_host, | ||
} | ||
}) | ||
|
||
assert.res_status(200, res) | ||
assert.equal('200', res.headers[constants.HEADERS.UPSTREAM_STATUS]) | ||
end) | ||
|
||
it("when a plugin changes status code", function() | ||
local res = assert(client:send { | ||
method = "GET", | ||
host = helpers.mock_upstream_host, | ||
path = "/bar", | ||
headers = { | ||
["Host"] = helpers.mock_upstream_host, | ||
} | ||
}) | ||
assert.res_status(500, res) | ||
assert.equal('200', res.headers[constants.HEADERS.UPSTREAM_STATUS]) | ||
end) | ||
end) | ||
|
||
|
||
describe("is not injected with default configuration", function() | ||
|
||
setup(function() | ||
local bp, db, dao = helpers.get_db_utils(strategy) | ||
assert(helpers.dao:run_migrations()) | ||
|
||
local service = bp.services:insert { | ||
host = helpers.mock_upstream_host, | ||
port = helpers.mock_upstream_port, | ||
protocol = helpers.mock_upstream_protocol, | ||
} | ||
assert(service) | ||
|
||
local route1 = bp.routes:insert { | ||
protocols = { "http" }, | ||
service = service, | ||
paths = { "/foo" }, | ||
} | ||
assert(route1) | ||
|
||
assert(helpers.start_kong{ | ||
nginx_conf = "spec/fixtures/custom_nginx.template", | ||
}) | ||
end) | ||
|
||
teardown(function() | ||
if client then | ||
client:close() | ||
end | ||
helpers.stop_kong() | ||
end) | ||
|
||
it("", function() | ||
local client = helpers.proxy_client() | ||
local res = assert(client:send { | ||
method = "GET", | ||
path = "/foo", | ||
headers = { | ||
host = helpers.mock_upstream_host, | ||
} | ||
}) | ||
|
||
assert.res_status(200, res) | ||
assert.is_nil(res.headers[constants.HEADERS.UPSTREAM_STATUS]) | ||
end) | ||
end) | ||
|
||
describe("is injected with configuration [headers=X-Kong-Upstream-Status]", function() | ||
|
||
setup(function() | ||
local bp, db, dao = helpers.get_db_utils(strategy) | ||
assert(helpers.dao:run_migrations()) | ||
|
||
local service = bp.services:insert { | ||
host = helpers.mock_upstream_host, | ||
port = helpers.mock_upstream_port, | ||
protocol = helpers.mock_upstream_protocol, | ||
} | ||
assert(service) | ||
|
||
local route1 = bp.routes:insert { | ||
protocols = { "http" }, | ||
service = service, | ||
paths = { "/foo" }, | ||
} | ||
assert(route1) | ||
assert(helpers.start_kong{ | ||
nginx_conf = "spec/fixtures/custom_nginx.template", | ||
headers="X-Kong-Upstream-Status", | ||
}) | ||
end) | ||
|
||
teardown(function() | ||
if client then | ||
client:close() | ||
end | ||
helpers.stop_kong() | ||
end) | ||
|
||
it("", function() | ||
local client = helpers.proxy_client() | ||
local res = assert(client:send { | ||
method = "GET", | ||
path = "/foo", | ||
headers = { | ||
host = helpers.mock_upstream_host, | ||
} | ||
}) | ||
|
||
assert.res_status(200, res) | ||
assert('200', res.headers[constants.HEADERS.UPSTREAM_STATUS]) | ||
end) | ||
end) | ||
end) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters