HTTPCLIENT-859: CookieIdentityComparator now takes path attribute into consideration when comparing cookies

git-svn-id: https://svn.apache.org/repos/asf/httpcomponents/httpclient/trunk@792645 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2009-07-09 19:29:11 +00:00
parent 2b69ac0ae9
commit 9a2a3cb732
4 changed files with 54 additions and 1 deletions

View File

@ -1,6 +1,10 @@
Changes since 4.0 beta 2
-------------------
* [HTTPCLIENT-859] CookieIdentityComparator now takes path attribute into
consideration when comparing cookies.
Contributed by Oleg Kalnichevski <olegk at apache.org>
* HttpClient will no longer send expired cookies back to the origin server.
Contributed by Oleg Kalnichevski <olegk at apache.org>

View File

@ -176,7 +176,7 @@ public class RequestAddCookies implements HttpRequestInterceptor {
List<Cookie> matchedCookies = new ArrayList<Cookie>();
Date now = new Date();
for (Cookie cookie : cookies) {
if (cookie.getExpiryDate() == null || cookie.getExpiryDate().after(now)) {
if (!cookie.isExpired(now)) {
if (cookieSpec.match(cookie, cookieOrigin)) {
if (this.log.isDebugEnabled()) {
this.log.debug("Cookie " + cookie + " match " + cookieOrigin);

View File

@ -67,6 +67,17 @@ public class CookieIdentityComparator implements Serializable, Comparator<Cookie
}
res = d1.compareToIgnoreCase(d2);
}
if (res == 0) {
String p1 = c1.getPath();
if (p1 == null) {
p1 = "/";
}
String p2 = c2.getPath();
if (p2 == null) {
p2 = "/";
}
res = p1.compareTo(p2);
}
return res;
}

View File

@ -116,4 +116,42 @@ public class TestCookieIdentityComparator extends TestCase {
assertTrue(comparator.compare(c3, c4) == 0);
}
public void testCookieIdentityComparasionByNameDomainAndPath() {
CookieIdentityComparator comparator = new CookieIdentityComparator();
BasicClientCookie c1 = new BasicClientCookie("name", "value1");
c1.setDomain("www.domain.com");
c1.setPath("/whatever");
BasicClientCookie c2 = new BasicClientCookie("name", "value2");
c2.setDomain("www.domain.com");
c2.setPath("/whatever");
assertTrue(comparator.compare(c1, c2) == 0);
BasicClientCookie c3 = new BasicClientCookie("name", "value1");
c3.setDomain("www.domain.com");
c3.setPath("/whatever");
BasicClientCookie c4 = new BasicClientCookie("name", "value2");
c4.setDomain("domain.com");
c4.setPath("/whatever-not");
assertFalse(comparator.compare(c3, c4) == 0);
}
public void testCookieIdentityComparasionByNameDomainAndNullPath() {
CookieIdentityComparator comparator = new CookieIdentityComparator();
BasicClientCookie c1 = new BasicClientCookie("name", "value1");
c1.setDomain("www.domain.com");
c1.setPath("/");
BasicClientCookie c2 = new BasicClientCookie("name", "value2");
c2.setDomain("www.domain.com");
c2.setPath(null);
assertTrue(comparator.compare(c1, c2) == 0);
BasicClientCookie c3 = new BasicClientCookie("name", "value1");
c3.setDomain("www.domain.com");
c3.setPath("/whatever");
BasicClientCookie c4 = new BasicClientCookie("name", "value2");
c4.setDomain("domain.com");
c4.setPath(null);
assertFalse(comparator.compare(c3, c4) == 0);
}
}