test/tc_cookie_jar.rb in mechanize-0.5.4 vs test/tc_cookie_jar.rb in mechanize-0.6.0

- old
+ new

@@ -5,10 +5,18 @@ require 'uri' require 'test_includes' require 'fileutils' class CookieJarTest < Test::Unit::TestCase + def cookie_from_hash(hash) + c = WWW::Mechanize::Cookie.new(hash[:name], hash[:value]) + hash.each { |k,v| + next if k == :name || k == :value + c.send("#{k}=", v) + } + c + end def test_add_future_cookies values = { :name => 'Foo', :value => 'Bar', :path => '/', :expires => Time.now + (10 * 86400), @@ -18,16 +26,16 @@ jar = WWW::Mechanize::CookieJar.new assert_equal(0, jar.cookies(url).length) # Add one cookie with an expiration date in the future - cookie = WWW::Mechanize::Cookie.new(values) - jar.add(cookie) + cookie = cookie_from_hash(values) + jar.add(url, cookie) assert_equal(1, jar.cookies(url).length) # Add the same cookie, and we should still only have one - jar.add(WWW::Mechanize::Cookie.new(values)) + jar.add(url, cookie_from_hash(values)) assert_equal(1, jar.cookies(url).length) # Make sure we can get the cookie from different paths assert_equal(1, jar.cookies(URI.parse('http://rubyforge.org/login')).length) @@ -46,16 +54,16 @@ jar = WWW::Mechanize::CookieJar.new assert_equal(0, jar.cookies(url).length) # Add one cookie with an expiration date in the future - cookie = WWW::Mechanize::Cookie.new(values) - jar.add(cookie) + cookie = cookie_from_hash(values) + jar.add(url, cookie) assert_equal(1, jar.cookies(url).length) # Add the same cookie, and we should still only have one - jar.add(WWW::Mechanize::Cookie.new(values.merge( :name => 'Baz' ))) + jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' ))) assert_equal(2, jar.cookies(url).length) # Make sure we can get the cookie from different paths assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length) @@ -74,13 +82,13 @@ jar = WWW::Mechanize::CookieJar.new assert_equal(0, jar.cookies(url).length) # Add one cookie with an expiration date in the future - cookie = WWW::Mechanize::Cookie.new(values) - jar.add(cookie) - jar.add(WWW::Mechanize::Cookie.new(values.merge( :name => 'Baz' ))) + cookie = cookie_from_hash(values) + jar.add(url, cookie) + jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' ))) assert_equal(2, jar.cookies(url).length) jar.clear! assert_equal(0, jar.cookies(url).length) @@ -97,13 +105,13 @@ jar = WWW::Mechanize::CookieJar.new assert_equal(0, jar.cookies(url).length) # Add one cookie with an expiration date in the future - cookie = WWW::Mechanize::Cookie.new(values) - jar.add(cookie) - jar.add(WWW::Mechanize::Cookie.new(values.merge( :name => 'Baz' ))) + cookie = cookie_from_hash(values) + jar.add(url, cookie) + jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' ))) assert_equal(2, jar.cookies(url).length) jar.save_as("cookies.yml") jar.clear! assert_equal(0, jar.cookies(url).length) @@ -124,27 +132,27 @@ jar = WWW::Mechanize::CookieJar.new assert_equal(0, jar.cookies(url).length) # Add one cookie with an expiration date in the future - cookie = WWW::Mechanize::Cookie.new(values) - jar.add(cookie) + cookie = cookie_from_hash(values) + jar.add(url, cookie) assert_equal(1, jar.cookies(url).length) # Add a second cookie - jar.add(WWW::Mechanize::Cookie.new(values.merge( :name => 'Baz' ))) + jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' ))) assert_equal(2, jar.cookies(url).length) # Make sure we can get the cookie from different paths assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length) # Expire the first cookie - jar.add(WWW::Mechanize::Cookie.new(values.merge( :expires => Time.now - (10 * 86400)))) + jar.add(url, cookie_from_hash(values.merge( :expires => Time.now - (10 * 86400)))) assert_equal(1, jar.cookies(url).length) # Expire the second cookie - jar.add(WWW::Mechanize::Cookie.new(values.merge( :name => 'Baz', + jar.add(url, cookie_from_hash(values.merge( :name => 'Baz', :expires => Time.now - (10 * 86400)))) assert_equal(0, jar.cookies(url).length) end def test_session_cookies @@ -158,37 +166,37 @@ jar = WWW::Mechanize::CookieJar.new assert_equal(0, jar.cookies(url).length) # Add one cookie with an expiration date in the future - cookie = WWW::Mechanize::Cookie.new(values) - jar.add(cookie) + cookie = cookie_from_hash(values) + jar.add(url, cookie) assert_equal(1, jar.cookies(url).length) # Add a second cookie - jar.add(WWW::Mechanize::Cookie.new(values.merge( :name => 'Baz' ))) + jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' ))) assert_equal(2, jar.cookies(url).length) # Make sure we can get the cookie from different paths assert_equal(2, jar.cookies(URI.parse('http://rubyforge.org/login')).length) # Expire the first cookie - jar.add(WWW::Mechanize::Cookie.new(values.merge( :expires => Time.now - (10 * 86400)))) + jar.add(url, cookie_from_hash(values.merge( :expires => Time.now - (10 * 86400)))) assert_equal(1, jar.cookies(url).length) # Expire the second cookie - jar.add(WWW::Mechanize::Cookie.new(values.merge( :name => 'Baz', + jar.add(url, cookie_from_hash(values.merge( :name => 'Baz', :expires => Time.now - (10 * 86400)))) assert_equal(0, jar.cookies(url).length) # When given a URI with a blank path, CookieJar#cookies should return # cookies with the path '/': url = URI.parse('http://rubyforge.org') assert_equal '', url.path assert_equal(0, jar.cookies(url).length) # Now add a cookie with the path set to '/': - jar.add(WWW::Mechanize::Cookie.new(values.merge( :name => 'has_root_path', + jar.add(url, cookie_from_hash(values.merge( :name => 'has_root_path', :path => '/'))) assert_equal(1, jar.cookies(url).length) end def test_paths @@ -202,27 +210,27 @@ jar = WWW::Mechanize::CookieJar.new assert_equal(0, jar.cookies(url).length) # Add one cookie with an expiration date in the future - cookie = WWW::Mechanize::Cookie.new(values) - jar.add(cookie) + cookie = cookie_from_hash(values) + jar.add(url, cookie) assert_equal(1, jar.cookies(url).length) # Add a second cookie - jar.add(WWW::Mechanize::Cookie.new(values.merge( :name => 'Baz' ))) + jar.add(url, cookie_from_hash(values.merge( :name => 'Baz' ))) assert_equal(2, jar.cookies(url).length) # Make sure we don't get the cookie in a different path assert_equal(0, jar.cookies(URI.parse('http://rubyforge.org/hello')).length) assert_equal(0, jar.cookies(URI.parse('http://rubyforge.org/')).length) # Expire the first cookie - jar.add(WWW::Mechanize::Cookie.new(values.merge( :expires => Time.now - (10 * 86400)))) + jar.add(url, cookie_from_hash(values.merge( :expires => Time.now - (10 * 86400)))) assert_equal(1, jar.cookies(url).length) # Expire the second cookie - jar.add(WWW::Mechanize::Cookie.new(values.merge( :name => 'Baz', + jar.add(url, cookie_from_hash(values.merge( :name => 'Baz', :expires => Time.now - (10 * 86400)))) assert_equal(0, jar.cookies(url).length) end end