From d2890896e73d2b54652d5ea948d10651d36aefbd Mon Sep 17 00:00:00 2001 From: Luke Hill <20105237+luke-hill@users.noreply.github.com> Date: Mon, 2 Dec 2024 17:41:53 +0000 Subject: [PATCH 1/2] Update cookie.rb to handle situations when expires is a DateTime object The standard Selenium WebDriver response is to return an object which has an expiry in datetime format. In order to most effectively work with Selenium, and to provide the smallest barrier possible, co-erce the DateTime object into a Time object and then store it as a HTTP Cookie --- lib/http/cookie.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/http/cookie.rb b/lib/http/cookie.rb index be06148..6282e3c 100644 --- a/lib/http/cookie.rb +++ b/lib/http/cookie.rb @@ -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 @@ -493,6 +493,8 @@ class HTTP::Cookie def expires= t case t when nil, Time + when DateTime + t = t.to_time else t = Time.parse(t) end From fc422f23784d5c31725d1c46aff170687d6de052 Mon Sep 17 00:00:00 2001 From: Mike Dalessio Date: Thu, 5 Dec 2024 16:46:09 -0500 Subject: [PATCH 2/2] test: passing DateTime to Cookie#expires= --- test/test_http_cookie.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/test_http_cookie.rb b/test/test_http_cookie.rb index 6f8ce87..a01b46a 100644 --- a/test/test_http_cookie.rb +++ b/test/test_http_cookie.rb @@ -717,8 +717,22 @@ class TestHTTPCookie < Test::Unit::TestCase 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)