Skip to content

Commit

Permalink
fix(proxy-cache): correctly add age header on cache hit
Browse files Browse the repository at this point in the history
  • Loading branch information
jschmid1 committed Jul 23, 2024
1 parent 4c772c6 commit 623d5ee
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 1 deletion.
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

0 comments on commit 623d5ee

Please sign in to comment.