diff --git a/lib/http/cookie.rb b/lib/http/cookie.rb index c76524f..fb2056e 100644 --- a/lib/http/cookie.rb +++ b/lib/http/cookie.rb @@ -52,7 +52,7 @@ class HTTP::Cookie attr_reader :name, :domain, :path, :origin attr_accessor :secure, :httponly, :value, :version - attr_reader :domain_name + attr_reader :domain_name, :expires attr_accessor :comment, :max_age attr_accessor :session @@ -334,16 +334,17 @@ class HTTP::Cookie end def expires=(t) - @expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s) - end - - def expires - @expires && Time.parse(@expires) + case t + when nil, Time + @expires = t + else + @expires = Time.parse(t) + end end def expired? - return false unless expires - Time.now > expires + return false unless @expires + Time.now > @expires end alias secure? secure @@ -400,7 +401,7 @@ class HTTP::Cookie @for_domain ? True : False, @path, @secure ? True : False, - @expires.to_i.to_s, + @expires.to_i, @name, @value ].join("\t") << linefeed diff --git a/test/test_http_cookie_jar.rb b/test/test_http_cookie_jar.rb index 3a0a818..65f2711 100644 --- a/test/test_http_cookie_jar.rb +++ b/test/test_http_cookie_jar.rb @@ -18,7 +18,7 @@ class TestHTTPCookieJar < Test::Unit::TestCase :name => 'Foo', :value => 'Bar', :path => '/', - :expires => Time.now + (10 * 86400), + :expires => Time.at(Time.now.to_i + 10 * 86400), # to_i is important here :for_domain => true, :domain => 'rubyforge.org', :origin => 'http://rubyforge.org/' @@ -335,18 +335,23 @@ class TestHTTPCookieJar < Test::Unit::TestCase end - def test_save_cookies_cookiestxt + def test_save_and_read_cookiestxt url = URI 'http://rubyforge.org/foo/' # Add one cookie with an expiration date in the future cookie = HTTP::Cookie.new(cookie_values) + expires = cookie.expires s_cookie = HTTP::Cookie.new(cookie_values(:name => 'Bar', :expires => nil, :session => true)) + cookie2 = HTTP::Cookie.new(cookie_values(:name => 'Baz', + :value => 'Foo#Baz', + :path => '/foo/', + :for_domain => false)) @jar.add(cookie) @jar.add(s_cookie) - @jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz', :value => 'Foo#Baz', :path => '/foo/', :for_domain => false))) + @jar.add(cookie2) assert_equal(3, @jar.cookies(url).length) @@ -361,6 +366,7 @@ class TestHTTPCookieJar < Test::Unit::TestCase case cookie.name when 'Foo' assert_equal 'Bar', cookie.value + assert_equal expires, cookie.expires assert_equal 'rubyforge.org', cookie.domain assert_equal true, cookie.for_domain assert_equal '/', cookie.path @@ -463,52 +469,6 @@ class TestHTTPCookieJar < Test::Unit::TestCase assert_equal(0, @jar.cookies(url).length) end - def test_save_and_read_cookiestxt - url = URI 'http://rubyforge.org/' - - # Add one cookie with an expiration date in the future - cookie = HTTP::Cookie.new(cookie_values) - @jar.add(cookie) - @jar.add(HTTP::Cookie.new(cookie_values(:name => 'Baz'))) - assert_equal(2, @jar.cookies(url).length) - - in_tmpdir do - @jar.save_as("cookies.txt", :cookiestxt) - @jar.clear - - @jar.load("cookies.txt", :cookiestxt) - end - - assert_equal(2, @jar.cookies(url).length) - end - - def test_save_and_read_cookiestxt_with_session_cookies - url = URI 'http://rubyforge.org/' - - @jar.add(HTTP::Cookie.new(cookie_values(:expires => nil))) - - in_tmpdir do - @jar.save_as("cookies.txt", :cookiestxt) - @jar.clear - - @jar.load("cookies.txt", :cookiestxt) - end - - assert_equal(1, @jar.cookies(url).length) - assert_nil @jar.cookies(url).first.expires - end - - def test_save_and_read_expired_cookies - url = URI 'http://rubyforge.org/' - - @jar.jar['rubyforge.org'] = {} - - - @jar.add HTTP::Cookie.new(cookie_values) - - # HACK no asertion - end - def test_ssl_cookies # thanks to michal "ocher" ochman for reporting the bug responsible for this test. values = cookie_values(:expires => nil)