diff --git a/spec/std/http/cookie_spec.cr b/spec/std/http/cookie_spec.cr index 1a29a3f56754..f501da15bb84 100644 --- a/spec/std/http/cookie_spec.cr +++ b/spec/std/http/cookie_spec.cr @@ -70,6 +70,15 @@ module HTTP end end + it "#destroy" do + cookie = HTTP::Cookie.new("hello", "world") + cookie.destroy + + cookie.value.empty?.should be_true + cookie.expired?.should be_true + cookie.max_age.should eq(Time::Span.zero) + 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..b2c0932725ce 100644 --- a/src/http/cookie.cr +++ b/src/http/cookie.cr @@ -192,6 +192,16 @@ 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 = Time::UNIX_EPOCH + self.max_age = Time::Span.zero + end + # :nodoc: module Parser module Regex