Skip to content

Commit

Permalink
fix(core): fix a bug that X-Kong-Upstream-Status header is not set …
Browse files Browse the repository at this point in the history
…when

the response is returned from the cache of `proxy-cache` plugin.

FTI-5827
  • Loading branch information
liverpool8056 committed Mar 18, 2024
1 parent 58fc97d commit aba3f35
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
3 changes: 3 additions & 0 deletions changelog/unreleased/kong/fix-upstream-status-unset.yml
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion kong/runloop/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
35 changes: 32 additions & 3 deletions spec/02-integration/05-proxy/15-upstream-status-header_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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

Expand All @@ -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()
Expand All @@ -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",
Expand All @@ -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()
Expand Down

0 comments on commit aba3f35

Please sign in to comment.