From d818b5580a81000781ecfa7b3d8f51c9497992dc Mon Sep 17 00:00:00 2001 From: Wangchong Zhou Date: Tue, 13 Aug 2024 02:25:34 +0800 Subject: [PATCH] tests(tools): add tests against `kong.tools.http.parse_directive_header()` (#13482) Co-authored-by: Qi (cherry picked from commit 97b16801b091551065e211a11eaa612696bfd3eb) --- spec/01-unit/05-utils_spec.lua | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/spec/01-unit/05-utils_spec.lua b/spec/01-unit/05-utils_spec.lua index 5923c0eca267..e2008aed59b9 100644 --- a/spec/01-unit/05-utils_spec.lua +++ b/spec/01-unit/05-utils_spec.lua @@ -186,6 +186,34 @@ describe("Utils", function() assert.is_true(tools_http.check_https(true, false)) assert.is_true(tools_http.check_https(true, true)) end) + + it("test parsing directive header", function() + -- EMPTY table return by the function with `nil` should be read-only + assert.is_false(pcall(function() + tools_http.parse_directive_header(nil)["foo"] = "bar" + end)) + + -- 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", + }) + + -- test table + assert.same(tools_http.parse_directive_header({ + "cache-age=300", + "cache-key=kong-cache", + }), { + ["cache-age"] = 300, + ["cache-key"] = "kong-cache", + }) + end) end) end)