From 4f008895c23307b7a8553123d59acaad9112806e Mon Sep 17 00:00:00 2001 From: Abdullah Alhusaini <44743015+a-alhusaini@users.noreply.github.com> Date: Tue, 17 Dec 2024 21:59:27 +0300 Subject: [PATCH] Add `HTTP::Cookie#expire` (#14819) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Johannes Müller --- spec/std/http/cookie_spec.cr | 9 +++++++++ src/http/cookie.cr | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/spec/std/http/cookie_spec.cr b/spec/std/http/cookie_spec.cr index 55183c48cbe5..7bc13080f60e 100644 --- a/spec/std/http/cookie_spec.cr +++ b/spec/std/http/cookie_spec.cr @@ -80,6 +80,15 @@ module HTTP end end + it "#expire" do + cookie = HTTP::Cookie.new("hello", "world") + cookie.expire + + 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 56d8800848d7..8a9a29855318 100644 --- a/src/http/cookie.cr +++ b/src/http/cookie.cr @@ -243,6 +243,24 @@ module HTTP end end + # Expires the cookie. + # + # Causes the cookie to be destroyed. Sets the value to the empty string and + # expires its lifetime. + # + # ``` + # cookie = HTTP::Cookie.new("hello", "world") + # cookie.expire + # + # cookie.value # => "" + # cookie.expired? # => true + # ``` + def expire + self.value = "" + self.expires = Time::UNIX_EPOCH + self.max_age = Time::Span.zero + end + # :nodoc: module Parser module Regex