diff --git a/lib/http/cookie_jar.rb b/lib/http/cookie_jar.rb index 9102eef..ae6bf99 100644 --- a/lib/http/cookie_jar.rb +++ b/lib/http/cookie_jar.rb @@ -94,7 +94,7 @@ class HTTP::CookieJar begin cookie.acceptable? rescue RuntimeError => e - raise ArgumentError, e.message + raise ArgumentError, e.message end self end diff --git a/test/helper.rb b/test/helper.rb index 87dd8a0..6b463ad 100644 --- a/test/helper.rb +++ b/test/helper.rb @@ -47,3 +47,9 @@ end def test_file(filename) File.expand_path(filename, File.dirname(__FILE__)) end + +def sleep_until(time) + if (s = time - Time.now) > 0 + sleep s + end +end diff --git a/test/test_http_cookie_jar.rb b/test/test_http_cookie_jar.rb index b7b6dcc..c25985c 100644 --- a/test/test_http_cookie_jar.rb +++ b/test/test_http_cookie_jar.rb @@ -749,6 +749,46 @@ module TestHTTPCookieJar assert_equal %w[Akinori Japan], cookies.map { |c| c.value } assert_equal %w[Japan Akinori], @jar.to_a.sort_by { |c| c.name }.map { |c| c.value } end + + def test_expire_by_each_and_cleanup + uri = URI('http://www.example.org/') + + ts = Time.now.to_f + if ts % 1 > 0.5 + sleep 0.5 + ts += 0.5 + end + expires = Time.at(ts.floor) + time = expires + + if mozilla_store? + # MozillaStore only has the time precision of seconds. + time = expires + expires -= 1 + end + + 0.upto(2) { |i| + c = HTTP::Cookie.new('Foo%d' % (3 - i), 'Bar', :expires => expires + i, :origin => uri) + @jar << c + @jar2 << c + } + + assert_equal %w[Foo1 Foo2], @jar.cookies.map(&:name) + assert_equal %w[Foo1 Foo2], @jar2.cookies(uri).map(&:name) + + sleep_until time + 1 + + assert_equal %w[Foo1], @jar.cookies.map(&:name) + assert_equal %w[Foo1], @jar2.cookies(uri).map(&:name) + + sleep_until time + 2 + + @jar.cleanup + @jar2.cleanup + + assert_send [@jar, :empty?] + assert_send [@jar2, :empty?] + end end class WithHashStore < Test::Unit::TestCase @@ -770,6 +810,20 @@ module TestHTTPCookieJar } end + def test_clone + jar = @jar.clone + assert_not_send [ + @jar.store, + :equal?, + jar.store + ] + assert_not_send [ + @jar.store.instance_variable_get(:@jar), + :equal?, + jar.store.instance_variable_get(:@jar) + ] + assert_equal @jar.cookies, jar.cookies + end end class WithMozillaStore < Test::Unit::TestCase @@ -789,6 +843,12 @@ module TestHTTPCookieJar jar.delete(HTTP::Cookie.new("name", :domain => 'rubyforge.org')) end + def test_clone + assert_raises(TypeError) { + @jar.clone + } + end + def test_close add_and_delete(@jar)