From d20462c122e4552137869ace439f7d9de07efe11 Mon Sep 17 00:00:00 2001 From: Shugo Maeda Date: Thu, 11 Apr 2013 16:05:50 +0900 Subject: [PATCH] Expire old cookies before finding a cookie in CookieManager#add. Before this fix, CookieManager#add find a cookie, and then expire old cookies, so the found cookie might be removed before being updated. --- lib/httpclient/cookie.rb | 2 +- test/test_cookie.rb | 21 +++++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/lib/httpclient/cookie.rb b/lib/httpclient/cookie.rb index e0c37e9a..c6a3d6fe 100644 --- a/lib/httpclient/cookie.rb +++ b/lib/httpclient/cookie.rb @@ -303,6 +303,7 @@ def add(given) cookie = nil @cookies.synchronize do + check_expired_cookies cookie = @cookies.find { |c| c.domain == domain && c.path == path && c.name == given.name } @@ -311,7 +312,6 @@ def add(given) cookie.use = true @cookies << cookie end - check_expired_cookies end cookie.domain = domain diff --git a/test/test_cookie.rb b/test/test_cookie.rb index 82e37660..dde2cddb 100644 --- a/test/test_cookie.rb +++ b/test/test_cookie.rb @@ -235,6 +235,27 @@ def test_parse_expires assert_equal("/", cookie.path) end + def test_parse_after_expiration + str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=Wed, 01-Dec-2010 00:00:00 GMT; path=/" + @cm.parse(str, urify('http://www.test.jp')) + cookie = @cm.cookies[0] + assert_instance_of(WebAgent::Cookie, cookie) + assert_equal("inkid", cookie.name) + assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value) + assert_equal(Time.gm(2010, 12, 1, 0,0,0), cookie.expires) + assert_equal("/", cookie.path) + + time = Time.now.utc.round + 60 + expires = time.strftime("%a, %d-%b-%Y %H:%M:%S GMT") + str = "inkid=n92b0ADOgACIgUb9lsjHqAAAHu2a; expires=#{expires}; path=/" + @cm.parse(str, urify('http://www.test.jp')) + cookie = @cm.cookies[0] + assert_equal("inkid", cookie.name) + assert_equal("n92b0ADOgACIgUb9lsjHqAAAHu2a", cookie.value) + assert_equal(time, cookie.expires) + assert_equal("/", cookie.path) + end + def test_find_cookie() str = "xmen=off,0,0,1; path=/; domain=.excite2.co.jp; expires=Wednesday, 31-Dec-2037 12:00:00 GMT" @cm.parse(str, urify("http://www.excite2.co.jp/"))