Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(proxy-cache): correctly add age header on cache hit #13387

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions changelog/unreleased/kong/proxy-cache-fix-age-header.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
message: |
**proxy-cache**: Fixed an issue where the Age header was not being updated correctly when serving cached responses.
scope: Plugin
type: bugfix
2 changes: 1 addition & 1 deletion kong/plugins/proxy-cache/handler.lua
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ function ProxyCacheHandler:access(conf)


reset_res_header(res)
set_res_header(res, "Age", floor(time() - res.timestamp), conf)
set_res_header(res, "age", floor(time() - res.timestamp), conf)
set_res_header(res, "X-Cache-Status", "Hit", conf)
set_res_header(res, "X-Cache-Key", cache_key, conf)

Expand Down
82 changes: 82 additions & 0 deletions spec/03-plugins/31-proxy-cache/02-access_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,12 @@ do
local route22 = assert(bp.routes:insert({
hosts = { "route-22.test" },
}))
local route23 = assert(bp.routes:insert({
hosts = { "route-23.test" },
}))
local route24 = assert(bp.routes:insert({
hosts = { "route-24.test" },
}))

local consumer1 = assert(bp.consumers:insert {
username = "bob",
Expand Down Expand Up @@ -336,6 +342,32 @@ do
},
})

assert(bp.plugins:insert {
name = "proxy-cache",
route = { id = route23.id },
config = {
strategy = policy,
content_type = { "text/plain", "application/json" },
[policy] = policy_config,
response_headers = {
age = true,
["X-Cache-Status"] = true,
["X-Cache-Key"] = true
},
},
})

assert(bp.plugins:insert {
name = "proxy-cache",
route = { id = route24.id },
config = {
strategy = policy,
content_type = { "text/plain", "application/json" },
[policy] = policy_config,
-- leave reponse_header to default values
},
})

assert(helpers.start_kong({
plugins = "bundled",
nginx_conf = "spec/fixtures/custom_nginx.template",
Expand Down Expand Up @@ -416,6 +448,56 @@ do
assert.is_not_nil(res.headers["X-Cache-Key"])
end)

it("response_headers headers on the response when configured", function()
-- Initial query to set cache
local res = assert(client:get("/get", {
headers = {
Host = "route-23.test",
},
}))
-- Cache should be Miss
assert.res_status(200, res)
assert.is_same("Miss", res.headers["X-Cache-Status"])
assert.is_not_nil(res.headers["X-Cache-Key"])
assert.is_nil(res.headers["Age"])
-- Cache should be HIT
res = assert(client:get("/get", {
headers = {
Host = "route-23.test",
},
}))
assert.res_status(200, res)
assert.same("Hit", res.headers["X-Cache-Status"])
-- response_headers are configured
assert.is_not_nil(res.headers["Age"])
assert.is_not_nil(res.headers["X-Cache-Key"])
end)

it("response_headers headers on the response when set to default", function()
-- Initial query to set cache
local res = assert(client:get("/get", {
headers = {
Host = "route-24.test",
},
}))
-- Cache should be Miss
assert.res_status(200, res)
assert.is_same("Miss", res.headers["X-Cache-Status"])
assert.is_not_nil(res.headers["X-Cache-Key"])
assert.is_nil(res.headers["Age"])
res = assert(client:get("/get", {
headers = {
Host = "route-24.test",
},
}))
-- Cache should be Hit
assert.res_status(200, res)
assert.same("Hit", res.headers["X-Cache-Status"])
-- response_headers are on by default
assert.is_not_nil(res.headers["Age"])
assert.is_not_nil(res.headers["X-Cache-Key"])
end)

it("respects cache ttl", function()
local res = assert(get(client, "route-6.test"))

Expand Down
Loading