-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
101 additions
and
3 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
61 changes: 61 additions & 0 deletions
61
spec/02-integration/05-proxy/13-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,61 @@ | ||
local helpers = require "spec.helpers" | ||
local constants = require "kong.constants" | ||
|
||
|
||
describe(constants.HEADERS.UPSTREAM_STATUS .. " header", function() | ||
local client | ||
|
||
setup(function() | ||
assert(helpers.dao.apis:insert { | ||
name = "upstream-header-1", | ||
hosts = { "upstream-header-1.com" }, | ||
upstream_url = helpers.mock_upstream_url, | ||
}) | ||
local api2 = assert(helpers.dao.apis:insert { | ||
name = "upstream-header-2", | ||
hosts = { "upstream-header-2.com" }, | ||
upstream_url = helpers.mock_upstream_url, | ||
}) | ||
assert(helpers.dao.plugins:insert { | ||
name = "force-error", | ||
api_id = api2.id, | ||
}) | ||
assert(helpers.start_kong({ | ||
nginx_conf = "spec/fixtures/custom_nginx.template", | ||
custom_plugins = "force-error", | ||
})) | ||
client = helpers.proxy_client() | ||
end) | ||
|
||
teardown(function() | ||
if client then | ||
client:close() | ||
end | ||
helpers.stop_kong() | ||
end) | ||
|
||
it("should be same as upstream staus code", function() | ||
local res = assert(client:send { | ||
method = "GET", | ||
path = "/", | ||
headers = { | ||
host = "upstream-header-1.com", | ||
} | ||
}) | ||
|
||
assert.res_status(200, res) | ||
assert.equal('200', res.headers[constants.HEADERS.UPSTREAM_STATUS]) | ||
end) | ||
|
||
it("should be same as upstream staus code even if plugin changes status code", function() | ||
local res = assert(client:send { | ||
method = "GET", | ||
path = "/status/200", | ||
headers = { | ||
["Host"] = "upstream-header-2.com", | ||
} | ||
}) | ||
assert.res_status(500, res) | ||
assert.equal('200', res.headers[constants.HEADERS.UPSTREAM_STATUS]) | ||
end) | ||
end) |
19 changes: 19 additions & 0 deletions
19
spec/fixtures/custom_plugins/kong/plugins/force-error/handler.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,19 @@ | ||
local BasePlugin = require "kong.plugins.base_plugin" | ||
|
||
|
||
local ForceError = BasePlugin:extend() | ||
|
||
|
||
ForceError.PRIORITY = 1000 | ||
|
||
|
||
function ForceError:new() | ||
ForceError.super.new(self, "force-error") | ||
end | ||
|
||
function ForceError:header_filter() | ||
ForceError.super.access(self) | ||
ngx.status = ngx.HTTP_INTERNAL_SERVER_ERROR | ||
end | ||
|
||
return ForceError |
4 changes: 4 additions & 0 deletions
4
spec/fixtures/custom_plugins/kong/plugins/force-error/schema.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,4 @@ | ||
return { | ||
fields = { | ||
} | ||
} |