Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add HTTP::Cookie#expire #14819

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions spec/std/http/cookie_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,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 @@ -192,6 +192,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
Loading