Skip to content

Commit b4460a2

Browse files
committed
spec/cookie_spec: Add tests for :remove()
1 parent f611bf0 commit b4460a2

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

spec/cookie_spec.lua

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,96 @@ describe("cookie module", function()
198198
assert.same("", s:lookup("example.com", "/", true, true, false, "other.com", false))
199199
end)
200200
end)
201+
it("cleans up", function()
202+
local s = http_cookie.new_store()
203+
assert.truthy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("foo=foo; Expires=Wed, 09 Jun 2021 10:18:14 GMT")))
204+
assert.same("foo", s:get("example.com", "/", "foo"))
205+
s.time = function() return 9876543210 end -- set time to something after the expiry
206+
s:clean()
207+
assert.same(nil, s:get("example.com", "/", "foo"))
208+
end)
209+
describe(":remove()", function()
210+
it("can remove cookies by domain", function()
211+
local s = http_cookie.new_store()
212+
-- Try remove on empty store
213+
s:remove("example.com")
214+
215+
assert.truthy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("foo=foo")))
216+
assert.truthy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("foo=other; Path=/subpath")))
217+
assert.truthy(s:store("other.com", "/", true, true, nil, http_cookie.parse_setcookie("bar=bar")))
218+
assert.same("foo", s:get("example.com", "/", "foo"))
219+
assert.same("other", s:get("example.com", "/subpath", "foo"))
220+
assert.same("bar", s:get("other.com", "/", "bar"))
221+
222+
s:remove("example.com")
223+
assert.same(nil, s:get("example.com", "/", "foo"))
224+
assert.same(nil, s:get("example.com", "/subpath", "foo"))
225+
assert.same("bar", s:get("other.com", "/", "bar"))
226+
end)
227+
it("can remove cookies by path", function()
228+
local s = http_cookie.new_store()
229+
-- Try remove on empty store
230+
s:remove("example.com", "/")
231+
232+
assert.truthy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("foo=foo")))
233+
assert.truthy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("foo=other; Path=/subpath")))
234+
assert.truthy(s:store("other.com", "/", true, true, nil, http_cookie.parse_setcookie("bar=bar")))
235+
assert.truthy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("qux=qux")))
236+
assert.same("foo", s:get("example.com", "/", "foo"))
237+
assert.same("other", s:get("example.com", "/subpath", "foo"))
238+
assert.same("bar", s:get("other.com", "/", "bar"))
239+
assert.same("qux", s:get("example.com", "/", "qux"))
240+
241+
-- Remove all names under "/" path
242+
s:remove("example.com", "/")
243+
assert.same(nil, s:get("example.com", "/", "foo"))
244+
assert.same("other", s:get("example.com", "/subpath", "foo"))
245+
assert.same("bar", s:get("other.com", "/", "bar"))
246+
assert.same(nil, s:get("example.com", "/", "qux"))
247+
248+
-- Remove last path in domain (making domain empty)
249+
s:remove("example.com", "/subpath")
250+
assert.same(nil, s:get("example.com", "/", "foo"))
251+
assert.same(nil, s:get("example.com", "/subpath", "foo"))
252+
assert.same("bar", s:get("other.com", "/", "bar"))
253+
assert.same(nil, s:get("example.com", "/", "qux"))
254+
end)
255+
it("can remove cookies by name", function()
256+
local s = http_cookie.new_store()
257+
-- Try remove on empty store
258+
s:remove("example.com", "/", "foo")
259+
260+
assert.truthy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("foo=foo")))
261+
assert.truthy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("foo=other; Path=/subpath")))
262+
assert.truthy(s:store("other.com", "/", true, true, nil, http_cookie.parse_setcookie("bar=bar")))
263+
assert.truthy(s:store("example.com", "/", true, true, nil, http_cookie.parse_setcookie("qux=qux")))
264+
assert.same("foo", s:get("example.com", "/", "foo"))
265+
assert.same("other", s:get("example.com", "/subpath", "foo"))
266+
assert.same("bar", s:get("other.com", "/", "bar"))
267+
assert.same("qux", s:get("example.com", "/", "qux"))
268+
269+
-- Remove just one name
270+
s:remove("example.com", "/", "foo")
271+
assert.same(nil, s:get("example.com", "/", "foo"))
272+
assert.same("other", s:get("example.com", "/subpath", "foo"))
273+
assert.same("bar", s:get("other.com", "/", "bar"))
274+
assert.same("qux", s:get("example.com", "/", "qux"))
275+
276+
-- Remove last name in path (making path empty)
277+
s:remove("example.com", "/", "qux")
278+
assert.same(nil, s:get("example.com", "/", "foo"))
279+
assert.same("other", s:get("example.com", "/subpath", "foo"))
280+
assert.same("bar", s:get("other.com", "/", "bar"))
281+
assert.same(nil, s:get("example.com", "/", "qux"))
282+
283+
-- Remove last name in domain (making domain empty)
284+
s:remove("example.com", "/subpath", "foo")
285+
assert.same(nil, s:get("example.com", "/", "foo"))
286+
assert.same(nil, s:get("example.com", "/subpath", "foo"))
287+
assert.same("bar", s:get("other.com", "/", "bar"))
288+
assert.same(nil, s:get("example.com", "/", "qux"))
289+
end)
290+
end)
201291
it("can bake cookies", function()
202292
assert.same("foo=bar", http_cookie.bake("foo", "bar"))
203293
assert.same("foo=bar; Max-Age=0", http_cookie.bake("foo", "bar", -math.huge))

0 commit comments

Comments
 (0)