mirror of
https://github.com/samsonjs/http-cookie.git
synced 2026-03-25 08:55:53 +00:00
Update cookie.rb to handle situations when expires is a DateTime object (#52)
This commit is contained in:
commit
e4e66bdf0a
2 changed files with 18 additions and 2 deletions
|
|
@ -89,7 +89,7 @@ class HTTP::Cookie
|
||||||
|
|
||||||
# The Expires attribute value as a Time object.
|
# 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`.
|
# of date/time that Time.parse can understand, or `nil`.
|
||||||
#
|
#
|
||||||
# Setting this value resets #max_age to nil. When #max_age is
|
# Setting this value resets #max_age to nil. When #max_age is
|
||||||
|
|
@ -493,6 +493,8 @@ class HTTP::Cookie
|
||||||
def expires= t
|
def expires= t
|
||||||
case t
|
case t
|
||||||
when nil, Time
|
when nil, Time
|
||||||
|
when DateTime
|
||||||
|
t = t.to_time
|
||||||
else
|
else
|
||||||
t = Time.parse(t)
|
t = Time.parse(t)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -717,8 +717,22 @@ class TestHTTPCookie < Test::Unit::TestCase
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_expiration
|
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 false, cookie.expired?
|
||||||
assert_equal true, cookie.expired?(cookie.expires + 1)
|
assert_equal true, cookie.expired?(cookie.expires + 1)
|
||||||
assert_equal false, cookie.expired?(cookie.expires - 1)
|
assert_equal false, cookie.expired?(cookie.expires - 1)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue