Fix saving in the cookies.txt format. Expires values were broken.

Remove duplicate and incomplete tests.
This commit is contained in:
Akinori MUSHA 2013-03-15 04:06:33 +09:00
parent 84d375e3b7
commit 91193dace3
2 changed files with 19 additions and 58 deletions

View file

@ -52,7 +52,7 @@ class HTTP::Cookie
attr_reader :name, :domain, :path, :origin attr_reader :name, :domain, :path, :origin
attr_accessor :secure, :httponly, :value, :version attr_accessor :secure, :httponly, :value, :version
attr_reader :domain_name attr_reader :domain_name, :expires
attr_accessor :comment, :max_age attr_accessor :comment, :max_age
attr_accessor :session attr_accessor :session
@ -334,16 +334,17 @@ class HTTP::Cookie
end end
def expires=(t) def expires=(t)
@expires = t && (t.is_a?(Time) ? t.httpdate : t.to_s) case t
end when nil, Time
@expires = t
def expires else
@expires && Time.parse(@expires) @expires = Time.parse(t)
end
end end
def expired? def expired?
return false unless expires return false unless @expires
Time.now > expires Time.now > @expires
end end
alias secure? secure alias secure? secure
@ -400,7 +401,7 @@ class HTTP::Cookie
@for_domain ? True : False, @for_domain ? True : False,
@path, @path,
@secure ? True : False, @secure ? True : False,
@expires.to_i.to_s, @expires.to_i,
@name, @name,
@value @value
].join("\t") << linefeed ].join("\t") << linefeed

View file

@ -18,7 +18,7 @@ class TestHTTPCookieJar < Test::Unit::TestCase
:name => 'Foo', :name => 'Foo',
:value => 'Bar', :value => 'Bar',
:path => '/', :path => '/',
:expires => Time.now + (10 * 86400), :expires => Time.at(Time.now.to_i + 10 * 86400), # to_i is important here
:for_domain => true, :for_domain => true,
:domain => 'rubyforge.org', :domain => 'rubyforge.org',
:origin => 'http://rubyforge.org/' :origin => 'http://rubyforge.org/'
@ -335,18 +335,23 @@ class TestHTTPCookieJar < Test::Unit::TestCase
end end
def test_save_cookies_cookiestxt def test_save_and_read_cookiestxt
url = URI 'http://rubyforge.org/foo/' url = URI 'http://rubyforge.org/foo/'
# Add one cookie with an expiration date in the future # Add one cookie with an expiration date in the future
cookie = HTTP::Cookie.new(cookie_values) cookie = HTTP::Cookie.new(cookie_values)
expires = cookie.expires
s_cookie = HTTP::Cookie.new(cookie_values(:name => 'Bar', s_cookie = HTTP::Cookie.new(cookie_values(:name => 'Bar',
:expires => nil, :expires => nil,
:session => true)) :session => true))
cookie2 = HTTP::Cookie.new(cookie_values(:name => 'Baz',
:value => 'Foo#Baz',
:path => '/foo/',
:for_domain => false))
@jar.add(cookie) @jar.add(cookie)
@jar.add(s_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) assert_equal(3, @jar.cookies(url).length)
@ -361,6 +366,7 @@ class TestHTTPCookieJar < Test::Unit::TestCase
case cookie.name case cookie.name
when 'Foo' when 'Foo'
assert_equal 'Bar', cookie.value assert_equal 'Bar', cookie.value
assert_equal expires, cookie.expires
assert_equal 'rubyforge.org', cookie.domain assert_equal 'rubyforge.org', cookie.domain
assert_equal true, cookie.for_domain assert_equal true, cookie.for_domain
assert_equal '/', cookie.path assert_equal '/', cookie.path
@ -463,52 +469,6 @@ class TestHTTPCookieJar < Test::Unit::TestCase
assert_equal(0, @jar.cookies(url).length) assert_equal(0, @jar.cookies(url).length)
end 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 def test_ssl_cookies
# thanks to michal "ocher" ochman for reporting the bug responsible for this test. # thanks to michal "ocher" ochman for reporting the bug responsible for this test.
values = cookie_values(:expires => nil) values = cookie_values(:expires => nil)