Refactoring of the Netscape cookie draft spec

git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@415068 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Oleg Kalnichevski 2006-06-17 21:51:43 +00:00
parent cd657a4939
commit 536068976f
8 changed files with 683 additions and 81 deletions

View File

@ -33,9 +33,9 @@ import org.apache.http.cookie.CookieAttributeHandler;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.MalformedCookieException;
public class BrowserCompatDomainHandler implements CookieAttributeHandler {
public class BasicDomainHandler implements CookieAttributeHandler {
public BrowserCompatDomainHandler() {
public BasicDomainHandler() {
super();
}

View File

@ -71,7 +71,7 @@ public class BrowserCompatSpec extends CookieSpecBase {
public BrowserCompatSpec() {
super();
registerAttribHandler("path", new BasicPathHandler());
registerAttribHandler("domain", new BrowserCompatDomainHandler());
registerAttribHandler("domain", new BasicDomainHandler());
registerAttribHandler("max-age", new BasicMaxAgeHandler());
registerAttribHandler("secure", new BasicSecureHandler());
registerAttribHandler("comment", new BasicCommentHandler());

View File

@ -0,0 +1,104 @@
/*
* $HeadURL$
* $Revision$
* $Date$
*
* ====================================================================
*
* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.cookie.impl;
import java.util.StringTokenizer;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.MalformedCookieException;
public class NetscapeDomainHandler extends BasicDomainHandler {
public NetscapeDomainHandler() {
super();
}
public void validate(final Cookie cookie, final CookieOrigin origin)
throws MalformedCookieException {
super.validate(cookie, origin);
// Perform Netscape Cookie draft specific validation
String host = origin.getHost();
String domain = cookie.getDomain();
if (host.indexOf(".") >= 0) {
int domainParts = new StringTokenizer(domain, ".").countTokens();
if (isSpecialDomain(domain)) {
if (domainParts < 2) {
throw new MalformedCookieException("Domain attribute \""
+ domain
+ "\" violates the Netscape cookie specification for "
+ "special domains");
}
} else {
if (domainParts < 3) {
throw new MalformedCookieException("Domain attribute \""
+ domain
+ "\" violates the Netscape cookie specification");
}
}
}
}
/**
* Checks if the given domain is in one of the seven special
* top level domains defined by the Netscape cookie specification.
* @param domain The domain.
* @return True if the specified domain is "special"
*/
private static boolean isSpecialDomain(final String domain) {
final String ucDomain = domain.toUpperCase();
if (ucDomain.endsWith(".COM")
|| ucDomain.endsWith(".EDU")
|| ucDomain.endsWith(".NET")
|| ucDomain.endsWith(".GOV")
|| ucDomain.endsWith(".MIL")
|| ucDomain.endsWith(".ORG")
|| ucDomain.endsWith(".INT")) {
return true;
}
return false;
}
public boolean match(Cookie cookie, CookieOrigin origin) {
if (cookie == null) {
throw new IllegalArgumentException("Cookie may not be null");
}
if (origin == null) {
throw new IllegalArgumentException("Cookie origin may not be null");
}
String host = origin.getHost();
String domain = cookie.getDomain();
if (domain == null) {
return false;
}
return host.endsWith(domain);
}
}

View File

@ -0,0 +1,131 @@
/*
* $HeadURL$
* $Revision$
* $Date$
*
* ====================================================================
*
* Copyright 2002-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.cookie.impl;
import org.apache.http.Header;
import org.apache.http.HeaderElement;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.MalformedCookieException;
import org.apache.http.io.CharArrayBuffer;
/**
* Netscape cookie draft specific cookie management functions
*
* @author B.C. Holmes
* @author <a href="mailto:jericho@thinkfree.com">Park, Sung-Gu</a>
* @author <a href="mailto:dsale@us.britannica.com">Doug Sale</a>
* @author Rod Waldhoff
* @author dIon Gillard
* @author Sean C. Sullivan
* @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
* @author Marc A. Saegesser
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
* @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
*
* @since 2.0
*/
public class NetscapeDraftSpec extends CookieSpecBase {
/** Default constructor */
public NetscapeDraftSpec() {
super();
registerAttribHandler("path", new BasicPathHandler());
registerAttribHandler("domain", new NetscapeDomainHandler());
registerAttribHandler("max-age", new BasicMaxAgeHandler());
registerAttribHandler("secure", new BasicSecureHandler());
registerAttribHandler("comment", new BasicCommentHandler());
registerAttribHandler("expires", new BasicExpiresHandler(
new String[] {"EEE, dd-MMM-yyyy HH:mm:ss z"}));
}
/**
* Parses the Set-Cookie value into an array of <tt>Cookie</tt>s.
*
* <p>Syntax of the Set-Cookie HTTP Response Header:</p>
*
* <p>This is the format a CGI script would use to add to
* the HTTP headers a new piece of data which is to be stored by
* the client for later retrieval.</p>
*
* <PRE>
* Set-Cookie: NAME=VALUE; expires=DATE; path=PATH; domain=DOMAIN_NAME; secure
* </PRE>
*
* <p>Please note that Netscape draft specification does not fully
* conform to the HTTP header format. Netscape draft does not specify
* whether multiple cookies may be sent in one header. Hence, comma
* character may be present in unquoted cookie value or unquoted
* parameter value.</p>
*
* @link http://wp.netscape.com/newsref/std/cookie_spec.html
*
* @param header the <tt>Set-Cookie</tt> received from the server
* @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie value
* @throws MalformedCookieException if an exception occurs during parsing
*
* @since 3.0
*/
public Cookie[] parse(final Header header, final CookieOrigin origin)
throws MalformedCookieException {
if (header == null) {
throw new IllegalArgumentException("Header may not be null");
}
if (origin == null) {
throw new IllegalArgumentException("Cookie origin may not be null");
}
String headervalue = header.getValue();
return parse(new HeaderElement[] { HeaderElement.parse(headervalue) }, origin);
}
public Header[] formatCookies(final Cookie[] cookies) {
if (cookies == null) {
throw new IllegalArgumentException("Cookie array may not be null");
}
if (cookies.length == 0) {
throw new IllegalArgumentException("Cookie array may not be empty");
}
CharArrayBuffer buffer = new CharArrayBuffer(20 * cookies.length);
for (int i = 0; i < cookies.length; i++) {
Cookie cookie = cookies[i];
if (i > 0) {
buffer.append("; ");
}
buffer.append(cookie.getName());
String s = cookie.getValue();
if (s != null) {
buffer.append("=");
buffer.append(s);
}
}
return new Header[] { new Header("Cookie", buffer.toString()) };
}
}

View File

@ -40,8 +40,9 @@ public class TestAllCookieImpl extends TestCase {
TestSuite suite = new TestSuite();
suite.addTest(TestAbstractCookieSpec.suite());
suite.addTest(TestBasicCookieAttribHandlers.suite());
suite.addTest(TestBrowserCompatCookieAttribHandlers.suite());
suite.addTest(TestNetscapeCookieAttribHandlers.suite());
suite.addTest(TestBrowserCompatSpec.suite());
suite.addTest(TestCookieNetscapeDraft.suite());
return suite;
}

View File

@ -58,6 +58,156 @@ public class TestBasicCookieAttribHandlers extends TestCase {
junit.textui.TestRunner.main(testCaseName);
}
public void testBasicDomainParse() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieAttributeHandler h = new BasicDomainHandler();
h.parse(cookie, "www.somedomain.com");
assertEquals("www.somedomain.com", cookie.getDomain());
assertTrue(cookie.isDomainAttributeSpecified());
}
public void testBasicDomainParseInvalid() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieAttributeHandler h = new BasicDomainHandler();
try {
h.parse(cookie, "");
fail("MalformedCookieException should have been thrown");
} catch (MalformedCookieException ex) {
// expected
}
try {
h.parse(cookie, null);
fail("MalformedCookieException should have been thrown");
} catch (MalformedCookieException ex) {
// expected
}
}
public void testBasicDomainValidate1() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
CookieAttributeHandler h = new BasicDomainHandler();
cookie.setDomain(".somedomain.com");
h.validate(cookie, origin);
cookie.setDomain(".otherdomain.com");
try {
h.validate(cookie, origin);
fail("MalformedCookieException should have been thrown");
} catch (MalformedCookieException ex) {
// expected
}
cookie.setDomain("www.otherdomain.com");
try {
h.validate(cookie, origin);
fail("MalformedCookieException should have been thrown");
} catch (MalformedCookieException ex) {
// expected
}
}
public void testBasicDomainValidate2() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
CookieAttributeHandler h = new BasicDomainHandler();
cookie.setDomain("somehost");
h.validate(cookie, origin);
cookie.setDomain("otherhost");
try {
h.validate(cookie, origin);
fail("MalformedCookieException should have been thrown");
} catch (MalformedCookieException ex) {
// expected
}
}
public void testBasicDomainValidate3() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
CookieAttributeHandler h = new BasicDomainHandler();
cookie.setDomain(".somedomain.com");
h.validate(cookie, origin);
}
public void testBasicDomainValidate4() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
CookieAttributeHandler h = new BasicDomainHandler();
cookie.setDomain(null);
try {
h.validate(cookie, origin);
fail("MalformedCookieException should have been thrown");
} catch (MalformedCookieException ex) {
// expected
}
}
public void testBasicDomainMatch1() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
CookieAttributeHandler h = new BasicDomainHandler();
cookie.setDomain("somedomain.com");
assertTrue(h.match(cookie, origin));
cookie.setDomain(".somedomain.com");
assertTrue(h.match(cookie, origin));
}
public void testBasicDomainMatch2() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
CookieAttributeHandler h = new BasicDomainHandler();
cookie.setDomain("somedomain.com");
assertTrue(h.match(cookie, origin));
cookie.setDomain(".somedomain.com");
assertTrue(h.match(cookie, origin));
cookie.setDomain(null);
assertFalse(h.match(cookie, origin));
}
public void testBasicDomainInvalidInput() throws Exception {
CookieAttributeHandler h = new BasicDomainHandler();
try {
h.parse(null, null);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
h.validate(null, null);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
h.validate(new Cookie("name", "value"), null);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
h.match(null, null);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
h.match(new Cookie("name", "value"), null);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
}
public void testBasicPathParse() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieAttributeHandler h = new BasicPathHandler();

View File

@ -0,0 +1,254 @@
/*
* $HeadURL$
* $Revision$
* $Date$
* ====================================================================
*
* Copyright 1999-2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of the Apache Software Foundation. For more
* information on the Apache Software Foundation, please see
* <http://www.apache.org/>.
*
*/
package org.apache.http.cookie.impl;
import org.apache.http.Header;
import org.apache.http.cookie.Cookie;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.CookieSpec;
import org.apache.http.cookie.MalformedCookieException;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test cases for Netscape cookie draft
*
* @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
*
* @version $Revision$
*/
public class TestCookieNetscapeDraft extends TestCase {
// ------------------------------------------------------------ Constructor
public TestCookieNetscapeDraft(String name) {
super(name);
}
// ------------------------------------------------------- TestCase Methods
public static Test suite() {
return new TestSuite(TestCookieNetscapeDraft.class);
}
public void testParseAbsPath() throws Exception {
Header header = new Header("Set-Cookie", "name1=value1;Path=/path/");
CookieSpec cookiespec = new NetscapeDraftSpec();
CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
Cookie[] parsed = cookiespec.parse(header, origin);
for (int i = 0; i < parsed.length; i++) {
cookiespec.validate(parsed[i], origin);
}
assertEquals("Found 1 cookies.",1,parsed.length);
assertEquals("Name","name1",parsed[0].getName());
assertEquals("Value","value1",parsed[0].getValue());
assertEquals("Domain","host",parsed[0].getDomain());
assertEquals("Path","/path/",parsed[0].getPath());
}
public void testParseAbsPath2() throws Exception {
Header header = new Header("Set-Cookie", "name1=value1;Path=/");
CookieSpec cookiespec = new NetscapeDraftSpec();
CookieOrigin origin = new CookieOrigin("host", 80, "/", true);
Cookie[] parsed = cookiespec.parse(header, origin);
for (int i = 0; i < parsed.length; i++) {
cookiespec.validate(parsed[i], origin);
}
assertEquals("Found 1 cookies.",1,parsed.length);
assertEquals("Name","name1",parsed[0].getName());
assertEquals("Value","value1",parsed[0].getValue());
assertEquals("Domain","host",parsed[0].getDomain());
assertEquals("Path","/",parsed[0].getPath());
}
public void testParseRelativePath() throws Exception {
Header header = new Header("Set-Cookie", "name1=value1;Path=whatever");
CookieSpec cookiespec = new NetscapeDraftSpec();
CookieOrigin origin = new CookieOrigin("host", 80, "whatever", true);
Cookie[] parsed = cookiespec.parse(header, origin);
for (int i = 0; i < parsed.length; i++) {
cookiespec.validate(parsed[i], origin);
}
assertEquals("Found 1 cookies.",1,parsed.length);
assertEquals("Name","name1",parsed[0].getName());
assertEquals("Value","value1",parsed[0].getValue());
assertEquals("Domain","host",parsed[0].getDomain());
assertEquals("Path","whatever",parsed[0].getPath());
}
public void testParseWithIllegalNetscapeDomain1() throws Exception {
Header header = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com");
CookieSpec cookiespec = new NetscapeDraftSpec();
try {
CookieOrigin origin = new CookieOrigin("a.com", 80, "/", false);
Cookie[] parsed = cookiespec.parse(header, origin);
for (int i = 0; i < parsed.length; i++) {
cookiespec.validate(parsed[i], origin);
}
fail("MalformedCookieException exception should have been thrown");
} catch (MalformedCookieException e) {
// expected
}
}
public void testParseWithWrongNetscapeDomain2() throws Exception {
Header header = new Header("Set-Cookie","cookie-name=cookie-value; domain=.y.z");
CookieSpec cookiespec = new NetscapeDraftSpec();
try {
CookieOrigin origin = new CookieOrigin("x.y.z", 80, "/", false);
Cookie[] parsed = cookiespec.parse(header, origin);
for (int i = 0; i < parsed.length; i++) {
cookiespec.validate(parsed[i], origin);
}
fail("MalformedCookieException exception should have been thrown");
} catch (MalformedCookieException e) {
// expected
}
}
/**
* Tests Netscape specific cookie formatting.
*/
public void testNetscapeCookieFormatting() throws Exception {
Header header = new Header(
"Set-Cookie", "name=value; path=/; domain=.mydomain.com");
CookieSpec cookiespec = new NetscapeDraftSpec();
CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
Cookie[] cookies = cookiespec.parse(header, origin);
cookiespec.validate(cookies[0], origin);
Header[] headers = cookiespec.formatCookies(cookies);
assertEquals(1, headers.length);
assertEquals("name=value", headers[0].getValue());
}
/**
* Tests Netscape specific expire attribute parsing.
*/
public void testNetscapeCookieExpireAttribute() throws Exception {
CookieSpec cookiespec = new NetscapeDraftSpec();
Header header = new Header("Set-Cookie",
"name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment");
CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
Cookie[] cookies = cookiespec.parse(header, origin);
cookiespec.validate(cookies[0], origin);
header = new Header("Set-Cookie",
"name=value; path=/; domain=.mydomain.com; expires=Thu 01-Jan-2070 00:00:10 GMT; comment=no_comment");
try {
cookies = cookiespec.parse(header, origin);
cookiespec.validate(cookies[0], origin);
fail("MalformedCookieException exception should have been thrown");
} catch (MalformedCookieException e) {
// expected
}
}
/**
* Tests Netscape specific expire attribute without a time zone.
*/
public void testNetscapeCookieExpireAttributeNoTimeZone() throws Exception {
CookieSpec cookiespec = new NetscapeDraftSpec();
Header header = new Header("Set-Cookie",
"name=value; expires=Thu, 01-Jan-2006 00:00:00 ");
CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
try {
cookiespec.parse(header, origin);
fail("MalformedCookieException should have been thrown");
} catch (MalformedCookieException ex) {
// expected
}
}
/**
* Tests if cookie values with embedded comma are handled correctly.
*/
public void testCookieWithComma() throws Exception {
Header header = new Header("Set-Cookie", "a=b,c");
CookieSpec cookiespec = new NetscapeDraftSpec();
CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
Cookie[] cookies = cookiespec.parse(header, origin);
assertEquals("number of cookies", 1, cookies.length);
assertEquals("a", cookies[0].getName());
assertEquals("b,c", cookies[0].getValue());
}
public void testFormatCookies() throws Exception {
Cookie c1 = new Cookie("name1", "value1");
c1.setDomain(".whatever.com");
c1.setDomainAttributeSpecified(true);
c1.setPath("/");
c1.setPathAttributeSpecified(true);
Cookie c2 = new Cookie("name2", "value2");
Cookie c3 = new Cookie("name3", null);
CookieSpec cookiespec = new NetscapeDraftSpec();
Header[] headers = cookiespec.formatCookies(new Cookie[] {c1, c2, c3});
assertNotNull(headers);
assertEquals(1, headers.length);
assertEquals("name1=value1; name2=value2; name3", headers[0].getValue());
}
public void testInvalidInput() throws Exception {
CookieSpec cookiespec = new NetscapeDraftSpec();
try {
cookiespec.parse(null, null);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
cookiespec.parse(new Header("Set-Cookie", "name=value"), null);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
cookiespec.formatCookies(null);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
cookiespec.formatCookies(new Cookie[] {});
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
}
}

View File

@ -37,51 +37,43 @@ import org.apache.http.cookie.CookieAttributeHandler;
import org.apache.http.cookie.CookieOrigin;
import org.apache.http.cookie.MalformedCookieException;
public class TestBrowserCompatCookieAttribHandlers extends TestCase {
public class TestNetscapeCookieAttribHandlers extends TestCase {
public TestBrowserCompatCookieAttribHandlers(String testName) {
public TestNetscapeCookieAttribHandlers(String testName) {
super(testName);
}
public static Test suite() {
return new TestSuite(TestBrowserCompatCookieAttribHandlers.class);
return new TestSuite(TestNetscapeCookieAttribHandlers.class);
}
// ------------------------------------------------------------------- Main
public static void main(String args[]) {
String[] testCaseName = { TestBrowserCompatCookieAttribHandlers.class.getName() };
String[] testCaseName = { TestNetscapeCookieAttribHandlers.class.getName() };
junit.textui.TestRunner.main(testCaseName);
}
public void testBrowserCompatDomainParse() throws Exception {
public void testNetscapeDomainValidate1() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieAttributeHandler h = new BrowserCompatDomainHandler();
h.parse(cookie, "www.somedomain.com");
assertEquals("www.somedomain.com", cookie.getDomain());
assertTrue(cookie.isDomainAttributeSpecified());
}
CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
CookieAttributeHandler h = new NetscapeDomainHandler();
cookie.setDomain("somehost");
h.validate(cookie, origin);
public void testBrowserCompatDomainParseInvalid() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieAttributeHandler h = new BrowserCompatDomainHandler();
cookie.setDomain("otherhost");
try {
h.parse(cookie, "");
fail("MalformedCookieException should have been thrown");
} catch (MalformedCookieException ex) {
// expected
}
try {
h.parse(cookie, null);
h.validate(cookie, origin);
fail("MalformedCookieException should have been thrown");
} catch (MalformedCookieException ex) {
// expected
}
}
public void testBrowserCompatDomainValidate1() throws Exception {
public void testNetscapeDomainValidate2() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
CookieAttributeHandler h = new BrowserCompatDomainHandler();
CookieAttributeHandler h = new NetscapeDomainHandler();
cookie.setDomain(".somedomain.com");
h.validate(cookie, origin);
@ -102,15 +94,15 @@ public class TestBrowserCompatCookieAttribHandlers extends TestCase {
}
}
public void testBrowserCompatDomainValidate2() throws Exception {
public void testNetscapeDomainValidate3() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
CookieAttributeHandler h = new BrowserCompatDomainHandler();
CookieOrigin origin = new CookieOrigin("www.a.com", 80, "/", false);
CookieAttributeHandler h = new NetscapeDomainHandler();
cookie.setDomain("somehost");
cookie.setDomain(".a.com");
h.validate(cookie, origin);
cookie.setDomain("otherhost");
cookie.setDomain(".com");
try {
h.validate(cookie, origin);
fail("MalformedCookieException should have been thrown");
@ -119,21 +111,15 @@ public class TestBrowserCompatCookieAttribHandlers extends TestCase {
}
}
public void testBrowserCompatDomainValidate3() throws Exception {
public void testNetscapeDomainValidate4() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
CookieAttributeHandler h = new BrowserCompatDomainHandler();
CookieOrigin origin = new CookieOrigin("www.a.b.c", 80, "/", false);
CookieAttributeHandler h = new NetscapeDomainHandler();
cookie.setDomain(".somedomain.com");
cookie.setDomain(".a.b.c");
h.validate(cookie, origin);
}
public void testBrowserCompatDomainValidate4() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
CookieAttributeHandler h = new BrowserCompatDomainHandler();
cookie.setDomain(null);
cookie.setDomain(".b.c");
try {
h.validate(cookie, origin);
fail("MalformedCookieException should have been thrown");
@ -142,53 +128,29 @@ public class TestBrowserCompatCookieAttribHandlers extends TestCase {
}
}
public void testBrowserCompatDomainMatch1() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
CookieAttributeHandler h = new BrowserCompatDomainHandler();
cookie.setDomain("somedomain.com");
assertTrue(h.match(cookie, origin));
cookie.setDomain(".somedomain.com");
assertTrue(h.match(cookie, origin));
}
public void testBrowserCompatDomainMatch2() throws Exception {
public void testNetscapeDomainMatch1() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
CookieAttributeHandler h = new BrowserCompatDomainHandler();
cookie.setDomain("somedomain.com");
assertTrue(h.match(cookie, origin));
cookie.setDomain(".somedomain.com");
assertTrue(h.match(cookie, origin));
CookieAttributeHandler h = new NetscapeDomainHandler();
cookie.setDomain(null);
assertFalse(h.match(cookie, origin));
cookie.setDomain(".somedomain.com");
assertTrue(h.match(cookie, origin));
}
public void testNetscapeDomainMatch2() throws Exception {
Cookie cookie = new Cookie("name", "value");
CookieOrigin origin = new CookieOrigin("www.whatever.somedomain.com", 80, "/", false);
CookieAttributeHandler h = new NetscapeDomainHandler();
cookie.setDomain(".somedomain.com");
assertTrue(h.match(cookie, origin));
}
public void testBrowserCompatDomainInvalidInput() throws Exception {
CookieAttributeHandler h = new BrowserCompatDomainHandler();
try {
h.parse(null, null);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
h.validate(null, null);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
try {
h.validate(new Cookie("name", "value"), null);
fail("IllegalArgumentException must have been thrown");
} catch (IllegalArgumentException ex) {
// expected
}
CookieAttributeHandler h = new NetscapeDomainHandler();
try {
h.match(null, null);
fail("IllegalArgumentException must have been thrown");