Skip to content

Commit

Permalink
chore(tools): add parse_directive_header and calculate_resource_ttl f…
Browse files Browse the repository at this point in the history
…unction unit test (#13499)
  • Loading branch information
oowl authored Aug 15, 2024
1 parent 700b3b0 commit b45233d
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions spec/01-unit/05-utils_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -1734,4 +1734,51 @@ describe("Utils", function()
assert.equal(nil, kong_table.table_path(t, path))
end)
end)

it("test parse_directive_header function", function()
-- test null
assert.same(tools_http.parse_directive_header(nil), {})

-- test empty string
assert.same(tools_http.parse_directive_header(""), {})

-- test string
assert.same(tools_http.parse_directive_header("cache-key=kong-cache,cache-age=300"), {
["cache-age"] = 300,
["cache-key"] = "kong-cache",
})
end)

it("test calculate_resource_ttl function", function()
-- test max-age header
_G.ngx = {
var = {
sent_http_expires = "60",
},
}
local access_control_header = tools_http.parse_directive_header("cache-key=kong-cache,max-age=300")

assert.same(tools_http.calculate_resource_ttl(access_control_header), 300)

-- test s-maxage header
_G.ngx = {
var = {
sent_http_expires = "60",
},
}
local access_control_header = tools_http.parse_directive_header("cache-key=kong-cache,s-maxage=310")

assert.same(tools_http.calculate_resource_ttl(access_control_header), 310)

-- test empty headers
local expiry_year = os.date("%Y") + 1
_G.ngx = {
var = {
sent_http_expires = os.date("!%a, %d %b ") .. expiry_year .. " " .. os.date("!%X GMT") -- format: "Thu, 18 Nov 2099 11:27:35 GMT",
},
}

-- chop the last digit to avoid flaky tests (clock skew)
assert.same(string.sub(tools_http.calculate_resource_ttl(), 0, -2), "3153600")
end)
end)

1 comment on commit b45233d

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bazel Build

Docker image available kong/kong:b45233d9a55c1dbac1f42213ffdc46d7b946175a
Artifacts available https://github.com/Kong/kong/actions/runs/10400129725

Please sign in to comment.