0001: /*
0002: * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieCompatibilitySpec.java,v 1.7 2004/09/14 20:11:32 olegk Exp $
0003: * $Revision: 480424 $
0004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
0005: * ====================================================================
0006: *
0007: * Licensed to the Apache Software Foundation (ASF) under one or more
0008: * contributor license agreements. See the NOTICE file distributed with
0009: * this work for additional information regarding copyright ownership.
0010: * The ASF licenses this file to You under the Apache License, Version 2.0
0011: * (the "License"); you may not use this file except in compliance with
0012: * the License. You may obtain a copy of the License at
0013: *
0014: * http://www.apache.org/licenses/LICENSE-2.0
0015: *
0016: * Unless required by applicable law or agreed to in writing, software
0017: * distributed under the License is distributed on an "AS IS" BASIS,
0018: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0019: * See the License for the specific language governing permissions and
0020: * limitations under the License.
0021: * ====================================================================
0022: *
0023: * This software consists of voluntary contributions made by many
0024: * individuals on behalf of the Apache Software Foundation. For more
0025: * information on the Apache Software Foundation, please see
0026: * <http://www.apache.org/>.
0027: *
0028: */
0029:
0030: package org.apache.commons.httpclient.cookie;
0031:
0032: import java.util.Collection;
0033: import java.util.Date;
0034:
0035: import junit.framework.Test;
0036: import junit.framework.TestSuite;
0037:
0038: import org.apache.commons.httpclient.Cookie;
0039: import org.apache.commons.httpclient.Header;
0040: import org.apache.commons.httpclient.HttpException;
0041: import org.apache.commons.httpclient.HttpState;
0042: import org.apache.commons.httpclient.NameValuePair;
0043: import org.apache.commons.httpclient.params.DefaultHttpParamsFactory;
0044: import org.apache.commons.httpclient.params.HttpMethodParams;
0045: import org.apache.commons.httpclient.params.HttpParams;
0046:
0047: /**
0048: * Test cases for Cookie
0049: *
0050: * @author BC Holmes
0051: * @author Rod Waldhoff
0052: * @author dIon Gillard
0053: * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
0054: * @author Marc A. Saegesser
0055: * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
0056: * @version $Revision: 480424 $
0057: */
0058: public class TestCookieCompatibilitySpec extends TestCookieBase {
0059:
0060: // ------------------------------------------------------------ Constructor
0061:
0062: public TestCookieCompatibilitySpec(String name) {
0063: super (name);
0064: }
0065:
0066: // ------------------------------------------------------- TestCase Methods
0067:
0068: public static Test suite() {
0069: return new TestSuite(TestCookieCompatibilitySpec.class);
0070: }
0071:
0072: public void testParseAttributeInvalidAttrib() throws Exception {
0073: CookieSpec cookiespec = new CookieSpecBase();
0074: try {
0075: cookiespec.parseAttribute(null, null);
0076: fail("IllegalArgumentException must have been thrown");
0077: } catch (IllegalArgumentException expected) {
0078: }
0079: }
0080:
0081: public void testParseAttributeInvalidCookie() throws Exception {
0082: CookieSpec cookiespec = new CookieSpecBase();
0083: try {
0084: cookiespec.parseAttribute(
0085: new NameValuePair("name", "value"), null);
0086: fail("IllegalArgumentException must have been thrown");
0087: } catch (IllegalArgumentException expected) {
0088: }
0089: }
0090:
0091: public void testParseAttributeNullPath() throws Exception {
0092: CookieSpec cookiespec = new CookieSpecBase();
0093: Cookie cookie = new Cookie();
0094: cookiespec.parseAttribute(new NameValuePair("path", null),
0095: cookie);
0096: assertEquals("/", cookie.getPath());
0097: }
0098:
0099: public void testParseAttributeBlankPath() throws Exception {
0100: CookieSpec cookiespec = new CookieSpecBase();
0101: Cookie cookie = new Cookie();
0102: cookiespec.parseAttribute(new NameValuePair("path", " "),
0103: cookie);
0104: assertEquals("/", cookie.getPath());
0105: }
0106:
0107: public void testParseAttributeNullDomain() throws Exception {
0108: CookieSpec cookiespec = new CookieSpecBase();
0109: Cookie cookie = new Cookie();
0110: try {
0111: cookiespec.parseAttribute(
0112: new NameValuePair("domain", null), cookie);
0113: fail("MalformedCookieException must have been thrown");
0114: } catch (MalformedCookieException expected) {
0115: }
0116: }
0117:
0118: public void testParseAttributeBlankDomain() throws Exception {
0119: CookieSpec cookiespec = new CookieSpecBase();
0120: Cookie cookie = new Cookie();
0121: try {
0122: cookiespec.parseAttribute(
0123: new NameValuePair("domain", " "), cookie);
0124: fail("MalformedCookieException must have been thrown");
0125: } catch (MalformedCookieException expected) {
0126: }
0127: }
0128:
0129: public void testParseAttributeNullMaxAge() throws Exception {
0130: CookieSpec cookiespec = new CookieSpecBase();
0131: Cookie cookie = new Cookie();
0132: try {
0133: cookiespec.parseAttribute(
0134: new NameValuePair("max-age", null), cookie);
0135: fail("MalformedCookieException must have been thrown");
0136: } catch (MalformedCookieException expected) {
0137: }
0138: }
0139:
0140: public void testParseAttributeInvalidMaxAge() throws Exception {
0141: CookieSpec cookiespec = new CookieSpecBase();
0142: Cookie cookie = new Cookie();
0143: try {
0144: cookiespec.parseAttribute(new NameValuePair("max-age",
0145: "crap"), cookie);
0146: fail("MalformedCookieException must have been thrown");
0147: } catch (MalformedCookieException expected) {
0148: }
0149: }
0150:
0151: public void testParseAttributeNullExpires() throws Exception {
0152: CookieSpec cookiespec = new CookieSpecBase();
0153: Cookie cookie = new Cookie();
0154: try {
0155: cookiespec.parseAttribute(
0156: new NameValuePair("expires", null), cookie);
0157: fail("MalformedCookieException must have been thrown");
0158: } catch (MalformedCookieException expected) {
0159: }
0160: }
0161:
0162: public void testParseAttributeUnknownValue() throws Exception {
0163: CookieSpec cookiespec = new CookieSpecBase();
0164: Cookie cookie = new Cookie();
0165: cookiespec.parseAttribute(new NameValuePair("nonsense", null),
0166: cookie);
0167: }
0168:
0169: public void testValidateNullHost() throws Exception {
0170: CookieSpec cookiespec = new CookieSpecBase();
0171: Cookie cookie = new Cookie();
0172: try {
0173: cookiespec.validate(null, 80, "/", false, cookie);
0174: fail("IllegalArgumentException must have been thrown");
0175: } catch (IllegalArgumentException expected) {
0176: }
0177: }
0178:
0179: public void testValidateBlankHost() throws Exception {
0180: CookieSpec cookiespec = new CookieSpecBase();
0181: Cookie cookie = new Cookie();
0182: try {
0183: cookiespec.validate(" ", 80, "/", false, cookie);
0184: fail("IllegalArgumentException must have been thrown");
0185: } catch (IllegalArgumentException expected) {
0186: }
0187: }
0188:
0189: public void testValidateNullPath() throws Exception {
0190: CookieSpec cookiespec = new CookieSpecBase();
0191: Cookie cookie = new Cookie();
0192: try {
0193: cookiespec.validate("host", 80, null, false, cookie);
0194: fail("IllegalArgumentException must have been thrown");
0195: } catch (IllegalArgumentException expected) {
0196: }
0197: }
0198:
0199: public void testValidateBlankPath() throws Exception {
0200: CookieSpec cookiespec = new CookieSpecBase();
0201: Cookie cookie = new Cookie("host", "name", "value", "/", null,
0202: false);
0203: cookiespec.validate("host", 80, " ", false, cookie);
0204: }
0205:
0206: public void testValidateInvalidPort() throws Exception {
0207: CookieSpec cookiespec = new CookieSpecBase();
0208: Cookie cookie = new Cookie();
0209: try {
0210: cookiespec.validate("host", -80, "/", false, cookie);
0211: fail("IllegalArgumentException must have been thrown");
0212: } catch (IllegalArgumentException expected) {
0213: }
0214: }
0215:
0216: public void testValidateInvalidCookieVersion() throws Exception {
0217: CookieSpec cookiespec = new CookieSpecBase();
0218: Cookie cookie = new Cookie();
0219: cookie.setVersion(-1);
0220: try {
0221: cookiespec.validate("host", 80, "/", false, cookie);
0222: fail("MalformedCookieException must have been thrown");
0223: } catch (MalformedCookieException expected) {
0224: }
0225: }
0226:
0227: /**
0228: * Tests whether domain attribute check is case-insensitive.
0229: */
0230: public void testDomainCaseInsensitivity() throws Exception {
0231: Header header = new Header("Set-Cookie",
0232: "name=value; path=/; domain=.whatever.com");
0233:
0234: CookieSpec cookiespec = new CookieSpecBase();
0235: Cookie[] parsed = cookieParse(cookiespec, "www.WhatEver.com",
0236: 80, "/", false, header);
0237: assertNotNull(parsed);
0238: assertEquals(1, parsed.length);
0239: assertEquals(".whatever.com", parsed[0].getDomain());
0240: }
0241:
0242: /**
0243: * Test basic parse (with various spacings
0244: */
0245: public void testParse1() throws Exception {
0246: String headerValue = "custno = 12345; comment=test; version=1,"
0247: + " name=John; version=1; max-age=600; secure; domain=.apache.org";
0248:
0249: Header header = new Header("set-cookie", headerValue);
0250:
0251: CookieSpec cookiespec = new CookieSpecBase();
0252: Cookie[] cookies = cookieParse(cookiespec, "www.apache.org",
0253: 80, "/", false, header);
0254: assertEquals(2, cookies.length);
0255:
0256: assertEquals("custno", cookies[0].getName());
0257: assertEquals("12345", cookies[0].getValue());
0258: assertEquals("test", cookies[0].getComment());
0259: assertEquals(0, cookies[0].getVersion());
0260: assertEquals("www.apache.org", cookies[0].getDomain());
0261: assertEquals("/", cookies[0].getPath());
0262: assertFalse(cookies[0].getSecure());
0263:
0264: assertEquals("name", cookies[1].getName());
0265: assertEquals("John", cookies[1].getValue());
0266: assertEquals(null, cookies[1].getComment());
0267: assertEquals(0, cookies[1].getVersion());
0268: assertEquals(".apache.org", cookies[1].getDomain());
0269: assertEquals("/", cookies[1].getPath());
0270: assertTrue(cookies[1].getSecure());
0271: }
0272:
0273: /**
0274: * Test no spaces
0275: */
0276: public void testParse2() throws Exception {
0277: String headerValue = "custno=12345;comment=test; version=1,"
0278: + "name=John;version=1;max-age=600;secure;domain=.apache.org";
0279:
0280: Header header = new Header("set-cookie", headerValue);
0281:
0282: CookieSpec cookiespec = new CookieSpecBase();
0283: Cookie[] cookies = cookieParse(cookiespec, "www.apache.org",
0284: 80, "/", false, header);
0285:
0286: assertEquals(2, cookies.length);
0287:
0288: assertEquals("custno", cookies[0].getName());
0289: assertEquals("12345", cookies[0].getValue());
0290: assertEquals("test", cookies[0].getComment());
0291: assertEquals(0, cookies[0].getVersion());
0292: assertEquals("www.apache.org", cookies[0].getDomain());
0293: assertEquals("/", cookies[0].getPath());
0294: assertFalse(cookies[0].getSecure());
0295:
0296: assertEquals("name", cookies[1].getName());
0297: assertEquals("John", cookies[1].getValue());
0298: assertEquals(null, cookies[1].getComment());
0299: assertEquals(0, cookies[1].getVersion());
0300: assertEquals(".apache.org", cookies[1].getDomain());
0301: assertEquals("/", cookies[1].getPath());
0302: assertTrue(cookies[1].getSecure());
0303: }
0304:
0305: /**
0306: * Test parse with quoted text
0307: */
0308: public void testParse3() throws Exception {
0309: String headerValue = "name=\"Doe, John\";version=1;max-age=600;secure;domain=.apache.org";
0310: Header header = new Header("set-cookie", headerValue);
0311:
0312: CookieSpec cookiespec = new CookieSpecBase();
0313: Cookie[] cookies = cookieParse(cookiespec, "www.apache.org",
0314: 80, "/", false, header);
0315:
0316: assertEquals(1, cookies.length);
0317:
0318: assertEquals("name", cookies[0].getName());
0319: assertEquals("Doe, John", cookies[0].getValue());
0320: assertEquals(null, cookies[0].getComment());
0321: assertEquals(0, cookies[0].getVersion());
0322: assertEquals(".apache.org", cookies[0].getDomain());
0323: assertEquals("/", cookies[0].getPath());
0324: assertTrue(cookies[0].getSecure());
0325: }
0326:
0327: // see issue #5279
0328: public void testQuotedExpiresAttribute() throws Exception {
0329: String headerValue = "custno=12345;Expires='Thu, 01-Jan-2070 00:00:10 GMT'";
0330:
0331: Header header = new Header("set-cookie", headerValue);
0332:
0333: CookieSpec cookiespec = new CookieSpecBase();
0334: Cookie[] cookies = cookieParse(cookiespec, "www.apache.org",
0335: 80, "/", true, header);
0336: assertNotNull("Expected some cookies", cookies);
0337: assertEquals("Expected 1 cookie", 1, cookies.length);
0338: assertNotNull("Expected cookie to have getExpiryDate",
0339: cookies[0].getExpiryDate());
0340: }
0341:
0342: public void testSecurityError() throws Exception {
0343: String headerValue = "custno=12345;comment=test; version=1,"
0344: + "name=John;version=1;max-age=600;secure;domain=jakarta.apache.org";
0345: Header header = new Header("set-cookie", headerValue);
0346:
0347: CookieSpec cookiespec = new CookieSpecBase();
0348: try {
0349: Cookie[] cookies = cookieParse(cookiespec,
0350: "www.apache.org", 80, "/", false, header);
0351: fail("HttpException exception should have been thrown");
0352: } catch (HttpException e) {
0353: // expected
0354: }
0355: }
0356:
0357: public void testParseSimple() throws Exception {
0358: Header header = new Header("Set-Cookie",
0359: "cookie-name=cookie-value");
0360:
0361: CookieSpec cookiespec = new CookieSpecBase();
0362: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
0363: "/path/path", false, header);
0364: assertEquals("Found 1 cookie.", 1, parsed.length);
0365: assertEquals("Name", "cookie-name", parsed[0].getName());
0366: assertEquals("Value", "cookie-value", parsed[0].getValue());
0367: assertTrue("Comment", null == parsed[0].getComment());
0368: assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
0369: //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded());
0370: assertTrue("isPersistent", !parsed[0].isPersistent());
0371: assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
0372: assertEquals("Path", "/path", parsed[0].getPath());
0373: assertTrue("Secure", !parsed[0].getSecure());
0374: assertEquals("Version", 0, parsed[0].getVersion());
0375: }
0376:
0377: public void testParseSimple2() throws Exception {
0378: Header header = new Header("Set-Cookie",
0379: "cookie-name=cookie-value");
0380:
0381: CookieSpec cookiespec = new CookieSpecBase();
0382: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
0383: "/path", false, header);
0384: assertEquals("Found 1 cookie.", 1, parsed.length);
0385: assertEquals("Name", "cookie-name", parsed[0].getName());
0386: assertEquals("Value", "cookie-value", parsed[0].getValue());
0387: assertTrue("Comment", null == parsed[0].getComment());
0388: assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
0389: //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded());
0390: assertTrue("isPersistent", !parsed[0].isPersistent());
0391: assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
0392: assertEquals("Path", "/", parsed[0].getPath());
0393: assertTrue("Secure", !parsed[0].getSecure());
0394: assertEquals("Version", 0, parsed[0].getVersion());
0395: }
0396:
0397: public void testParseNoName() throws Exception {
0398: Header header = new Header("Set-Cookie", "=stuff; path=/");
0399:
0400: CookieSpec cookiespec = new CookieSpecBase();
0401: try {
0402: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
0403: "/", false, header);
0404: fail("MalformedCookieException should have been thrown");
0405: } catch (MalformedCookieException ex) {
0406: // expected
0407: }
0408: }
0409:
0410: public void testParseNoValue() throws Exception {
0411: Header header = new Header("Set-Cookie", "cookie-name=");
0412:
0413: CookieSpec cookiespec = new CookieSpecBase();
0414: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/",
0415: false, header);
0416: assertEquals("Found 1 cookie.", 1, parsed.length);
0417: assertEquals("Name", "cookie-name", parsed[0].getName());
0418: assertEquals("Value", "", parsed[0].getValue());
0419: assertTrue("Comment", null == parsed[0].getComment());
0420: assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
0421: //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded());
0422: assertTrue("isPersistent", !parsed[0].isPersistent());
0423: assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
0424: assertEquals("Path", "/", parsed[0].getPath());
0425: assertTrue("Secure", !parsed[0].getSecure());
0426: assertEquals("Version", 0, parsed[0].getVersion());
0427: }
0428:
0429: public void testParseWithWhiteSpace() throws Exception {
0430: Header header = new Header("Set-Cookie",
0431: " cookie-name = cookie-value ");
0432:
0433: CookieSpec cookiespec = new CookieSpecBase();
0434: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/",
0435: false, header);
0436: assertEquals("Found 1 cookie.", 1, parsed.length);
0437: assertEquals("Name", "cookie-name", parsed[0].getName());
0438: assertEquals("Value", "cookie-value", parsed[0].getValue());
0439: assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
0440: assertEquals("Path", "/", parsed[0].getPath());
0441: assertTrue("Secure", !parsed[0].getSecure());
0442: assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
0443: assertTrue("Comment", null == parsed[0].getComment());
0444: }
0445:
0446: public void testParseWithQuotes() throws Exception {
0447: Header header = new Header("Set-Cookie",
0448: " cookie-name = \" cookie-value \" ;path=/");
0449:
0450: CookieSpec cookiespec = new CookieSpecBase();
0451: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/",
0452: false, header);
0453: assertEquals("Found 1 cookie.", 1, parsed.length);
0454: assertEquals("Name", "cookie-name", parsed[0].getName());
0455: assertEquals("Value", " cookie-value ", parsed[0].getValue());
0456: assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
0457: assertEquals("Path", "/", parsed[0].getPath());
0458: assertTrue("Secure", !parsed[0].getSecure());
0459: assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
0460: assertTrue("Comment", null == parsed[0].getComment());
0461: }
0462:
0463: public void testParseWithPath() throws Exception {
0464: Header header = new Header("Set-Cookie",
0465: "cookie-name=cookie-value; Path=/path/");
0466:
0467: CookieSpec cookiespec = new CookieSpecBase();
0468: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
0469: "/path/path", false, header);
0470: assertEquals("Found 1 cookie.", 1, parsed.length);
0471: assertEquals("Name", "cookie-name", parsed[0].getName());
0472: assertEquals("Value", "cookie-value", parsed[0].getValue());
0473: assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
0474: assertEquals("Path", "/path/", parsed[0].getPath());
0475: assertTrue("Secure", !parsed[0].getSecure());
0476: assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
0477: assertTrue("Comment", null == parsed[0].getComment());
0478: }
0479:
0480: public void testParseWithDomain() throws Exception {
0481: Header header = new Header("Set-Cookie",
0482: "cookie-name=cookie-value; Domain=127.0.0.1");
0483:
0484: CookieSpec cookiespec = new CookieSpecBase();
0485: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/",
0486: false, header);
0487: assertEquals("Found 1 cookie.", 1, parsed.length);
0488: assertEquals("Name", "cookie-name", parsed[0].getName());
0489: assertEquals("Value", "cookie-value", parsed[0].getValue());
0490: assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
0491: assertEquals("Path", "/", parsed[0].getPath());
0492: assertTrue("Secure", !parsed[0].getSecure());
0493: assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
0494: assertTrue("Comment", null == parsed[0].getComment());
0495: }
0496:
0497: public void testParseWithSecure() throws Exception {
0498: Header header = new Header("Set-Cookie",
0499: "cookie-name=cookie-value; secure");
0500:
0501: CookieSpec cookiespec = new CookieSpecBase();
0502: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/",
0503: true, header);
0504: assertEquals("Found 1 cookie.", 1, parsed.length);
0505: assertEquals("Name", "cookie-name", parsed[0].getName());
0506: assertEquals("Value", "cookie-value", parsed[0].getValue());
0507: assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
0508: assertEquals("Path", "/", parsed[0].getPath());
0509: assertTrue("Secure", parsed[0].getSecure());
0510: assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
0511: assertTrue("Comment", null == parsed[0].getComment());
0512: }
0513:
0514: public void testParseWithComment() throws Exception {
0515: Header header = new Header("Set-Cookie",
0516: "cookie-name=cookie-value; comment=\"This is a comment.\"");
0517:
0518: CookieSpec cookiespec = new CookieSpecBase();
0519: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/",
0520: true, header);
0521: assertEquals("Found 1 cookie.", 1, parsed.length);
0522: assertEquals("Name", "cookie-name", parsed[0].getName());
0523: assertEquals("Value", "cookie-value", parsed[0].getValue());
0524: assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
0525: assertEquals("Path", "/", parsed[0].getPath());
0526: assertTrue("Secure", !parsed[0].getSecure());
0527: assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
0528: assertEquals("Comment", "This is a comment.", parsed[0]
0529: .getComment());
0530: }
0531:
0532: public void testParseWithExpires() throws Exception {
0533: Header header = new Header("Set-Cookie",
0534: "cookie-name=cookie-value;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
0535:
0536: CookieSpec cookiespec = new CookieSpecBase();
0537: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/",
0538: true, header);
0539: assertEquals("Found 1 cookie.", 1, parsed.length);
0540: assertEquals("Name", "cookie-name", parsed[0].getName());
0541: assertEquals("Value", "cookie-value", parsed[0].getValue());
0542: assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
0543: assertEquals("Path", "/", parsed[0].getPath());
0544: assertTrue("Secure", !parsed[0].getSecure());
0545: assertEquals(new Date(10000L), parsed[0].getExpiryDate());
0546: assertTrue("Comment", null == parsed[0].getComment());
0547: }
0548:
0549: public void testParseWithAll() throws Exception {
0550: Header header = new Header(
0551: "Set-Cookie",
0552: "cookie-name=cookie-value;Version=1;Path=/commons;Domain=.apache.org;"
0553: + "Comment=This is a comment.;secure;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
0554:
0555: CookieSpec cookiespec = new CookieSpecBase();
0556: Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80,
0557: "/commons/httpclient", true, header);
0558: assertEquals("Found 1 cookie.", 1, parsed.length);
0559: assertEquals("Name", "cookie-name", parsed[0].getName());
0560: assertEquals("Value", "cookie-value", parsed[0].getValue());
0561: assertEquals("Domain", ".apache.org", parsed[0].getDomain());
0562: assertEquals("Path", "/commons", parsed[0].getPath());
0563: assertTrue("Secure", parsed[0].getSecure());
0564: assertEquals(new Date(10000L), parsed[0].getExpiryDate());
0565: assertEquals("Comment", "This is a comment.", parsed[0]
0566: .getComment());
0567: assertEquals("Version", 0, parsed[0].getVersion());
0568: }
0569:
0570: public void testParseMultipleDifferentPaths() throws Exception {
0571: Header header = new Header("Set-Cookie",
0572: "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;"
0573: + "Path=/commons/httpclient;Version=1");
0574:
0575: CookieSpec cookiespec = new CookieSpecBase();
0576: Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80,
0577: "/commons/httpclient", true, header);
0578: HttpState state = new HttpState();
0579: state.addCookies(parsed);
0580: Cookie[] cookies = state.getCookies();
0581: assertEquals("Wrong number of cookies.", 2, cookies.length);
0582: assertEquals("Name", "name1", cookies[0].getName());
0583: assertEquals("Value", "value1", cookies[0].getValue());
0584: assertEquals("Name", "name1", cookies[1].getName());
0585: assertEquals("Value", "value2", cookies[1].getValue());
0586: }
0587:
0588: public void testParseMultipleSamePaths() throws Exception {
0589: Header header = new Header("Set-Cookie",
0590: "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons");
0591:
0592: CookieSpec cookiespec = new CookieSpecBase();
0593: Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80,
0594: "/commons/httpclient", true, header);
0595: HttpState state = new HttpState();
0596: state.addCookies(parsed);
0597: Cookie[] cookies = state.getCookies();
0598: assertEquals("Found 1 cookies.", 1, cookies.length);
0599: assertEquals("Name", "name1", cookies[0].getName());
0600: assertEquals("Value", "value2", cookies[0].getValue());
0601: }
0602:
0603: public void testParseRelativePath() throws Exception {
0604: Header header = new Header("Set-Cookie",
0605: "name1=value1;Path=whatever");
0606:
0607: CookieSpec cookiespec = new CookieSpecBase();
0608: Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80,
0609: "whatever", true, header);
0610: assertEquals("Found 1 cookies.", 1, parsed.length);
0611: assertEquals("Name", "name1", parsed[0].getName());
0612: assertEquals("Value", "value1", parsed[0].getValue());
0613: assertEquals("Path", "whatever", parsed[0].getPath());
0614: }
0615:
0616: public void testParseWithWrongDomain() throws Exception {
0617: Header header = new Header("Set-Cookie",
0618: "cookie-name=cookie-value; domain=127.0.0.1; version=1");
0619:
0620: CookieSpec cookiespec = new CookieSpecBase();
0621: try {
0622: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.2", 80,
0623: "/", false, header);
0624: fail("HttpException exception should have been thrown");
0625: } catch (HttpException e) {
0626: // expected
0627: }
0628: }
0629:
0630: public void testParseWithNullHost() throws Exception {
0631: Header header = new Header("Set-Cookie",
0632: "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
0633:
0634: CookieSpec cookiespec = new CookieSpecBase();
0635: try {
0636: Cookie[] parsed = cookieParse(cookiespec, null, 80, "/",
0637: false, header);
0638: fail("IllegalArgumentException should have been thrown");
0639: } catch (IllegalArgumentException e) {
0640: // expected
0641: }
0642: }
0643:
0644: public void testParseWithBlankHost() throws Exception {
0645: Header header = new Header("Set-Cookie",
0646: "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
0647:
0648: CookieSpec cookiespec = new CookieSpecBase();
0649: try {
0650: Cookie[] parsed = cookieParse(cookiespec, " ", 80, "/",
0651: false, header);
0652: fail("IllegalArgumentException should have been thrown");
0653: } catch (IllegalArgumentException e) {
0654: // expected
0655: }
0656: }
0657:
0658: public void testParseWithNullPath() throws Exception {
0659: Header header = new Header("Set-Cookie",
0660: "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
0661:
0662: CookieSpec cookiespec = new CookieSpecBase();
0663: try {
0664: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
0665: null, false, header);
0666: fail("IllegalArgumentException should have been thrown");
0667: } catch (IllegalArgumentException e) {
0668: // expected
0669: }
0670: }
0671:
0672: public void testParseWithBlankPath() throws Exception {
0673: Header header = new Header("Set-Cookie",
0674: "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
0675:
0676: CookieSpec cookiespec = new CookieSpecBase();
0677: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
0678: " ", false, header);
0679: assertNotNull(parsed);
0680: assertEquals(1, parsed.length);
0681: assertEquals("/", parsed[0].getPath());
0682: }
0683:
0684: public void testParseWithNegativePort() throws Exception {
0685: Header header = new Header("Set-Cookie",
0686: "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
0687:
0688: CookieSpec cookiespec = new CookieSpecBase();
0689: try {
0690: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", -80,
0691: null, false, header);
0692: fail("IllegalArgumentException should have been thrown");
0693: } catch (IllegalArgumentException e) {
0694: // expected
0695: }
0696: }
0697:
0698: public void testParseWithNullHostAndPath() throws Exception {
0699: Header header = new Header("Set-Cookie",
0700: "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
0701:
0702: CookieSpec cookiespec = new CookieSpecBase();
0703: try {
0704: Cookie[] parsed = cookieParse(cookiespec, null, 80, null,
0705: false, header);
0706: fail("IllegalArgumentException should have been thrown");
0707: } catch (IllegalArgumentException e) {
0708: // expected
0709: }
0710: }
0711:
0712: public void testParseWithPathMismatch() throws Exception {
0713: Header header = new Header("Set-Cookie",
0714: "cookie-name=cookie-value; path=/path/path/path");
0715:
0716: CookieSpec cookiespec = new CookieSpecBase();
0717: try {
0718: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
0719: "/path", false, header);
0720: fail("MalformedCookieException should have been thrown.");
0721: } catch (MalformedCookieException e) {
0722: // expected
0723: }
0724: }
0725:
0726: public void testParseWithPathMismatch2() throws Exception {
0727: Header header = new Header("Set-Cookie",
0728: "cookie-name=cookie-value; path=/foobar");
0729:
0730: CookieSpec cookiespec = new CookieSpecBase();
0731: try {
0732: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80,
0733: "/foo", false, header);
0734: fail("MalformedCookieException should have been thrown.");
0735: } catch (MalformedCookieException e) {
0736: // expected
0737: }
0738: }
0739:
0740: public void testParseWithInvalidHeader1() throws Exception {
0741: CookieSpec cookiespec = new CookieSpecBase();
0742: try {
0743: Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo",
0744: false, (Header) null);
0745: fail("IllegalArgumentException should have been thrown.");
0746: } catch (IllegalArgumentException e) {
0747: // expected
0748: }
0749: }
0750:
0751: public void testParseWithInvalidHeader2() throws Exception {
0752: CookieSpec cookiespec = new CookieSpecBase();
0753: try {
0754: Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo",
0755: false, (String) null);
0756: fail("IllegalArgumentException should have been thrown.");
0757: } catch (IllegalArgumentException e) {
0758: // expected
0759: }
0760: }
0761:
0762: /**
0763: * Tests if cookie constructor rejects cookie name containing blanks.
0764: */
0765: public void testCookieNameWithBlanks() throws Exception {
0766: Header setcookie = new Header("Set-Cookie", "invalid name=");
0767: CookieSpec cookiespec = new CookieSpecBase();
0768: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/",
0769: false, setcookie);
0770: assertNotNull(parsed);
0771: assertEquals(1, parsed.length);
0772: }
0773:
0774: /**
0775: * Tests if cookie constructor rejects cookie name starting with $.
0776: */
0777: public void testCookieNameStartingWithDollarSign() throws Exception {
0778: Header setcookie = new Header("Set-Cookie", "$invalid_name=");
0779: CookieSpec cookiespec = new CookieSpecBase();
0780: Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/",
0781: false, setcookie);
0782: assertNotNull(parsed);
0783: assertEquals(1, parsed.length);
0784: }
0785:
0786: /**
0787: * Tests if malformatted expires attribute is parsed correctly.
0788: */
0789: public void testCookieWithComma() throws Exception {
0790: Header header = new Header("Set-Cookie",
0791: "name=value; expires=\"Thu, 01-Jan-1970 00:00:00 GMT");
0792:
0793: CookieSpec cookiespec = new CookieSpecBase();
0794: try {
0795: Cookie[] cookies = cookiespec.parse("localhost", 80, "/",
0796: false, header);
0797: fail("MalformedCookieException should have been thrown");
0798: } catch (MalformedCookieException expected) {
0799: }
0800: }
0801:
0802: /**
0803: * Tests several date formats.
0804: */
0805: public void testDateFormats() throws Exception {
0806: //comma, dashes
0807: checkDate("Thu, 01-Jan-70 00:00:10 GMT");
0808: checkDate("Thu, 01-Jan-2070 00:00:10 GMT");
0809: //no comma, dashes
0810: checkDate("Thu 01-Jan-70 00:00:10 GMT");
0811: checkDate("Thu 01-Jan-2070 00:00:10 GMT");
0812: //comma, spaces
0813: checkDate("Thu, 01 Jan 70 00:00:10 GMT");
0814: checkDate("Thu, 01 Jan 2070 00:00:10 GMT");
0815: //no comma, spaces
0816: checkDate("Thu 01 Jan 70 00:00:10 GMT");
0817: checkDate("Thu 01 Jan 2070 00:00:10 GMT");
0818: //weird stuff
0819: checkDate("Wed, 20-Nov-2002 09-38-33 GMT");
0820:
0821: try {
0822: checkDate("this aint a date");
0823: fail("Date check is bogous");
0824: } catch (Exception e) {
0825: /* must fail */
0826: }
0827: }
0828:
0829: private void checkDate(String date) throws Exception {
0830: Header header = new Header("Set-Cookie",
0831: "custno=12345;Expires='" + date + "';");
0832: HttpParams params = new DefaultHttpParamsFactory()
0833: .getDefaultParams();
0834: CookieSpec cookiespec = new CookieSpecBase();
0835: cookiespec.setValidDateFormats((Collection) params
0836: .getParameter(HttpMethodParams.DATE_PATTERNS));
0837: cookieParse(cookiespec, "localhost", 80, "/", false, header);
0838: }
0839:
0840: /**
0841: * Tests if invalid second domain level cookie gets accepted in the
0842: * browser compatibility mode.
0843: */
0844: public void testSecondDomainLevelCookie() throws Exception {
0845: Cookie cookie = new Cookie(".sourceforge.net", "name", null,
0846: "/", null, false);
0847: cookie.setDomainAttributeSpecified(true);
0848: cookie.setPathAttributeSpecified(true);
0849:
0850: CookieSpec cookiespec = new CookieSpecBase();
0851: cookiespec.validate("sourceforge.net", 80, "/", false, cookie);
0852: }
0853:
0854: public void testSecondDomainLevelCookieMatch1() throws Exception {
0855: Cookie cookie = new Cookie(".sourceforge.net", "name", null,
0856: "/", null, false);
0857: cookie.setDomainAttributeSpecified(true);
0858: cookie.setPathAttributeSpecified(true);
0859:
0860: CookieSpec cookiespec = new CookieSpecBase();
0861: assertTrue(cookiespec.match("sourceforge.net", 80, "/", false,
0862: cookie));
0863: }
0864:
0865: public void testSecondDomainLevelCookieMatch2() throws Exception {
0866: Cookie cookie = new Cookie("sourceforge.net", "name", null,
0867: "/", null, false);
0868: cookie.setDomainAttributeSpecified(true);
0869: cookie.setPathAttributeSpecified(true);
0870:
0871: CookieSpec cookiespec = new CookieSpecBase();
0872: assertTrue(cookiespec.match("www.sourceforge.net", 80, "/",
0873: false, cookie));
0874: }
0875:
0876: public void testSecondDomainLevelCookieMatch3() throws Exception {
0877: Cookie cookie = new Cookie(".sourceforge.net", "name", null,
0878: "/", null, false);
0879: cookie.setDomainAttributeSpecified(true);
0880: cookie.setPathAttributeSpecified(true);
0881:
0882: CookieSpec cookiespec = new CookieSpecBase();
0883: assertTrue(cookiespec.match("www.sourceforge.net", 80, "/",
0884: false, cookie));
0885: }
0886:
0887: public void testInvalidSecondDomainLevelCookieMatch1()
0888: throws Exception {
0889: Cookie cookie = new Cookie(".sourceforge.net", "name", null,
0890: "/", null, false);
0891: cookie.setDomainAttributeSpecified(true);
0892: cookie.setPathAttributeSpecified(true);
0893:
0894: CookieSpec cookiespec = new CookieSpecBase();
0895: assertFalse(cookiespec.match("antisourceforge.net", 80, "/",
0896: false, cookie));
0897: }
0898:
0899: public void testInvalidSecondDomainLevelCookieMatch2()
0900: throws Exception {
0901: Cookie cookie = new Cookie("sourceforge.net", "name", null,
0902: "/", null, false);
0903: cookie.setDomainAttributeSpecified(true);
0904: cookie.setPathAttributeSpecified(true);
0905:
0906: CookieSpec cookiespec = new CookieSpecBase();
0907: assertFalse(cookiespec.match("antisourceforge.net", 80, "/",
0908: false, cookie));
0909: }
0910:
0911: public void testMatchNullHost() throws Exception {
0912: CookieSpec cookiespec = new CookieSpecBase();
0913: Cookie cookie = new Cookie();
0914: try {
0915: cookiespec.match(null, 80, "/", false, cookie);
0916: fail("IllegalArgumentException must have been thrown");
0917: } catch (IllegalArgumentException expected) {
0918: }
0919: }
0920:
0921: public void testMatchBlankHost() throws Exception {
0922: CookieSpec cookiespec = new CookieSpecBase();
0923: Cookie cookie = new Cookie();
0924: try {
0925: cookiespec.match(" ", 80, "/", false, cookie);
0926: fail("IllegalArgumentException must have been thrown");
0927: } catch (IllegalArgumentException expected) {
0928: }
0929: }
0930:
0931: public void testMatchInvalidPort() throws Exception {
0932: CookieSpec cookiespec = new CookieSpecBase();
0933: Cookie cookie = new Cookie();
0934: try {
0935: cookiespec.match("host", -80, "/", false, cookie);
0936: fail("IllegalArgumentException must have been thrown");
0937: } catch (IllegalArgumentException expected) {
0938: }
0939: }
0940:
0941: public void testMatchNullPath() throws Exception {
0942: CookieSpec cookiespec = new CookieSpecBase();
0943: Cookie cookie = new Cookie();
0944: try {
0945: cookiespec.match("host", 80, null, false, cookie);
0946: fail("IllegalArgumentException must have been thrown");
0947: } catch (IllegalArgumentException expected) {
0948: }
0949: }
0950:
0951: public void testMatchBlankPath() throws Exception {
0952: CookieSpec cookiespec = new CookieSpecBase();
0953: Cookie cookie = new Cookie("host", "name", "value", "/", null,
0954: false);
0955: assertTrue(cookiespec.match("host", 80, " ", false, cookie));
0956: }
0957:
0958: public void testMatchNullCookie() throws Exception {
0959: CookieSpec cookiespec = new CookieSpecBase();
0960: try {
0961: cookiespec.match("host", 80, "/", false, (Cookie) null);
0962: fail("IllegalArgumentException must have been thrown");
0963: } catch (IllegalArgumentException expected) {
0964: }
0965: }
0966:
0967: public void testMatchNullCookieDomain() throws Exception {
0968: CookieSpec cookiespec = new CookieSpecBase();
0969: Cookie cookie = new Cookie(null, "name", "value", "/", null,
0970: false);
0971: assertFalse(cookiespec.match("host", 80, "/", false, cookie));
0972: }
0973:
0974: public void testMatchNullCookiePath() throws Exception {
0975: CookieSpec cookiespec = new CookieSpecBase();
0976: Cookie cookie = new Cookie("host", "name", "value", null, null,
0977: false);
0978: assertFalse(cookiespec.match("host", 80, "/", false, cookie));
0979: }
0980:
0981: public void testCookieMatch1() throws Exception {
0982: CookieSpec cookiespec = new CookieSpecBase();
0983: Cookie cookie = new Cookie("host", "name", "value", "/", null,
0984: false);
0985: assertTrue(cookiespec.match("host", 80, "/", false, cookie));
0986: }
0987:
0988: public void testCookieMatch2() throws Exception {
0989: CookieSpec cookiespec = new CookieSpecBase();
0990: Cookie cookie = new Cookie(".whatever.com", "name", "value",
0991: "/", null, false);
0992: assertTrue(cookiespec.match(".whatever.com", 80, "/", false,
0993: cookie));
0994: }
0995:
0996: public void testCookieMatch3() throws Exception {
0997: CookieSpec cookiespec = new CookieSpecBase();
0998: Cookie cookie = new Cookie(".whatever.com", "name", "value",
0999: "/", null, false);
1000: assertTrue(cookiespec.match(".really.whatever.com", 80, "/",
1001: false, cookie));
1002: }
1003:
1004: public void testCookieMatch4() throws Exception {
1005: CookieSpec cookiespec = new CookieSpecBase();
1006: Cookie cookie = new Cookie("host", "name", "value", "/", null,
1007: false);
1008: assertTrue(cookiespec.match("host", 80, "/foobar", false,
1009: cookie));
1010: }
1011:
1012: public void testCookieMismatch1() throws Exception {
1013: CookieSpec cookiespec = new CookieSpecBase();
1014: Cookie cookie = new Cookie("host1", "name", "value", "/", null,
1015: false);
1016: assertFalse(cookiespec.match("host2", 80, "/", false, cookie));
1017: }
1018:
1019: public void testCookieMismatch2() throws Exception {
1020: CookieSpec cookiespec = new CookieSpecBase();
1021: Cookie cookie = new Cookie(".aaaaaaaaa.com", "name", "value",
1022: "/", null, false);
1023: assertFalse(cookiespec.match(".bbbbbbbb.com", 80, "/", false,
1024: cookie));
1025: }
1026:
1027: public void testCookieMismatch3() throws Exception {
1028: CookieSpec cookiespec = new CookieSpecBase();
1029: Cookie cookie = new Cookie("host", "name", "value", "/foobar",
1030: null, false);
1031: assertFalse(cookiespec.match("host", 80, "/foo", false, cookie));
1032: }
1033:
1034: public void testCookieMismatch4() throws Exception {
1035: CookieSpec cookiespec = new CookieSpecBase();
1036: Cookie cookie = new Cookie("host", "name", "value", "/foobar",
1037: null, true);
1038: assertFalse(cookiespec.match("host", 80, "/foobar/", false,
1039: cookie));
1040: }
1041:
1042: public void testCookieMatch5() throws Exception {
1043: CookieSpec cookiespec = new CookieSpecBase();
1044: Cookie cookie = new Cookie("host", "name", "value",
1045: "/foobar/r", null, false);
1046: assertFalse(cookiespec.match("host", 80, "/foobar/", false,
1047: cookie));
1048: }
1049:
1050: public void testCookieMismatch6() throws Exception {
1051: CookieSpec cookiespec = new CookieSpecBase();
1052: Cookie cookie = new Cookie("host", "name", "value", "/foobar",
1053: null, true);
1054: assertFalse(cookiespec.match("host", 80, "/foobar", false,
1055: cookie));
1056: }
1057:
1058: public void testMatchNullCookies() throws Exception {
1059: CookieSpec cookiespec = new CookieSpecBase();
1060: Cookie[] matched = cookiespec.match("host", 80, "/foobar",
1061: false, (Cookie[]) null);
1062: assertNull(matched);
1063: }
1064:
1065: public void testMatchedCookiesOrder() throws Exception {
1066: CookieSpec cookiespec = new CookieSpecBase();
1067: Cookie[] cookies = {
1068: new Cookie("host", "nomatch", "value", "/noway", null,
1069: false),
1070: new Cookie("host", "name2", "value", "/foobar/yada",
1071: null, false),
1072: new Cookie("host", "name3", "value", "/foobar", null,
1073: false),
1074: new Cookie("host", "name1", "value",
1075: "/foobar/yada/yada", null, false) };
1076: Cookie[] matched = cookiespec.match("host", 80,
1077: "/foobar/yada/yada", false, cookies);
1078: assertNotNull(matched);
1079: assertEquals(3, matched.length);
1080: assertEquals("name1", matched[0].getName());
1081: assertEquals("name2", matched[1].getName());
1082: assertEquals("name3", matched[2].getName());
1083: }
1084:
1085: public void testInvalidMatchDomain() throws Exception {
1086: Cookie cookie = new Cookie("beta.gamma.com", "name", null, "/",
1087: null, false);
1088: cookie.setDomainAttributeSpecified(true);
1089: cookie.setPathAttributeSpecified(true);
1090:
1091: CookieSpec cookiespec = new CookieSpecBase();
1092: cookiespec.validate("alpha.beta.gamma.com", 80, "/", false,
1093: cookie);
1094: assertTrue(cookiespec.match("alpha.beta.gamma.com", 80, "/",
1095: false, cookie));
1096: }
1097:
1098: public void testFormatInvalidCookie() throws Exception {
1099: CookieSpec cookiespec = new CookieSpecBase();
1100: try {
1101: String s = cookiespec.formatCookie(null);
1102: fail("IllegalArgumentException nust have been thrown");
1103: } catch (IllegalArgumentException expected) {
1104: }
1105: }
1106:
1107: /**
1108: * Tests generic cookie formatting.
1109: */
1110: public void testGenericCookieFormatting() throws Exception {
1111: Header header = new Header("Set-Cookie",
1112: "name=value; path=/; domain=.mydomain.com");
1113: CookieSpec cookiespec = new CookieSpecBase();
1114: Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80,
1115: "/", false, header);
1116: cookiespec.validate("myhost.mydomain.com", 80, "/", false,
1117: cookies[0]);
1118: String s = cookiespec.formatCookie(cookies[0]);
1119: assertEquals("name=value", s);
1120: }
1121:
1122: public void testGenericCookieFormattingAsHeader() throws Exception {
1123: Header header = new Header("Set-Cookie",
1124: "name=value; path=/; domain=.mydomain.com");
1125: CookieSpec cookiespec = new CookieSpecBase();
1126: Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80,
1127: "/", false, header);
1128: cookiespec.validate("myhost.mydomain.com", 80, "/", false,
1129: cookies[0]);
1130: Header cookieheader = cookiespec.formatCookieHeader(cookies[0]);
1131: assertEquals("name=value", cookieheader.getValue());
1132: }
1133:
1134: /**
1135: * Tests if null cookie values are handled correctly.
1136: */
1137: public void testNullCookieValueFormatting() {
1138: Cookie cookie = new Cookie(".whatever.com", "name", null, "/",
1139: null, false);
1140: cookie.setDomainAttributeSpecified(true);
1141: cookie.setPathAttributeSpecified(true);
1142:
1143: CookieSpec cookiespec = new CookieSpecBase();
1144: String s = cookiespec.formatCookie(cookie);
1145: assertEquals("name=", s);
1146: }
1147:
1148: public void testFormatInvalidCookies() throws Exception {
1149: CookieSpec cookiespec = new CookieSpecBase();
1150: try {
1151: String s = cookiespec.formatCookies(null);
1152: fail("IllegalArgumentException nust have been thrown");
1153: } catch (IllegalArgumentException expected) {
1154: }
1155: }
1156:
1157: public void testFormatZeroCookies() throws Exception {
1158: CookieSpec cookiespec = new CookieSpecBase();
1159: try {
1160: String s = cookiespec.formatCookies(new Cookie[] {});
1161: fail("IllegalArgumentException nust have been thrown");
1162: } catch (IllegalArgumentException expected) {
1163: }
1164: }
1165:
1166: /**
1167: * Tests generic cookie formatting.
1168: */
1169: public void testFormatSeveralCookies() throws Exception {
1170: Header header = new Header(
1171: "Set-Cookie",
1172: "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
1173: CookieSpec cookiespec = new CookieSpecBase();
1174: Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80,
1175: "/", false, header);
1176: String s = cookiespec.formatCookies(cookies);
1177: assertEquals("name1=value1; name2=value2", s);
1178: }
1179:
1180: public void testFormatOneCookie() throws Exception {
1181: Header header = new Header("Set-Cookie",
1182: "name1=value1; path=/; domain=.mydomain.com;");
1183: CookieSpec cookiespec = new CookieSpecBase();
1184: Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80,
1185: "/", false, header);
1186: String s = cookiespec.formatCookies(cookies);
1187: assertEquals("name1=value1", s);
1188: }
1189:
1190: public void testFormatSeveralCookiesAsHeader() throws Exception {
1191: Header header = new Header(
1192: "Set-Cookie",
1193: "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
1194: CookieSpec cookiespec = new CookieSpecBase();
1195: Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80,
1196: "/", false, header);
1197: Header cookieheader = cookiespec.formatCookieHeader(cookies);
1198: assertEquals("name1=value1; name2=value2", cookieheader
1199: .getValue());
1200: }
1201:
1202: public void testKeepCloverHappy() throws Exception {
1203: MalformedCookieException ex1 = new MalformedCookieException();
1204: MalformedCookieException ex2 = new MalformedCookieException(
1205: "whatever");
1206: MalformedCookieException ex3 = new MalformedCookieException(
1207: "whatever", null);
1208: }
1209:
1210: }
|