Skip to content

Commit

Permalink
Add HTTP::Cookie#expire (#14819)
Browse files Browse the repository at this point in the history
Co-authored-by: Johannes Müller <straightshoota@gmail.com>
  • Loading branch information
a-alhusaini and straight-shoota authored Dec 17, 2024
1 parent 25086b9 commit 4f00889
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions spec/std/http/cookie_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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", "")
Expand Down
18 changes: 18 additions & 0 deletions src/http/cookie.cr
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 4f00889

Please sign in to comment.