Add tests for CookieJar#clone, #cleanup, expiration by #each and #empty?.

This commit is contained in:
Akinori MUSHA 2013-04-16 08:52:49 +09:00
parent 5bdb8f41ae
commit 2bb7485460
3 changed files with 67 additions and 1 deletions

View file

@ -94,7 +94,7 @@ class HTTP::CookieJar
begin
cookie.acceptable?
rescue RuntimeError => e
raise ArgumentError, e.message
raise ArgumentError, e.message
end
self
end

View file

@ -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

View file

@ -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)