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

tests(tools): add parse_directive_header and calculate_resource_ttl function unit test #13499

Merged
merged 1 commit into from
Aug 15, 2024
Merged
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
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)
Loading