hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r581953 [2/2] - in /jakarta/httpcomponents/httpclient/trunk/module-client/src: main/java/org/apache/http/impl/client/ main/java/org/apache/http/impl/cookie/ test/java/org/apache/http/impl/cookie/
Date Thu, 04 Oct 2007 15:53:55 GMT
Copied: jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java (from r581883, jakarta/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java)
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java?p2=jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java&p1=jakarta/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java&r1=581883&r2=581953&rev=581953&view=diff
==============================================================================
--- jakarta/httpcomponents/oac.hc3x/trunk/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2965Spec.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/module-client/src/test/java/org/apache/http/impl/cookie/TestCookieRFC2965Spec.java Thu Oct  4 08:53:50 2007
@@ -1,5 +1,5 @@
 /*
- * $Header: $
+ * $HeadURL$
  * $Revision$
  * $Date$
  * ====================================================================
@@ -27,22 +27,28 @@
  *
  */
 
-package org.apache.commons.httpclient.cookie;
+package org.apache.http.impl.cookie;
 
 import junit.framework.Test;
+import junit.framework.TestCase;
 import junit.framework.TestSuite;
 
-import org.apache.commons.httpclient.Cookie;
-import org.apache.commons.httpclient.Header;
-
 import java.util.Date;
 
+import org.apache.http.Header;
+import org.apache.http.cookie.ClientCookie;
+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 org.apache.http.message.BasicHeader;
+
 /**
  * Test cases for RFC2965 cookie spec
  *
  * @author jain.samit@gmail.com (Samit Jain)
  */
-public class TestCookieRFC2965Spec extends TestCookieBase {
+public class TestCookieRFC2965Spec extends TestCase {
 
     // ------------------------------------------------------------ Constructor
 
@@ -56,127 +62,105 @@
         return new TestSuite(TestCookieRFC2965Spec.class);
     }
 
-
     // ------------------------------------------------------- Test Cookie Parsing
 
     /**
-     * Test <tt>parse</tt> with invalid params.
-     */
-    public void testParseInvalidParams() throws Exception {
-        CookieSpec cookiespec = new RFC2965Spec();
-        try {
-            // invalid header
-            cookiespec.parse("www.domain.com", 80, "/", false, (Header) null /* header */);
-            fail("IllegalArgumentException must have been thrown");
-        } catch (IllegalArgumentException expected) {}
-
-        Header header = new Header("Set-Cookie2", "name=value;Version=1");
-        try {
-            // invalid request host
-            cookiespec.parse(null /* host */, 80, "/", false, header);
-            fail("IllegalArgumentException must have been thrown");
-        } catch (IllegalArgumentException expected) {}
-        try {
-            // invalid request port
-            cookiespec.parse("www.domain.com", -32 /* port */, "/", false, header);
-            fail("IllegalArgumentException must have been thrown");
-        } catch (IllegalArgumentException expected) {}
-        try {
-            // invalid request path
-            cookiespec.parse("www.domain.com", 80, null /* path */, false, header);
-            fail("IllegalArgumentException must have been thrown");
-        } catch (IllegalArgumentException expected) {}
-    }
-
-    /**
      * Test parsing cookie <tt>"Path"</tt> attribute.
      */
     public void testParsePath() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Path=/;Version=1;Path=");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Path=/;Version=1;Path=");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        // only the first occurence of path attribute is considered, others ignored
-        Cookie2 cookie = (Cookie2) parsed[0];
+        // only the first occurrence of path attribute is considered, others ignored
+        ClientCookie cookie = (ClientCookie) parsed[0];
         assertEquals("/", cookie.getPath());
-        assertTrue(cookie.isPathAttributeSpecified());
+        assertTrue(cookie.containsAttribute(ClientCookie.PATH_ATTR));
     }
 
     public void testParsePathDefault() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/path/", false);
         // Path is OPTIONAL, defaults to the request path
-        Header header = new Header("Set-Cookie2", "name=value;Version=1");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/path" /* request path */, false, header);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         assertEquals("/path", cookie.getPath());
-        assertFalse(cookie.isPathAttributeSpecified());
+        assertFalse(cookie.containsAttribute(ClientCookie.PATH_ATTR));
     }
 
     public void testParseNullPath() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Path=;Version=1");
-        try {
-            cookiespec.parse("www.domain.com", 80, "/", false, header);
-            fail("MalformedCookieException should have been thrown");
-        } catch (MalformedCookieException ex) {
-            // expected
-        }
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Path=;Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
+        assertNotNull(parsed);
+        assertEquals(1, parsed.length);
+        ClientCookie cookie = (ClientCookie) parsed[0];
+        assertEquals("/", cookie.getPath());
+        assertTrue(cookie.containsAttribute(ClientCookie.PATH_ATTR));
     }
 
     public void testParseBlankPath() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Path=\"   \";Version=1");
-        try {
-            cookiespec.parse("www.domain.com", 80, "/", false, header);
-            fail("MalformedCookieException should have been thrown");
-        } catch (MalformedCookieException ex) {
-            // expected
-        }
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Path=\"   \";Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
+        assertNotNull(parsed);
+        assertEquals(1, parsed.length);
+        ClientCookie cookie = (ClientCookie) parsed[0];
+        assertEquals("/", cookie.getPath());
+        assertTrue(cookie.containsAttribute(ClientCookie.PATH_ATTR));
     }
