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

Update cookie.rb to handle situations when expires is a DateTime object #52

Merged
merged 2 commits into from
Dec 5, 2024
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
4 changes: 3 additions & 1 deletion lib/http/cookie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class HTTP::Cookie

# The Expires attribute value as a Time object.
#
# The setter method accepts a Time object, a string representation
# The setter method accepts a Time / DateTime object, a string representation
# of date/time that Time.parse can understand, or `nil`.
#
# Setting this value resets #max_age to nil. When #max_age is
Expand Down Expand Up @@ -493,6 +493,8 @@ def expires
def expires= t
case t
when nil, Time
when DateTime
t = t.to_time
else
t = Time.parse(t)
end
Expand Down
16 changes: 15 additions & 1 deletion test/test_http_cookie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -717,8 +717,22 @@ def test_compare
end

def test_expiration
cookie = HTTP::Cookie.new(cookie_values)
expires = Time.now + 86400
cookie = HTTP::Cookie.new(cookie_values(expires: expires))

assert_equal(expires, cookie.expires)
assert_equal false, cookie.expired?
assert_equal true, cookie.expired?(cookie.expires + 1)
assert_equal false, cookie.expired?(cookie.expires - 1)
cookie.expire!
assert_equal true, cookie.expired?
end

def test_expiration_using_datetime
expires = DateTime.now + 1
cookie = HTTP::Cookie.new(cookie_values(expires: expires))

assert_equal(expires.to_time, cookie.expires)
assert_equal false, cookie.expired?
assert_equal true, cookie.expired?(cookie.expires + 1)
assert_equal false, cookie.expired?(cookie.expires - 1)
Expand Down
Loading