diff --git a/lib/http/cookie_jar.rb b/lib/http/cookie_jar.rb index 72fac57..e476dd3 100644 --- a/lib/http/cookie_jar.rb +++ b/lib/http/cookie_jar.rb @@ -46,8 +46,15 @@ class HTTP::CookieJar }.sort end - def empty?(url) - cookies(url).empty? + # Tests if the jar is empty. If url is given, tests if there is no + # cookie for the URL. + def empty?(url = nil) + if url + each(url) { return false } + return true + else + @jar.empty? + end end # Iterate over cookies. If +uri+ is given, cookies not for the diff --git a/test/test_http_cookie_jar.rb b/test/test_http_cookie_jar.rb index 65f2711..75cd2a8 100644 --- a/test/test_http_cookie_jar.rb +++ b/test/test_http_cookie_jar.rb @@ -25,6 +25,15 @@ class TestHTTPCookieJar < Test::Unit::TestCase }.merge(options) end + def test_empty? + assert_equal true, @jar.empty? + cookie = HTTP::Cookie.new(cookie_values) + @jar.add(cookie) + assert_equal false, @jar.empty? + assert_equal false, @jar.empty?('http://rubyforge.org/') + assert_equal true, @jar.empty?('http://example.local/') + end + def test_two_cookies_same_domain_and_name_different_paths url = URI 'http://rubyforge.org/'