From c012534bfb38f4100931dd7b9a5618c39009c026 Mon Sep 17 00:00:00 2001 From: Abdullah Alhusaini Date: Mon, 22 Jul 2024 00:52:29 +0300 Subject: [PATCH 1/4] added HTTP::Cookie#destroy. It expires cookie and sets its value to blank --- spec/std/http/cookie_spec.cr | 8 ++++++++ src/http/cookie.cr | 9 +++++++++ 2 files changed, 17 insertions(+) diff --git a/spec/std/http/cookie_spec.cr b/spec/std/http/cookie_spec.cr index 1a29a3f56754..a4687fd9db03 100644 --- a/spec/std/http/cookie_spec.cr +++ b/spec/std/http/cookie_spec.cr @@ -70,6 +70,14 @@ module HTTP end end + it "#destroy" do + cookie = HTTP::Cookie.new("hello", "world") + cookie.destroy + + assert cookie.value.empty? + assert cookie.expired? + end + describe "#name=" do it "raises on invalid name" do cookie = HTTP::Cookie.new("x", "") diff --git a/src/http/cookie.cr b/src/http/cookie.cr index 8138249aa830..a4fe6199eeda 100644 --- a/src/http/cookie.cr +++ b/src/http/cookie.cr @@ -192,6 +192,15 @@ module HTTP end end + # tell the browser to delete the cookie. + # Set its value to an empty string + # and set its expiration time to the past + # Browsers delete cookies that have expired. + def destroy + self.value = "" + self.expires = 5.minutes.ago + end + # :nodoc: module Parser module Regex From 1981575105b12a3d96b141a8c403097f79f41f76 Mon Sep 17 00:00:00 2001 From: Abdullah Alhusaini Date: Mon, 22 Jul 2024 01:04:42 +0300 Subject: [PATCH 2/4] fix invalid assertion --- spec/std/http/cookie_spec.cr | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/std/http/cookie_spec.cr b/spec/std/http/cookie_spec.cr index a4687fd9db03..50c37912f14e 100644 --- a/spec/std/http/cookie_spec.cr +++ b/spec/std/http/cookie_spec.cr @@ -74,8 +74,8 @@ module HTTP cookie = HTTP::Cookie.new("hello", "world") cookie.destroy - assert cookie.value.empty? - assert cookie.expired? + cookie.value.empty?.should be_true + cookie.expired?.should be_true end describe "#name=" do From 564f926351b79bb4ee7950719326306b4153736f Mon Sep 17 00:00:00 2001 From: Abdullah Alhusaini Date: Tue, 23 Jul 2024 21:31:58 +0300 Subject: [PATCH 3/4] set cookie expiry to unix epoch. Set cookie max_age to zero to ensure browser deletion --- spec/std/http/cookie_spec.cr | 1 + src/http/cookie.cr | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/std/http/cookie_spec.cr b/spec/std/http/cookie_spec.cr index 50c37912f14e..f501da15bb84 100644 --- a/spec/std/http/cookie_spec.cr +++ b/spec/std/http/cookie_spec.cr @@ -76,6 +76,7 @@ module HTTP cookie.value.empty?.should be_true cookie.expired?.should be_true + cookie.max_age.should eq(Time::Span.zero) end describe "#name=" do diff --git a/src/http/cookie.cr b/src/http/cookie.cr index a4fe6199eeda..94d0afc0039e 100644 --- a/src/http/cookie.cr +++ b/src/http/cookie.cr @@ -198,7 +198,8 @@ module HTTP # Browsers delete cookies that have expired. def destroy self.value = "" - self.expires = 5.minutes.ago + self.expires = Time::UNIx_EPOCH + self.max_age = Time::Span.zero end # :nodoc: From c59233083c5839ebf8fd6a209c816f46a0f5c214 Mon Sep 17 00:00:00 2001 From: Abdullah Alhusaini Date: Tue, 23 Jul 2024 21:43:26 +0300 Subject: [PATCH 4/4] fixed typo --- src/http/cookie.cr | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/http/cookie.cr b/src/http/cookie.cr index 94d0afc0039e..b2c0932725ce 100644 --- a/src/http/cookie.cr +++ b/src/http/cookie.cr @@ -198,7 +198,7 @@ module HTTP # Browsers delete cookies that have expired. def destroy self.value = "" - self.expires = Time::UNIx_EPOCH + self.expires = Time::UNIX_EPOCH self.max_age = Time::Span.zero end