+    
     /**
      * Test parsing cookie <tt>"Domain"</tt> attribute.
      */
     public void testParseDomain() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Domain=.domain.com;Version=1;Domain=");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Domain=.domain.com;Version=1;Domain=");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        // only the first occurence of domain attribute is considered, others ignored
-        Cookie2 cookie = (Cookie2) parsed[0];
+        // only the first occurrence of domain attribute is considered, others ignored
+        ClientCookie cookie = (ClientCookie) parsed[0];
         assertEquals(".domain.com", cookie.getDomain());
-        assertTrue(cookie.isDomainAttributeSpecified());
+        assertTrue(cookie.containsAttribute(ClientCookie.DOMAIN_ATTR));
 
         // should put a leading dot if there is no dot in front of domain
-        header = new Header("Set-Cookie2", "name=value;Domain=domain.com;Version=1");
-        parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        header = new BasicHeader("Set-Cookie2", "name=value;Domain=domain.com;Version=1");
+        parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        cookie = (Cookie2) parsed[0];
+        cookie = (ClientCookie) parsed[0];
         assertEquals(".domain.com", cookie.getDomain());
     }
 
     public void testParseDomainDefault() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
         // Domain is OPTIONAL, defaults to the request host
-        Header header = new Header("Set-Cookie2", "name=value;Version=1");
-        Cookie[] parsed = cookiespec.parse("www.domain.com" /* request host */, 80, "/", false, header);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         assertEquals("www.domain.com", cookie.getDomain());
-        assertFalse(cookie.isDomainAttributeSpecified());
+        assertFalse(cookie.containsAttribute(ClientCookie.DOMAIN_ATTR));
     }
 
     public void testParseNullDomain() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
         // domain cannot be null
-        Header header = new Header("Set-Cookie2", "name=value;Domain=;Version=1");
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Domain=;Version=1");
         try {
-            cookiespec.parse("www.domain.com", 80, "/", false, header);
+            cookiespec.parse(header, origin);
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException ex) {
             // expected
@@ -185,9 +169,10 @@
 
     public void testParseBlankDomain() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Domain=\"   \";Version=1");
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Domain=\"   \";Version=1");
         try {
-            cookiespec.parse("www.domain.com", 80, "/", false, header);
+            cookiespec.parse(header, origin);
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException ex) {
             // expected
@@ -199,67 +184,72 @@
      */
     public void testParsePort() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Port=\"80,800,8000\";Version=1;Port=nonsense");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Port=\"80,800,8000\";Version=1;Port=nonsense");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        // only the first occurence of port attribute is considered, others ignored
-        Cookie2 cookie = (Cookie2) parsed[0];
+        // only the first occurrence of port attribute is considered, others ignored
+        ClientCookie cookie = (ClientCookie) parsed[0];
         int[] ports = cookie.getPorts();
         assertNotNull(ports);
         assertEquals(3, ports.length);
         assertEquals(80, ports[0]);
         assertEquals(800, ports[1]);
         assertEquals(8000, ports[2]);
-        assertTrue(cookie.isPortAttributeSpecified());
+        assertTrue(cookie.containsAttribute(ClientCookie.PORT_ATTR));
     }
 
     public void testParsePortDefault() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
         // Port is OPTIONAL, cookie can be accepted from any port
-        Header header = new Header("Set-Cookie2", "name=value;Version=1");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
-        assertFalse(cookie.isPortAttributeSpecified());
+        ClientCookie cookie = (ClientCookie) parsed[0];
+        assertFalse(cookie.containsAttribute(ClientCookie.PORT_ATTR));
     }
 
     public void testParseNullPort() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
         // null port defaults to request port
-        Header header = new Header("Set-Cookie2", "name=value;Port=;Version=1");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80 /* request port */, "/", false, header);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Port=;Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         int[] ports = cookie.getPorts();
         assertNotNull(ports);
         assertEquals(1, ports.length);
         assertEquals(80, ports[0]);
-        assertTrue(cookie.isPortAttributeSpecified() && cookie.isPortAttributeBlank());
+        assertEquals("", cookie.getAttribute(ClientCookie.PORT_ATTR));
     }
 
     public void testParseBlankPort() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
         // blank port defaults to request port
-        Header header = new Header("Set-Cookie2", "name=value;Port=\"  \";Version=1");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80 /* request port */, "/", false, header);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Port=\"  \";Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         int[] ports = cookie.getPorts();
         assertNotNull(ports);
         assertEquals(1, ports.length);
         assertEquals(80, ports[0]);
-        assertTrue(cookie.isPortAttributeSpecified() && cookie.isPortAttributeBlank());
+        assertEquals("  ", cookie.getAttribute(ClientCookie.PORT_ATTR));
     }
 
     public void testParseInvalidPort() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Port=nonsense;Version=1");
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Port=nonsense;Version=1");
         try {
-            cookiespec.parse("www.domain.com", 80, "/", false, header);
+            cookiespec.parse(header, origin);
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException ex) {
             // expected
@@ -268,9 +258,10 @@
 
     public void testParseNegativePort() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Port=\"80,-800,8000\";Version=1");
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Port=\"80,-800,8000\";Version=1");
         try {
-            cookiespec.parse("www.domain.com", 80, "/", false, header);
+            cookiespec.parse(header, origin);
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException ex) {
             // expected
@@ -282,11 +273,12 @@
      */
     public void testParseNameValue() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Version=1;");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Version=1;");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         assertEquals("name", cookie.getName());
         assertEquals("value", cookie.getValue());
     }
