From aba3f35c496d3edd0f56bb4a2d446494ad54ba93 Mon Sep 17 00:00:00 2001 From: Robin Xiang Date: Mon, 18 Mar 2024 14:34:29 +0800 Subject: [PATCH] fix(core): fix a bug that `X-Kong-Upstream-Status` header is not set when the response is returned from the cache of `proxy-cache` plugin. FTI-5827 --- .../kong/fix-upstream-status-unset.yml | 3 ++ kong/runloop/handler.lua | 2 +- .../15-upstream-status-header_spec.lua | 35 +++++++++++++++++-- 3 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 changelog/unreleased/kong/fix-upstream-status-unset.yml diff --git a/changelog/unreleased/kong/fix-upstream-status-unset.yml b/changelog/unreleased/kong/fix-upstream-status-unset.yml new file mode 100644 index 000000000000..eefb1e02bcf4 --- /dev/null +++ b/changelog/unreleased/kong/fix-upstream-status-unset.yml @@ -0,0 +1,3 @@ +message: fix a bug that `X-Kong-Upstream-Status` will not appear in the response headers even if it is set in the `headers` parameter in the kong.conf when the response is hit and returned by proxy cache plugin. +scope: Core +type: bugfix diff --git a/kong/runloop/handler.lua b/kong/runloop/handler.lua index 1a5f3a00a00c..4ef16da3e827 100644 --- a/kong/runloop/handler.lua +++ b/kong/runloop/handler.lua @@ -1438,7 +1438,7 @@ return { local upstream_status_header = constants.HEADERS.UPSTREAM_STATUS if kong.configuration.enabled_headers[upstream_status_header] then - local upstream_status = ctx.buffered_status or tonumber(sub(var.upstream_status or "", -3)) + local upstream_status = ctx.buffered_status or tonumber(sub(var.upstream_status or "", -3)) or ngx.status header[upstream_status_header] = upstream_status if not header[upstream_status_header] then log(ERR, "failed to set ", upstream_status_header, " header") diff --git a/spec/02-integration/05-proxy/15-upstream-status-header_spec.lua b/spec/02-integration/05-proxy/15-upstream-status-header_spec.lua index 8b67a077f124..b7c24e51360e 100644 --- a/spec/02-integration/05-proxy/15-upstream-status-header_spec.lua +++ b/spec/02-integration/05-proxy/15-upstream-status-header_spec.lua @@ -18,7 +18,7 @@ local function setup_db() protocol = helpers.mock_upstream_protocol, } - bp.routes:insert { + local route1 = bp.routes:insert { protocols = { "http" }, paths = { "/status/200" }, service = service, @@ -49,6 +49,19 @@ local function setup_db() route = { id = route3.id }, } + bp.plugins:insert { + name = "proxy-cache", + route = { id = route1.id }, + config = { + response_code = { 200 }, + request_method = { "GET" }, + content_type = { "application/json" }, + cache_ttl = 300, + storage_ttl = 300, + strategy = "memory", + } + } + return bp end @@ -65,8 +78,6 @@ describe(constants.HEADERS.UPSTREAM_STATUS .. " header", function() headers = "server_tokens,latency_tokens,x-kong-upstream-status", plugins = "bundled,dummy", }) - - client = helpers.proxy_client() end) lazy_teardown(function() @@ -77,6 +88,11 @@ describe(constants.HEADERS.UPSTREAM_STATUS .. " header", function() helpers.stop_kong() end) + before_each(function() + if client then client:close() end + client = helpers.proxy_client() + end) + it("when no plugin changes status code", function() local res = assert(client:send { method = "GET", @@ -101,6 +117,19 @@ describe(constants.HEADERS.UPSTREAM_STATUS .. " header", function() assert.res_status(500, res) assert.equal("200", res.headers[constants.HEADERS.UPSTREAM_STATUS]) end) + + it("should be set when proxy-cache is enabled", function() + local res = assert(client:send { + method = "GET", + path = "/status/200", + headers = { + host = helpers.mock_upstream_host, + } + }) + assert.res_status(200, res) + assert.equal("Hit", res.headers["X-Cache-Status"]) + assert.equal("200", res.headers[constants.HEADERS.UPSTREAM_STATUS]) + end) end) describe("is not injected with default configuration", function()