From fd7450717af77610adb5a950901a3d0bfce3f873 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Fri, 15 Mar 2013 00:11:31 +0900 Subject: [PATCH] Implement HTTP::CookieJar#empty? without an argument. --- lib/http/cookie_jar.rb | 11 +++++++++-- test/test_http_cookie_jar.rb | 9 +++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) 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/'