@@ -296,21 +288,23 @@
      */
     public void testParseVersion() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Version=1;");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Version=1;");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         assertEquals(1, cookie.getVersion());
-        assertTrue(cookie.isVersionAttributeSpecified());
+        assertTrue(cookie.containsAttribute(ClientCookie.VERSION_ATTR));
     }
 
     public void testParseNullVersion() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        // version cannot ne null
-        Header header = new Header("Set-Cookie2", "name=value;Version=;");
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        // version cannot be null
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Version=;");
         try {
-            cookiespec.parse("www.domain.com", 80, "/", false, header);
+            cookiespec.parse(header, origin);
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException ex) {
             // expected
@@ -319,9 +313,10 @@
     
     public void testParseNegativeVersion() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Version=-1;");
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Version=-1;");
         try {
-            cookiespec.parse("www.domain.com", 80, "/", false, header);
+            cookiespec.parse(header, origin);
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException ex) {
             // expected
@@ -332,31 +327,34 @@
      */
     public void testParseMaxage() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Max-age=3600;Version=1;Max-age=nonsense");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Max-age=3600;Version=1;Max-age=nonsense");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
         // only the first occurence of max-age attribute is considered, others ignored
-        Cookie2 cookie = (Cookie2) parsed[0];
-        assertFalse(cookie.isExpired());
+        ClientCookie cookie = (ClientCookie) parsed[0];
+        assertFalse(cookie.isExpired(new Date()));
     }
 
     public void testParseMaxageDefault() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
         // Max-age is OPTIONAL, defaults to session cookie
-        Header header = new Header("Set-Cookie2", "name=value;Version=1");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         assertFalse(cookie.isPersistent());
     }
 
     public void testParseNullMaxage() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Max-age=;Version=1");
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Max-age=;Version=1");
         try {
-            cookiespec.parse("www.domain.com", 80, "/", false, header);
+            cookiespec.parse(header, origin);
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException ex) {
             // expected
@@ -365,9 +363,10 @@
 
     public void testParseNegativeMaxage() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Max-age=-3600;Version=1;");
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Max-age=-3600;Version=1;");
         try {
-            cookiespec.parse("www.domain.com", 80, "/", false, header);
+            cookiespec.parse(header, origin);
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException ex) {
             // expected
@@ -379,12 +378,13 @@
      */
     public void testParseSecure() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Secure;Version=1");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Secure;Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
-        assertTrue(cookie.getSecure());
+        ClientCookie cookie = (ClientCookie) parsed[0];
+        assertTrue(cookie.isSecure());
     }
 
     /**
@@ -392,20 +392,21 @@
      */
     public void testParseDiscard() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Discard;Max-age=36000;Version=1");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Discard;Max-age=36000;Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         // discard overrides max-age
         assertFalse(cookie.isPersistent());
 
         // Discard is OPTIONAL, default behavior is dictated by max-age
-        header = new Header("Set-Cookie2", "name=value;Max-age=36000;Version=1");
-        parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        header = new BasicHeader("Set-Cookie2", "name=value;Max-age=36000;Version=1");
+        parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        cookie = (Cookie2) parsed[0];
+        cookie = (ClientCookie) parsed[0];
         assertTrue(cookie.isPersistent());
     }
 
@@ -415,23 +416,24 @@
      */
     public void testParseOtherAttributes() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Comment=\"good cookie\";" +
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Comment=\"good cookie\";" +
                 "CommentURL=\"www.domain.com/goodcookie/\";Secure;Version=1");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         assertEquals("good cookie", cookie.getComment());
         assertEquals("www.domain.com/goodcookie/", cookie.getCommentURL());
-        assertTrue(cookie.getSecure());
+        assertTrue(cookie.isSecure());
 
         // Comment, CommentURL, Secure are OPTIONAL
-        header = new Header("Set-Cookie2", "name=value;Version=1");
-        parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        header = new BasicHeader("Set-Cookie2", "name=value;Version=1");
+        parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        cookie = (Cookie2) parsed[0];
-        assertFalse(cookie.getSecure());
+        cookie = (ClientCookie) parsed[0];
+        assertFalse(cookie.isSecure());
     }
 
     /**
@@ -439,8 +441,9 @@
      */
     public void testCookiesWithComma() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "a=b,c");
-        Cookie[] parsed = cookiespec.parse("www.domain.com", 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "a=b,c");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(2, parsed.length);
         assertEquals("a", parsed[0].getName());
@@ -457,11 +460,15 @@
      */
     public void testValidateNoDomain() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Version=1");
-        Cookie[] parsed = cookieParse(cookiespec, "www.domain.com" /* request host */, 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
+        for (int i = 0; i < parsed.length; i++) {
+            cookiespec.validate(parsed[i], origin);
+        }
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         // cookie domain must string match request host
         assertEquals("www.domain.com", cookie.getDomain());
     }
@@ -472,27 +479,39 @@
      */
     public void testValidateDomainLeadingDot() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;Domain=domain.com;Version=1");
