hc-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject svn commit: r417820 - in /jakarta/httpcomponents/httpclient/trunk/src: java/org/apache/http/cookie/impl/ test/org/apache/http/cookie/impl/
Date Wed, 28 Jun 2006 17:19:32 GMT
Author: olegk
Date: Wed Jun 28 10:19:31 2006
New Revision: 417820

URL: http://svn.apache.org/viewvc?rev=417820&view=rev
Log:
* Made the browser compatibility and the RFC2109 cookie specs configurable. Configuration
options can be passed as constructor parameters. 
* More test coverage 

Modified:
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BrowserCompatSpec.java
    jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/RFC2109Spec.java
    jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBrowserCompatSpec.java
    jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestCookieRFC2109Spec.java

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java?rev=417820&r1=417819&r2=417820&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicExpiresHandler.java
Wed Jun 28 10:19:31 2006
@@ -43,7 +43,7 @@
         if (datepatterns == null) {
             throw new IllegalArgumentException("Array of date patterns may not be null");
         }
-        this.datepatterns = (String[]) datepatterns.clone();
+        this.datepatterns = datepatterns;
     }
 
     public void parse(final Cookie cookie, final String value) 

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BrowserCompatSpec.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BrowserCompatSpec.java?rev=417820&r1=417819&r2=417820&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BrowserCompatSpec.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BrowserCompatSpec.java
Wed Jun 28 10:19:31 2006
@@ -49,8 +49,8 @@
  */
 public class BrowserCompatSpec extends CookieSpecBase {
     
-    /** Valid date patterns */
-    private String[] datepatterns = new String[] {
+    /** Valid date patterns used per default */
+    private static final String[] DATE_PATTERNS = new String[] {
             DateUtils.PATTERN_RFC1123,
             DateUtils.PATTERN_RFC1036,
             DateUtils.PATTERN_ASCTIME,
@@ -67,9 +67,16 @@
             "EEE, dd-MM-yyyy HH:mm:ss z",                
         };
 
+    private final String[] datepatterns; 
+    
     /** Default constructor */
-    public BrowserCompatSpec() {
+    public BrowserCompatSpec(final String[] datepatterns) {
         super();
+        if (datepatterns != null) {
+            this.datepatterns = (String [])datepatterns.clone();
+        } else {
+            this.datepatterns = DATE_PATTERNS;
+        }
         registerAttribHandler("path", new BasicPathHandler());
         registerAttribHandler("domain", new BasicDomainHandler());
         registerAttribHandler("max-age", new BasicMaxAgeHandler());
@@ -78,6 +85,11 @@
         registerAttribHandler("expires", new BasicExpiresHandler(this.datepatterns));
     }
 
+    /** Default constructor */
+    public BrowserCompatSpec() {
+        this(null);
+    }
+    
     public Cookie[] parse(final Header header, final CookieOrigin origin) 
             throws MalformedCookieException {
         if (header == null) {

Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/RFC2109Spec.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/RFC2109Spec.java?rev=417820&r1=417819&r2=417820&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/RFC2109Spec.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/RFC2109Spec.java
Wed Jun 28 10:19:31 2006
@@ -61,28 +61,36 @@
 
     private final static CookiePathComparator PATH_COMPARATOR = new CookiePathComparator();

     
-    private boolean oneHeader = false;
+    private final static String[] DATE_PATTERNS = {
+        DateUtils.PATTERN_RFC1123,
+        DateUtils.PATTERN_RFC1036,
+        DateUtils.PATTERN_ASCTIME 
+    }; 
+    
+    private final String[] datepatterns; 
+    private final boolean oneHeader;
     
     /** Default constructor */
-    public RFC2109Spec(boolean oneHeader) {
+    public RFC2109Spec(final String[] datepatterns, boolean oneHeader) {
         super();
+        if (datepatterns != null) {
+            this.datepatterns = (String [])datepatterns.clone();
+        } else {
+            this.datepatterns = DATE_PATTERNS;
+        }
+        this.oneHeader = oneHeader;
         registerAttribHandler("version", new RFC2109VersionHandler());
         registerAttribHandler("path", new BasicPathHandler());
         registerAttribHandler("domain", new RFC2109DomainHandler());
         registerAttribHandler("max-age", new BasicMaxAgeHandler());
         registerAttribHandler("secure", new BasicSecureHandler());
         registerAttribHandler("comment", new BasicCommentHandler());
-        registerAttribHandler("expires", new BasicExpiresHandler(
-                new String[] {
-                        DateUtils.PATTERN_RFC1123,
-                        DateUtils.PATTERN_RFC1036,
-                        DateUtils.PATTERN_ASCTIME}));
-        this.oneHeader = oneHeader;
+        registerAttribHandler("expires", new BasicExpiresHandler(this.datepatterns));
     }
 
     /** Default constructor */
     public RFC2109Spec() {
-        this(false);
+        this(null, false);
     }
     
     public Cookie[] parse(final Header header, final CookieOrigin origin) 

Modified: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBrowserCompatSpec.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBrowserCompatSpec.java?rev=417820&r1=417819&r2=417820&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBrowserCompatSpec.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBrowserCompatSpec.java
Wed Jun 28 10:19:31 2006
@@ -35,6 +35,7 @@
 import org.apache.http.cookie.CookieOrigin;
 import org.apache.http.cookie.CookieSpec;
 import org.apache.http.cookie.MalformedCookieException;
+import org.apache.http.util.DateUtils;
 
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -65,6 +66,12 @@
         return new TestSuite(TestBrowserCompatSpec.class);
     }
 
+    public void testConstructor() throws Exception {
+        new BrowserCompatSpec();
+        new BrowserCompatSpec(null);
+        new BrowserCompatSpec(new String[] { DateUtils.PATTERN_RFC1036 });
+    }
+    
     /**
      * Tests whether domain attribute check is case-insensitive.
      */

Modified: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestCookieRFC2109Spec.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestCookieRFC2109Spec.java?rev=417820&r1=417819&r2=417820&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestCookieRFC2109Spec.java
(original)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestCookieRFC2109Spec.java
Wed Jun 28 10:19:31 2006
@@ -33,6 +33,7 @@
 import org.apache.http.cookie.CookieOrigin;
 import org.apache.http.cookie.CookieSpec;
 import org.apache.http.cookie.MalformedCookieException;
+import org.apache.http.util.DateUtils;
 
 import junit.framework.Test;
 import junit.framework.TestCase;
@@ -60,6 +61,12 @@
         return new TestSuite(TestCookieRFC2109Spec.class);
     }
 
+    public void testConstructor() throws Exception {
+        new RFC2109Spec();
+        new RFC2109Spec(null, false);
+        new RFC2109Spec(new String[] { DateUtils.PATTERN_RFC1036 }, false);
+    }
+    
     public void testParseVersion() throws Exception {
         Header header = new Header("Set-Cookie","cookie-name=cookie-value; version=1");
 
@@ -288,7 +295,7 @@
      * Tests RFC 2109 compiant cookie formatting.
      */
     public void testRFC2109CookieFormatting() throws Exception {
-        CookieSpec cookiespec = new RFC2109Spec(false);
+        CookieSpec cookiespec = new RFC2109Spec(null, false);
         Header header = new Header("Set-Cookie", 
             "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
         CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
@@ -312,7 +319,7 @@
     }
 
     public void testRFC2109CookiesFormatting() throws Exception {
-        CookieSpec cookiespec = new RFC2109Spec(true);
+        CookieSpec cookiespec = new RFC2109Spec(null, true);
         Header header = new Header("Set-Cookie", 
             "name1=value1; path=/; domain=.mydomain.com, " + 
             "name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
@@ -390,6 +397,30 @@
         assertNotNull(headers);
         assertEquals(1, headers.length);
         assertEquals("$Version=0; name=", headers[0].getValue());
+    }
+
+    public void testCookieOrderingByPath() {
+        Cookie c1 = new Cookie("name1", "value1");
+        c1.setPath("/a/b/c");
+        c1.setPathAttributeSpecified(true);
+        Cookie c2 = new Cookie("name2", "value2");
+        c2.setPath("/a/b");
+        c2.setPathAttributeSpecified(true);
+        Cookie c3 = new Cookie("name3", "value3");
+        c3.setPath("/a");
+        c3.setPathAttributeSpecified(true);
+        Cookie c4 = new Cookie("name4", "value4");
+        c4.setPath("/");
+        c4.setPathAttributeSpecified(true);
+
+        CookieSpec cookiespec = new RFC2109Spec(null, true);
+        Header[] headers = cookiespec.formatCookies(new Cookie[] { c2, c4, c1, c3 });
+        assertNotNull(headers);
+        assertEquals(1, headers.length);
+        assertEquals("$Version=0; name1=value1; $Path=/a/b/c; " +
+                "name2=value2; $Path=/a/b; " +
+                "name3=value3; $Path=/a; " +
+                "name4=value4; $Path=/", headers[0].getValue());
     }
 
     public void testInvalidInput() throws Exception {



Mime
View raw message