Debian Codegraph @

test/lua/unit/caseless_table.lua

raw
1 local caseless_table = require "rspamd_caseless_table"
2
3 context("Caseless table tests", function()
4
5 context("Creation", function()
6 test("create() returns empty table", function()
7 local ct = caseless_table.create()
8 assert_not_nil(ct)
9 assert_equal(#ct, 0)
10 end)
11
12 test("from_table() converts a regular table", function()
13 local ct = caseless_table.from_table({
14 ["Content-Type"] = "text/html",
15 ["X-Spam"] = "yes",
16 })
17 assert_not_nil(ct)
18 assert_equal(#ct, 2)
19 end)
20 end)
21
22 context("Case-insensitive lookup", function()
23 test("Lookup ignores case", function()
24 local ct = caseless_table.from_table({
25 ["Content-Type"] = "text/html",
26 })
27 assert_equal(ct["Content-Type"], "text/html")
28 assert_equal(ct["content-type"], "text/html")
29 assert_equal(ct["CONTENT-TYPE"], "text/html")
30 assert_equal(ct["Content-type"], "text/html")
31 end)
32
33 test("Missing key returns nil", function()
34 local ct = caseless_table.from_table({
35 ["X-Header"] = "value",
36 })
37 assert_nil(ct["Y-Header"])
38 end)
39 end)
40
41 context("Key case preservation", function()
42 test("to_table preserves original key case", function()
43 local ct = caseless_table.from_table({
44 ["X-Original-Case"] = "value1",
45 })
46 local t = ct:to_table()
47 assert_not_nil(t["X-Original-Case"])
48 assert_equal(t["X-Original-Case"], "value1")
49 end)
50
51 test("each() yields original key case", function()
52 local ct = caseless_table.from_table({
53 ["X-MyHeader"] = "val",
54 })
55 local found = false
56 for k, v in ct:each() do
57 if k == "X-MyHeader" and v == "val" then
58 found = true
59 end
60 end
61 assert_true(found)
62 end)
63 end)
64
65 context("Assignment (__newindex)", function()
66 test("Set new key", function()
67 local ct = caseless_table.create()
68 ct["X-New"] = "hello"
69 assert_equal(ct["x-new"], "hello")
70 assert_equal(#ct, 1)
71 end)
72
73 test("Overwrite existing key with different case", function()
74 local ct = caseless_table.from_table({
75 ["Content-Type"] = "text/plain",
76 })
77 ct["content-type"] = "text/html"
78 assert_equal(ct["Content-Type"], "text/html")
79 assert_equal(#ct, 1)
80 end)
81
82 test("Delete key by assigning nil", function()
83 local ct = caseless_table.from_table({
84 ["X-Remove"] = "bye",
85 ["X-Keep"] = "stay",
86 })
87 assert_equal(#ct, 2)
88 ct["x-remove"] = nil
89 assert_nil(ct["X-Remove"])
90 assert_equal(#ct, 1)
91 assert_equal(ct["X-Keep"], "stay")
92 end)
93 end)
94
95 context("has_key", function()
96 test("Returns true for existing key", function()
97 local ct = caseless_table.from_table({
98 ["X-Exists"] = "yes",
99 })
100 assert_true(ct:has_key("X-Exists"))
101 assert_true(ct:has_key("x-exists"))
102 assert_true(ct:has_key("X-EXISTS"))
103 end)
104
105 test("Returns false for missing key", function()
106 local ct = caseless_table.from_table({
107 ["X-Exists"] = "yes",
108 })
109 assert_false(ct:has_key("X-Missing"))
110 end)
111 end)
112
113 context("Length (__len)", function()
114 test("Empty table has length 0", function()
115 local ct = caseless_table.create()
116 assert_equal(#ct, 0)
117 end)
118
119 test("Reflects number of keys", function()
120 local ct = caseless_table.from_table({
121 a = "1",
122 b = "2",
123 c = "3",
124 })
125 assert_equal(#ct, 3)
126 end)
127 end)
128
129 context("Iteration with each()", function()
130 test("Iterates over all keys", function()
131 local src = {
132 ["Alpha"] = "a",
133 ["Beta"] = "b",
134 ["Gamma"] = "c",
135 }
136 local ct = caseless_table.from_table(src)
137 local collected = {}
138 for k, v in ct:each() do
139 collected[k] = v
140 end
141 assert_equal(collected["Alpha"], "a")
142 assert_equal(collected["Beta"], "b")
143 assert_equal(collected["Gamma"], "c")
144 end)
145 end)
146
147 context("to_table()", function()
148 test("Converts back to regular table", function()
149 local src = {
150 ["Key1"] = "val1",
151 ["Key2"] = "val2",
152 }
153 local ct = caseless_table.from_table(src)
154 local t = ct:to_table()
155 assert_equal(type(t), "table")
156 assert_equal(t["Key1"], "val1")
157 assert_equal(t["Key2"], "val2")
158 end)
159 end)
160
161 context("Tostring (__tostring)", function()
162 test("Returns a descriptive string", function()
163 local ct = caseless_table.from_table({
164 a = "1",
165 b = "2",
166 })
167 local s = tostring(ct)
168 assert_match("caseless_table", s)
169 assert_match("2", s)
170 end)
171 end)
172
173 context("Multi-value entries", function()
174 test("from_table with array value stores multi-value", function()
175 local ct = caseless_table.from_table({
176 ["Set-Cookie"] = {"cookie1", "cookie2", "cookie3"},
177 })
178 -- __index returns first element
179 assert_equal(ct["Set-Cookie"], "cookie1")
180 -- get_all returns full array
181 local all = ct:get_all("set-cookie")
182 assert_not_nil(all)
183 assert_equal(type(all), "table")
184 assert_equal(#all, 3)
185 assert_equal(all[1], "cookie1")
186 assert_equal(all[2], "cookie2")
187 assert_equal(all[3], "cookie3")
188 end)
189
190 test("get_all wraps single value in array", function()
191 local ct = caseless_table.from_table({
192 ["X-Single"] = "only-one",
193 })
194 local all = ct:get_all("X-Single")
195 assert_equal(type(all), "table")
196 assert_equal(#all, 1)
197 assert_equal(all[1], "only-one")
198 end)
199
200 test("get_all returns nil for missing key", function()
201 local ct = caseless_table.create()
202 local all = ct:get_all("missing")
203 assert_nil(all)
204 end)
205 end)
206
207 context("Edge cases", function()
208 test("Empty string key", function()
209 local ct = caseless_table.create()
210 ct[""] = "empty"
211 assert_equal(ct[""], "empty")
212 assert_equal(#ct, 1)
213 end)
214
215 test("Numeric values", function()
216 local ct = caseless_table.create()
217 ct["num"] = 42
218 assert_equal(ct["num"], 42)
219 end)
220
221 test("Boolean values", function()
222 local ct = caseless_table.create()
223 ct["flag"] = true
224 assert_equal(ct["flag"], true)
225 end)
226
227 test("Long key (>256 chars)", function()
228 local long_key = string.rep("x", 300)
229 local ct = caseless_table.create()
230 ct[long_key] = "long"
231 assert_equal(ct[long_key], "long")
232 assert_equal(ct[string.upper(long_key)], "long")
233 end)
234
235 test("Method names are not exposed as keys", function()
236 local ct = caseless_table.from_table({
237 ["normal"] = "value",
238 })
239 -- __gc, __tostring etc. should not leak through __index
240 assert_nil(ct["__gc"])
241 assert_nil(ct["__tostring"])
242 assert_nil(ct["__len"])
243 assert_nil(ct["__newindex"])
244 end)
245
246 test("Methods are callable via method syntax", function()
247 local ct = caseless_table.from_table({
248 ["key"] = "value",
249 })
250 -- These should work as methods
251 assert_true(ct:has_key("key"))
252 assert_not_nil(ct:to_table())
253 assert_not_nil(ct:get_all("key"))
254 end)
255 end)
256 end)