-        Cookie[] parsed = cookieParse(cookiespec, "www.domain.com", 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value;Domain=domain.com;Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
+        for (int i = 0; i < parsed.length; i++) {
+            cookiespec.validate(parsed[i], origin);
+        }
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         assertEquals(".domain.com", cookie.getDomain());
     }
 
     /**
-     * Test <tt>Domain</tt> validation. Domain must have atleast one embedded dot.
+     * Test <tt>Domain</tt> validation. Domain must have at least one embedded dot.
      */
     public void testValidateDomainEmbeddedDot() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value; domain=.com; version=1");
+        CookieOrigin origin = new CookieOrigin("b.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value; domain=.com; version=1");
         try {
-            cookieParse(cookiespec, "b.com", 80, "/", false, header);
+            Cookie[] parsed = cookiespec.parse(header, origin);
+            for (int i = 0; i < parsed.length; i++) {
+                cookiespec.validate(parsed[i], origin);
+            }
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException expected) {}
 
-        header = new Header("Set-Cookie2", "name=value;Domain=domain.com;Version=1");
-        Cookie[] parsed = cookieParse(cookiespec, "www.domain.com", 80, "/", false, header);
+        origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        header = new BasicHeader("Set-Cookie2", "name=value;Domain=domain.com;Version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
+        for (int i = 0; i < parsed.length; i++) {
+            cookiespec.validate(parsed[i], origin);
+        }
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
     }
@@ -504,19 +523,26 @@
      */
     public void testValidateDomainLocal() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin = new CookieOrigin("simplehost", 80, "/", false);
         // when domain is specified as .local, simple host names are valid
-        Header header = new Header("Set-Cookie2", "name=value; domain=.local; version=1");
-        Cookie[] parsed = cookieParse(cookiespec, "simplehost" /* request host */, 80, "/", false, header);
+        Header header = new BasicHeader("Set-Cookie2", "name=value; domain=.local; version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
+        for (int i = 0; i < parsed.length; i++) {
+            cookiespec.validate(parsed[i], origin);
+        }
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         assertEquals(".local", cookie.getDomain());
 
         // when domain is NOT specified as .local, simple host names are invalid
-        header = new Header("Set-Cookie2", "name=value; domain=domain.com; version=1");
+        header = new BasicHeader("Set-Cookie2", "name=value; domain=domain.com; version=1");
         try {
             // since domain is not .local, this must fail
-            parsed = cookieParse(cookiespec, "simplehost" /* request host */, 80, "/", false, header);
+            parsed = cookiespec.parse(header, origin);
+            for (int i = 0; i < parsed.length; i++) {
+                cookiespec.validate(parsed[i], origin);
+            }
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException expected) {}
     }
@@ -530,15 +556,23 @@
         CookieSpec cookiespec = new RFC2965Spec();
 
         // cookie domain does not domain-match request host
-        Header header = new Header("Set-Cookie2", "name=value; domain=.domain.com; version=1");
+        Header header = new BasicHeader("Set-Cookie2", "name=value; domain=.domain.com; version=1");
         try {
-            cookieParse(cookiespec, "www.domain.org" /* request host */, 80, "/", false, header);
+            CookieOrigin origin = new CookieOrigin("www.domain.org", 80, "/", false);
+            Cookie[] parsed = cookiespec.parse(header, origin);
+            for (int i = 0; i < parsed.length; i++) {
+                cookiespec.validate(parsed[i], origin);
+            }
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException expected) {}
 
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
         // cookie domain domain-matches request host
-        header = new Header("Set-Cookie2", "name=value; domain=.domain.com; version=1");
-        Cookie[] parsed = cookieParse(cookiespec, "www.domain.com" /* request host */, 80, "/", false, header);
+        header = new BasicHeader("Set-Cookie2", "name=value; domain=.domain.com; version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
+        for (int i = 0; i < parsed.length; i++) {
+            cookiespec.validate(parsed[i], origin);
+        }
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
     }
@@ -549,9 +583,13 @@
      */
     public void testValidateDomainIllegal() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value; domain=.domain.com; version=1");
+        CookieOrigin origin = new CookieOrigin("a.b.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie2", "name=value; domain=.domain.com; version=1");
         try {
-            cookieParse(cookiespec, "a.b.domain.com" /* request host */, 80, "/", false, header);
+            Cookie[] parsed = cookiespec.parse(header, origin);
+            for (int i = 0; i < parsed.length; i++) {
+                cookiespec.validate(parsed[i], origin);
+            }
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException expected) {}
     }
@@ -562,22 +600,33 @@
      */
     public void testValidatePath() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie2", "name=value;path=/path;version=1");
