Index: src/test/org/apache/commons/httpclient/cookie/TestCookieCompatibilitySpec.java =================================================================== --- src/test/org/apache/commons/httpclient/cookie/TestCookieCompatibilitySpec.java (revision 180247) +++ src/test/org/apache/commons/httpclient/cookie/TestCookieCompatibilitySpec.java (working copy) @@ -792,7 +792,7 @@ cookiespec.validate("sourceforge.net", 80, "/", false, cookie); } - public void testSecondDomainLevelCookieMatch() throws Exception { + public void testSecondDomainLevelCookieMatch1() throws Exception { Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false); cookie.setDomainAttributeSpecified(true); cookie.setPathAttributeSpecified(true); @@ -801,6 +801,33 @@ assertTrue(cookiespec.match("sourceforge.net", 80, "/", false, cookie)); } + public void testSecondDomainLevelCookieMatch2() throws Exception { + Cookie cookie = new Cookie("sourceforge.net", "name", null, "/", null, false); + cookie.setDomainAttributeSpecified(true); + cookie.setPathAttributeSpecified(true); + + CookieSpec cookiespec = new CookieSpecBase(); + assertTrue(cookiespec.match("www.sourceforge.net", 80, "/", false, cookie)); + } + + public void testInvalidSecondDomainLevelCookieMatch1() throws Exception { + Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false); + cookie.setDomainAttributeSpecified(true); + cookie.setPathAttributeSpecified(true); + + CookieSpec cookiespec = new CookieSpecBase(); + assertFalse(cookiespec.match("antisourceforge.net", 80, "/", false, cookie)); + } + + public void testInvalidSecondDomainLevelCookieMatch2() throws Exception { + Cookie cookie = new Cookie("sourceforge.net", "name", null, "/", null, false); + cookie.setDomainAttributeSpecified(true); + cookie.setPathAttributeSpecified(true); + + CookieSpec cookiespec = new CookieSpecBase(); + assertFalse(cookiespec.match("antisourceforge.net", 80, "/", false, cookie)); + } + public void testMatchNullHost() throws Exception { CookieSpec cookiespec = new CookieSpecBase(); Cookie cookie = new Cookie(); Index: src/java/org/apache/commons/httpclient/cookie/CookieSpecBase.java =================================================================== --- src/java/org/apache/commons/httpclient/cookie/CookieSpecBase.java (revision 180247) +++ src/java/org/apache/commons/httpclient/cookie/CookieSpecBase.java (working copy) @@ -502,9 +502,14 @@ * @param domain The cookie domain attribute. * @return true if the specified host matches the given domain. */ - public boolean domainMatch(final String host, final String domain) { - return host.endsWith(domain) - || (domain.startsWith(".") && host.endsWith(domain.substring(1))); + public boolean domainMatch(final String host, String domain) { + if (host.equals(domain)) { + return true; + } + if (!domain.startsWith(".")) { + domain = "." + domain; + } + return host.endsWith(domain) || host.equals(domain.substring(1)); } /**