From f3b8abdd8e86cd541a6ec1008373daa2b670ec24 Mon Sep 17 00:00:00 2001 From: Akinori MUSHA Date: Fri, 12 Apr 2013 01:19:39 +0900 Subject: [PATCH] Add CookieJar#delete(). --- lib/http/cookie_jar.rb | 11 +++++++++++ test/test_http_cookie_jar.rb | 27 ++++++++++++++++++--------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/lib/http/cookie_jar.rb b/lib/http/cookie_jar.rb index 5ca066b..2e96edf 100644 --- a/lib/http/cookie_jar.rb +++ b/lib/http/cookie_jar.rb @@ -74,6 +74,17 @@ class HTTP::CookieJar end alias << add + # Deletes a cookie that has the same name, domain and path as a + # given cookie from the jar and returns self. + # + # How the `for_domain` flag value affects the set of deleted cookies + # depends on the store used. See individual store classes for that + # matter. + def delete(cookie) + @store.delete(cookie) + self + end + # Gets an array of cookies that should be sent for the URL/URI, # updating the access time of each cookie. def cookies(url) diff --git a/test/test_http_cookie_jar.rb b/test/test_http_cookie_jar.rb index bd3f9dd..ee41487 100644 --- a/test/test_http_cookie_jar.rb +++ b/test/test_http_cookie_jar.rb @@ -554,17 +554,26 @@ module TestHTTPCookieJar assert_equal('Foo1 Foo2', @jar.cookies(surl).map { |c| c.name }.sort.join(' ') ) end - def _test_delete - nurl = URI 'http://rubyforge.org/login' - surl = URI 'https://rubyforge.org/login' + def test_delete + cookie1 = HTTP::Cookie.new(cookie_values) + cookie2 = HTTP::Cookie.new(:name => 'Foo', :value => '', + :domain => 'rubyforge.org', + :for_domain => false, + :path => '/') + cookie3 = HTTP::Cookie.new(:name => 'Foo', :value => '', + :domain => 'rubyforge.org', + :for_domain => true, + :path => '/') - cookie1 = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => nurl)) - cookie2 = HTTP::Cookie.new(cookie_values(:name => 'Foo1', :origin => surl)) + @jar.add(cookie1) + @jar.delete(cookie2) - @jar.add(nncookie) - @jar.add(sncookie) - @jar.add(nscookie) - @jar.add(sscookie) + if mozilla_store? + assert_equal(1, @jar.to_a.length) + @jar.delete(cookie3) + end + + assert_equal(0, @jar.to_a.length) end def test_max_cookies