+        Header header = new BasicHeader("Set-Cookie2", "name=value;path=/path;version=1");
         try {
-            cookieParse(cookiespec, "www.domain.com", 80, "/" /* request path */, false, header);
+            CookieOrigin origin = new CookieOrigin("www.domain.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 expected) {}
 
         // path-matching is case-sensitive
-        header = new Header("Set-Cookie2", "name=value;path=/Path;version=1");
+        header = new BasicHeader("Set-Cookie2", "name=value;path=/Path;version=1");
         try {
-            cookieParse(cookiespec, "www.domain.com", 80, "/path" /* request path */, false, header);
+            CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/path", 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 expected) {}
 
-        header = new Header("Set-Cookie2", "name=value;path=/path;version=1");
-        Cookie[] parsed = cookieParse(cookiespec, "www.domain.com",
-                                      80, "/path/path1" /* request path */, false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/path/path1", false);
+        header = new BasicHeader("Set-Cookie2", "name=value;path=/path;version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
+        for (int i = 0; i < parsed.length; i++) {
+            cookiespec.validate(parsed[i], origin);
+        }
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
         assertEquals("/path", parsed[0].getPath());
@@ -588,26 +637,33 @@
      */
     public void testValidateCookieName() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false);
         // cookie name must not contain blanks
-        Header header = new Header("Set-Cookie2", "invalid name=value; version=1");
+        Header header = new BasicHeader("Set-Cookie2", "invalid name=value; version=1");
         try {
-            cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
+            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 expected) {}
 
         // cookie name must not start with '$'.
-        header = new Header("Set-Cookie2", "$invalid_name=value; version=1");
+        header = new BasicHeader("Set-Cookie2", "$invalid_name=value; version=1");
         try {
-            cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
+            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 expected) {}
 
         // valid name
-        header = new Header("Set-Cookie2", "name=value; version=1");
-        Cookie[] parsed = cookieParse(cookiespec, "www.domain.com", 80, "/", false, header);
+        header = new BasicHeader("Set-Cookie2", "name=value; version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         assertEquals("name", cookie.getName());
         assertEquals("value", cookie.getValue());
     }
@@ -617,18 +673,26 @@
      * port attribute list.
      */
     public void testValidatePort() throws Exception {
-        Header header = new Header("Set-Cookie2", "name=value; Port=\"80,800\"; version=1");
+        Header header = new BasicHeader("Set-Cookie2", "name=value; Port=\"80,800\"; version=1");
         CookieSpec cookiespec = new RFC2965Spec();
         try {
-            cookieParse(cookiespec, "www.domain.com", 8000 /* request port */, "/", false, header);
+            CookieOrigin origin = new CookieOrigin("www.domain.com", 8000, "/", false);
+            Cookie[] parsed = cookiespec.parse(header, origin);
+            for (int i = 0; i < parsed.length; i++) {
+                cookiespec.validate(parsed[i], origin);
+            }
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException e) {}
 
         // valid port list
-        Cookie[] parsed = cookieParse(cookiespec, "www.domain.com", 80 /* request port */, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Cookie[] parsed = cookiespec.parse(header, origin);
+        for (int i = 0; i < parsed.length; i++) {
+            cookiespec.validate(parsed[i], origin);
+        }
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
-        Cookie2 cookie = (Cookie2) parsed[0];
+        ClientCookie cookie = (ClientCookie) parsed[0];
         int[] ports = cookie.getPorts();
         assertNotNull(ports);
         assertEquals(2, ports.length);
@@ -642,9 +706,13 @@
     public void testValidateVersion() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
         // version attribute is REQUIRED
-        Header header = new Header("Set-Cookie2", "name=value");
+        Header header = new BasicHeader("Set-Cookie2", "name=value");
         try {
-            cookieParse(cookiespec, "www.domain.com", 8000, "/", false, header);
+            CookieOrigin origin = new CookieOrigin("www.domain.com", 8000, "/", false);
+            Cookie[] parsed = cookiespec.parse(header, origin);
+            for (int i = 0; i < parsed.length; i++) {
+                cookiespec.validate(parsed[i], origin);
+            }
             fail("MalformedCookieException should have been thrown");
         } catch (MalformedCookieException e) {}
     }
@@ -656,37 +724,50 @@
      * path of the request URI.
      */
     public void testMatchPath() throws Exception {
-        Cookie2 cookie = new Cookie2(".domain.com", "name",
-                                     "value", "/path" /* path */, null, false, new int[] {80});
-        CookieSpec cookiespec = new RFC2965Spec();
-        assertFalse(cookiespec.match("www.domain.com", 80, "/" /* request path */, false, cookie));
-        assertTrue(cookiespec.match("www.domain.com", 80, "/path/path1" /* request path */, false, cookie));
+        BasicClientCookie2 cookie = new BasicClientCookie2("name", "value");
+        cookie.setDomain(".domain.com");
+        cookie.setPath("/path");
+        cookie.setPorts(new int[] {80});
+        CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin1 = new CookieOrigin("www.domain.com", 80, "/", false);
+        assertFalse(cookiespec.match(cookie, origin1));
+        CookieOrigin origin2 = new CookieOrigin("www.domain.com", 80, "/path/path1", false);
+        assertTrue(cookiespec.match(cookie, origin2));
     }
 
     /**
      * test cookie <tt>Domain</tt> matching.
      */
     public void testMatchDomain() throws Exception {
-        Cookie2 cookie = new Cookie2(".domain.com" /* domain */, "name",
-                                     "value", "/", null, false, new int[] {80});
+        BasicClientCookie2 cookie = new BasicClientCookie2("name", "value");
+        cookie.setDomain(".domain.com");
+        cookie.setPath("/");
+        cookie.setPorts(new int[] {80});
         CookieSpec cookiespec = new RFC2965Spec();
         // effective host name minus domain must not contain any dots
-        assertFalse(cookiespec.match("a.b.domain.com" /* request host */, 80, "/", false, cookie));
+        CookieOrigin origin1 = new CookieOrigin("a.b.domain.com" /* request host */, 80, "/", false);
+        assertFalse(cookiespec.match(cookie, origin1));
         // The effective host name MUST domain-match the Domain
         // attribute of the cookie.
-        assertFalse(cookiespec.match("www.domain.org" /* request host */, 80, "/", false, cookie));
-        assertTrue(cookiespec.match("www.domain.com" /* request host */, 80, "/", false, cookie));
+        CookieOrigin origin2 = new CookieOrigin("www.domain.org" /* request host */, 80, "/", false);
+        assertFalse(cookiespec.match(cookie, origin2));
+        CookieOrigin origin3 = new CookieOrigin("www.domain.com" /* request host */, 80, "/", false);
+        assertTrue(cookiespec.match(cookie, origin3));
     }
 
     /**
      * test cookie local <tt>Domain</tt> matching.
      */
     public void testMatchDomainLocal() throws Exception {
-        Cookie2 cookie = new Cookie2(".local" /* domain */, "name",
-                                     "value", "/", null, false, new int[] {80});
-        CookieSpec cookiespec = new RFC2965Spec();
-        assertTrue(cookiespec.match("host" /* request host */, 80, "/", false, cookie));
-        assertFalse(cookiespec.match("host.com" /* request host */, 80, "/", false, cookie));
+        BasicClientCookie2 cookie = new BasicClientCookie2("name", "value");
+        cookie.setDomain(".local");
+        cookie.setPath("/");
+        cookie.setPorts(new int[] {80});
+        CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin1 = new CookieOrigin("host" /* request host */, 80, "/", false);
+        assertTrue(cookiespec.match(cookie, origin1));
+        CookieOrigin origin2 = new CookieOrigin("host.com" /* request host */, 80, "/", false);
+        assertFalse(cookiespec.match(cookie, origin2));
     }
 
     /**
@@ -694,40 +775,57 @@
      */
     public void testMatchPort() throws Exception {
         // cookie can be sent to any port if port attribute not specified
-        Cookie2 cookie = new Cookie2(".domain.com", "name",
-                                     "value", "/", null, false, null /* ports */);
-        CookieSpec cookiespec = new RFC2965Spec();
-        cookie.setPortAttributeSpecified(false);
-        assertTrue(cookiespec.match("www.domain.com", 8080 /* request port */, "/", false, cookie));
-        assertTrue(cookiespec.match("www.domain.com", 323  /* request port */, "/", false, cookie));
+        BasicClientCookie2 cookie = new BasicClientCookie2("name", "value");
+        cookie.setDomain(".domain.com");
+        cookie.setPath("/");
+        cookie.setPorts(null);
+        
+        CookieSpec cookiespec = new RFC2965Spec();
+        CookieOrigin origin1 = new CookieOrigin("www.domain.com", 8080 /* request port */, "/", false);
+        assertTrue(cookiespec.match(cookie, origin1));
+        CookieOrigin origin2 = new CookieOrigin("www.domain.com", 323 /* request port */, "/", false);
+        assertTrue(cookiespec.match(cookie, origin2));
 
         // otherwise, request port must be in cookie's port list
-        cookie = new Cookie2(".domain.com", "name",
-                             "value", "/", null, false, new int[] {80, 8080} /* ports */);
-        cookie.setPortAttributeSpecified(true);
-        assertFalse(cookiespec.match("www.domain.com", 434 /* request port */, "/", false, cookie));
-        assertTrue(cookiespec.match("www.domain.com", 8080 /* request port */, "/", false, cookie));
+        cookie = new BasicClientCookie2("name", "value");
+        cookie.setDomain(".domain.com");
+        cookie.setPath("/");
+        cookie.setPorts(new int[] {80, 8080});
+        cookie.setAttribute(ClientCookie.PORT_ATTR, "80, 8080");
+        CookieOrigin origin3 = new CookieOrigin("www.domain.com", 434 /* request port */, "/", false);
+        assertFalse(cookiespec.match(cookie, origin3));
+        CookieOrigin origin4 = new CookieOrigin("www.domain.com", 8080 /* request port */, "/", false);
+        assertTrue(cookiespec.match(cookie, origin4));
     }
 
     /**
      * test cookie expiration.
      */
     public void testCookieExpiration() throws Exception {
-        Date afterOneHour = new Date(System.currentTimeMillis() + 3600 * 1000L);
-        Cookie2 cookie = new Cookie2(".domain.com", "name",
-                                     "value", "/", afterOneHour /* expiry */, false, null);
-        CookieSpec cookiespec = new RFC2965Spec();
-        assertTrue(cookiespec.match("www.domain.com", 80, "/", false, cookie));
-
-        Date beforeOneHour = new Date(System.currentTimeMillis() - 3600 * 1000L);
-        cookie = new Cookie2(".domain.com", "name",
-                             "value", "/", beforeOneHour /* expiry */, false, null);
-        assertFalse(cookiespec.match("www.domain.com", 80, "/", false, cookie));
+        Date now = new Date();
+
+        Date beforeOneHour = new Date(now.getTime() - 3600 * 1000L);
+        BasicClientCookie2 cookie = new BasicClientCookie2("name", "value");
+        cookie.setDomain(".domain.com");
+        cookie.setPath("/");
+        cookie.setPorts(null);
+        cookie.setExpiryDate(beforeOneHour);
+
+        assertTrue(cookie.isExpired(now));
+
+        Date afterOneHour = new Date(now.getTime() + 3600 * 1000L);
+        cookie = new BasicClientCookie2("name", "value");
+        cookie.setDomain(".domain.com");
+        cookie.setPath("/");
+        cookie.setPorts(null);
+        cookie.setExpiryDate(afterOneHour);
+
+        assertFalse(cookie.isExpired(now));
 
         // discard attributes overrides cookie age, makes it a session cookie.
         cookie.setDiscard(true);
         assertFalse(cookie.isPersistent());
-        assertTrue(cookiespec.match("www.domain.com", 80, "/", false, cookie));
+        assertTrue(cookie.isExpired(now));
     }
 
     /**
@@ -736,87 +834,111 @@
     public void testCookieSecure() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
         // secure cookie can only be sent over a secure connection
-        Cookie2 cookie = new Cookie2(".domain.com", "name",
-                                     "value", "/", null, true /* secure */, null);
-        assertFalse(cookiespec.match("www.domain.com", 80, "/", false /* request secure */, cookie));
-        assertTrue(cookiespec.match("www.domain.com", 80, "/", true /* request secure */, cookie));
+        BasicClientCookie2 cookie = new BasicClientCookie2("name", "value");
+        cookie.setDomain(".domain.com");
+        cookie.setPath("/");
+        cookie.setSecure(true);
+        CookieOrigin origin1 = new CookieOrigin("www.domain.com", 80, "/", false);
+        assertFalse(cookiespec.match(cookie, origin1));
+        CookieOrigin origin2 = new CookieOrigin("www.domain.com", 80, "/", true);
+        assertTrue(cookiespec.match(cookie, origin2));
     }
 
     // ------------------------------------------------------- Test Cookie Formatting
 
-    public void testFormatInvalidCookie() throws Exception {
-        CookieSpec cookiespec = new RFC2965Spec();
-        try {
-            cookiespec.formatCookie(null);
-            fail("IllegalArgumentException nust have been thrown");
-        } catch (IllegalArgumentException expected) {}
-    }
-
     /**
      * Tests RFC 2965 compliant cookie formatting.
      */
     public void testRFC2965CookieFormatting() throws Exception {
-        CookieSpec cookiespec = new RFC2965Spec();
-        Cookie2 cookie1 = new Cookie2(".domain.com", "name1",
-                                     "value", "/", null, false, new int[] {80,8080});
+        CookieSpec cookiespec = new RFC2965Spec(null, true);
+        BasicClientCookie2 cookie1 = new BasicClientCookie2("name1", "value");
+        cookie1.setDomain(".domain.com");
+        cookie1.setPath("/");
+        cookie1.setPorts(new int[] {80,8080});
         cookie1.setVersion(1);
         // domain, path, port specified
-        cookie1.setDomainAttributeSpecified(true);
-        cookie1.setPathAttributeSpecified(true);
-        cookie1.setPortAttributeSpecified(true);
-        assertEquals("$Version=\"1\"; name1=\"value\"; $Domain=\".domain.com\"; $Path=\"/\"; $Port=\"80,8080\"",
-                     cookiespec.formatCookie(cookie1));
-
-        Cookie2 cookie2 = new Cookie2(".domain.com", "name2",
-                "value", "/a/", null, false, new int[] {80,8080});
+        cookie1.setAttribute(ClientCookie.DOMAIN_ATTR, ".domain.com");
+        cookie1.setAttribute(ClientCookie.PATH_ATTR, "/");
+        cookie1.setAttribute(ClientCookie.PORT_ATTR, "80,8080");
+        
+        Header[] headers = cookiespec.formatCookies(new Cookie[] {cookie1});
+        assertNotNull(headers);
+        assertEquals(1, headers.length);
+        assertEquals("$Version=1; name1=\"value\"; $Path=\"/\"; $Domain=\".domain.com\"; $Port=\"80,8080\"",
+                headers[0].getValue());
+
+        
+        BasicClientCookie2 cookie2 = new BasicClientCookie2("name2", "value");
+        cookie2.setDomain(".domain.com");
+        cookie2.setPath("/a/");
+        cookie2.setPorts(new int[] {80,8080});
         cookie2.setVersion(2);
         // domain, path specified  but port unspecified
-        cookie2.setDomainAttributeSpecified(true);
-        cookie2.setPathAttributeSpecified(true);
-        cookie2.setPortAttributeSpecified(false);
-        assertEquals("$Version=\"2\"; name2=\"value\"; $Domain=\".domain.com\"; $Path=\"/a/\"",
-                     cookiespec.formatCookie(cookie2));
-
-        Cookie2 cookie3 = new Cookie2(".domain.com", "name3",
-                "value", "/a/b/", null, false, new int[] {80,8080});
+        cookie2.setAttribute(ClientCookie.DOMAIN_ATTR, ".domain.com");
+        cookie2.setAttribute(ClientCookie.PATH_ATTR, "/a/");
+        
+        headers = cookiespec.formatCookies(new Cookie[] {cookie2});
+        assertNotNull(headers);
+        assertEquals(1, headers.length);
+        assertEquals("$Version=2; name2=\"value\"; $Path=\"/a/\"; $Domain=\".domain.com\"",
+                headers[0].getValue());
+
+        BasicClientCookie2 cookie3 = new BasicClientCookie2("name3", "value");
+        cookie3.setDomain(".domain.com");
+        cookie3.setPath("/a/b/");
+        cookie3.setPorts(new int[] {80,8080});
         cookie3.setVersion(1);
         // path specified, port specified but blank, domain unspecified
-        cookie3.setDomainAttributeSpecified(false);
-        cookie3.setPathAttributeSpecified(true);
-        cookie3.setPortAttributeSpecified(true);
-        cookie3.setPortAttributeBlank(true);
-        assertEquals("$Version=\"1\"; name3=\"value\"; $Path=\"/a/b/\"; $Port=\"\"",
-                     cookiespec.formatCookie(cookie3));
+        cookie3.setAttribute(ClientCookie.PATH_ATTR, "/a/b/");
+        cookie3.setAttribute(ClientCookie.PORT_ATTR, "  ");
 
-        assertEquals("$Version=\"2\"; " +
+        headers = cookiespec.formatCookies(new Cookie[] {cookie3});
+        assertNotNull(headers);
+        assertEquals(1, headers.length);
+        assertEquals("$Version=1; name3=\"value\"; $Path=\"/a/b/\"; $Port=\"\"",
+                headers[0].getValue());
+
+        headers = cookiespec.formatCookies(new Cookie[] {cookie3, cookie2, cookie1});
+        assertNotNull(headers);
+        assertEquals(1, headers.length);
+        assertEquals("$Version=1; " +
                 "name3=\"value\"; $Path=\"/a/b/\"; $Port=\"\"; " +
-                "name2=\"value\"; $Domain=\".domain.com\"; $Path=\"/a/\"; " +
-                "name1=\"value\"; $Domain=\".domain.com\"; $Path=\"/\"; $Port=\"80,8080\"",
-                cookiespec.formatCookies(new Cookie[] {cookie3, cookie2, cookie1}));
+                "name2=\"value\"; $Path=\"/a/\"; $Domain=\".domain.com\"; " +
+                "name1=\"value\"; $Path=\"/\"; $Domain=\".domain.com\"; $Port=\"80,8080\"",
+                headers[0].getValue());
     }
 
     /**
      * Tests RFC 2965 compliant cookies formatting.
      */
     public void testRFC2965CookiesFormatting() throws Exception {
-        CookieSpec cookiespec = new RFC2965Spec();
-        Cookie2 cookie1 = new Cookie2(".domain.com", "name1",
-                                      "value1", "/", null, false, new int[] {80,8080});
+        CookieSpec cookiespec = new RFC2965Spec(null, true);
+        BasicClientCookie2 cookie1 = new BasicClientCookie2("name1", "value1");
+        cookie1.setDomain(".domain.com");
+        cookie1.setPath("/");
+        cookie1.setPorts(new int[] {80,8080});
         cookie1.setVersion(1);
         // domain, path, port specified
-        cookie1.setDomainAttributeSpecified(true);
-        cookie1.setPathAttributeSpecified(true);
-        cookie1.setPortAttributeSpecified(true);
-        Cookie2 cookie2 = new Cookie2(".domain.com", "name2",
-                                      null, "/", null, false, null);
+        cookie1.setAttribute(ClientCookie.DOMAIN_ATTR, ".domain.com");
+        cookie1.setAttribute(ClientCookie.PATH_ATTR, "/");
+        cookie1.setAttribute(ClientCookie.PORT_ATTR, "80,8080");
+        
+        BasicClientCookie2 cookie2 = new BasicClientCookie2("name2", "");
+        cookie2.setDomain(".domain.com");
+        cookie2.setPath("/");
+        cookie2.setPorts(new int[] {80,8080});
         cookie2.setVersion(1);
-        // value null, domain, path, port specified
-        cookie2.setDomainAttributeSpecified(true);
-        cookie2.setPathAttributeSpecified(true);
-        cookie2.setPortAttributeSpecified(false);
-        Cookie[] cookies = new Cookie[] {cookie1, cookie2};
-        assertEquals("$Version=\"1\"; name1=\"value1\"; $Domain=\".domain.com\"; $Path=\"/\"; $Port=\"80,8080\"; " +
-            "name2=\"\"; $Domain=\".domain.com\"; $Path=\"/\"", cookiespec.formatCookies(cookies));
+        // value null, domain, path specified
+        cookie2.setAttribute(ClientCookie.DOMAIN_ATTR, ".domain.com");
+        cookie2.setAttribute(ClientCookie.PATH_ATTR, "/");
+        
+        Header[] headers = cookiespec.formatCookies(new Cookie[] {cookie1, cookie2});
+        assertNotNull(headers);
+        assertEquals(1, headers.length);
+        
+        assertEquals("$Version=1; name1=\"value1\"; $Path=\"/\"; $Domain=\".domain.com\"; $Port=\"80,8080\"; " +
+            "name2=\"\"; $Path=\"/\"; $Domain=\".domain.com\"", 
+            headers[0].getValue());
     }
 
     // ------------------------------------------------------- Backward compatibility tests
@@ -826,8 +948,9 @@
      */
     public void testCompatibilityWithSetCookie() throws Exception {
         CookieSpec cookiespec = new RFC2965Spec();
-        Header header = new Header("Set-Cookie", "name=value; domain=.domain.com; version=1");
-        Cookie[] parsed = cookieParse(cookiespec, "www.domain.com", 80, "/", false, header);
+        CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false);
+        Header header = new BasicHeader("Set-Cookie", "name=value; domain=.domain.com; version=1");
+        Cookie[] parsed = cookiespec.parse(header, origin);
         assertNotNull(parsed);
         assertEquals(1, parsed.length);
         assertEquals("name", parsed[0].getName());



Mime
View raw message