jsdever     2002/12/15 17:40:52

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        Cookie.java HttpMethodBase.java
               httpclient/src/java/org/apache/commons/httpclient/cookie
                        CookieSpec.java CookieSpecBase.java
                        NetscapeDraftSpec.java RFC2109Spec.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestCookie.java
  Log:
  Cookie specs refinement has been prompted by the problem reported by Christopher Lenz
  
  Changes:
  
  - Default cookie version is back to 0. My idea to assume that cookies by
  default must be treated as fully RFC 2109 compliant was ill-conceived.
  
  - RFC 2109 CookieSpec class is not better compliant with the
  requirements of backward compatibility stated in the section 10
  ("Historical") of the RFC 2109. The code got uglier though :-(
  
  - Better Netscape draft compliance (for history inclined)
  
  - Minor API refinements
  
  - More test cases
  
  Contributed by: Oleg Kalnichevski
  
  Revision  Changes    Path
  1.30      +5 -5      
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Cookie.java
  
  Index: Cookie.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/Cookie.java,v
  retrieving revision 1.29
  retrieving revision 1.30
  diff -u -r1.29 -r1.30
  --- Cookie.java       8 Dec 2002 06:09:46 -0000       1.29
  +++ Cookie.java       16 Dec 2002 01:40:51 -0000      1.30
  @@ -735,7 +735,7 @@
      private boolean _hasDomainAttribute = false;
   
      /** The version of the cookie specification I was created from. */
  -   private int     _version = 1;
  +   private int     _version = 0;
   
      // -------------------------------------------------------------- Constants
   
  
  
  
  1.88      +6 -7      
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java
  
  Index: HttpMethodBase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpMethodBase.java,v
  retrieving revision 1.87
  retrieving revision 1.88
  diff -u -r1.87 -r1.88
  --- HttpMethodBase.java       11 Dec 2002 13:21:43 -0000      1.87
  +++ HttpMethodBase.java       16 Dec 2002 01:40:51 -0000      1.88
  @@ -1512,15 +1512,14 @@
               for (int i = 0; i < cookies.length; i++)
               {
                   Cookie cookie = cookies[i];
  -                CookieSpec validator = 
CookiePolicy.getSpecByVersion(cookie.getVersion());
  -                validator.validate(
  +                parser.validate(
                     conn.getHost(), 
                     conn.getPort(),
                     getPath(), 
                     conn.isSecure(),
                     cookie);
                   if (log.isDebugEnabled()) {
  -                    log.debug("Cookie accepted: \"" + 
validator.formatCookie(cookie) + "\"");
  +                    log.debug("Cookie accepted: \"" + parser.formatCookie(cookie) + 
"\"");
                   }
                   state.addCookie(cookie);
               }
  
  
  
  1.3       +13 -0     
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookieSpec.java
  
  Index: CookieSpec.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookieSpec.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- CookieSpec.java   9 Dec 2002 12:48:40 -0000       1.2
  +++ CookieSpec.java   16 Dec 2002 01:40:52 -0000      1.3
  @@ -58,6 +58,7 @@
   package org.apache.commons.httpclient.cookie;
   
   import org.apache.commons.httpclient.Header;
  +import org.apache.commons.httpclient.NameValuePair;
   import org.apache.commons.httpclient.Cookie;
   
   /**
  @@ -111,6 +112,18 @@
         */
   
       public Cookie[] parse(String host, int port, String path, boolean secure, final 
Header header)
  +      throws MalformedCookieException;
  +
  +    /**
  +      * Parse the cookie attribute and update the corresponsing {@link Cookie} 
properties.
  +      *
  +      * @param attribute {@link NameValuePair} cookie attribute from the 
<tt>Set-Cookie</tt>
  +      * @param cookie {@link Cookie} to be updated
  +      * @throws MalformedCookieException if an exception occurs during parsing
  +      * @throws java.lang.IllegalArgumentException if an input parameter is illegal
  +      */
  +
  +    public void parseAttribute(final NameValuePair attribute, final Cookie cookie)
         throws MalformedCookieException;
   
       /**
  
  
  
  1.4       +115 -96   
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookieSpecBase.java
  
  Index: CookieSpecBase.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/CookieSpecBase.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- CookieSpecBase.java       11 Dec 2002 13:16:09 -0000      1.3
  +++ CookieSpecBase.java       16 Dec 2002 01:40:52 -0000      1.4
  @@ -214,101 +214,9 @@
               // could be null. In case only a header element and no parameters.
               if (parameters != null) {
   
  -                for (int j = 0; j < parameters.length; j++) {
  -
  -                    String param_name = parameters[j].getName().toLowerCase();
  -                    String param_value = parameters[j].getValue();
  -
  -                    if (param_name.equals("version")) {
  -
  -                        if (param_value == null) {
  -                            throw new MalformedCookieException("Missing value for 
version attribute");
  -                        }
  -                        try {
  -                           cookie.setVersion(Integer.parseInt(param_value));
  -                        } catch (NumberFormatException e) {
  -                            throw new MalformedCookieException( "Invalid version 
attribute: " + e.getMessage());
  -                        }
  -
  -                    } 
  -                    else if (param_name.equals("path")) {
  -
  -                        if (param_value == null) {
  -                            throw new MalformedCookieException("Missing value for 
path attribute");
  -                        }
  -                        if (param_value.trim().equals("")) {
  -                            throw new MalformedCookieException("Blank value for 
path attribute");
  -                        }
  -                        cookie.setPath(param_value);
  -                        cookie.setPathAttributeSpecified(true);
  -
  -                    } 
  -                    else if (param_name.equals("domain")) {
  -
  -                        if (param_value == null) {
  -                            throw new MalformedCookieException("Missing value for 
domain attribute");
  -                        }
  -                        if (param_value.trim().equals("")) {
  -                            throw new MalformedCookieException("Blank value for 
domain attribute");
  -                        }
  -                        cookie.setDomain(param_value);
  -                        cookie.setDomainAttributeSpecified(true);
  -
  -                    } 
  -                    else if (param_name.equals("max-age")) {
  -
  -                        if (param_value == null) {
  -                            throw new MalformedCookieException("Missing value for 
max-age attribute");
  -                        }
  -                        int age;
  -                        try {
  -                            age = Integer.parseInt(param_value);
  -                        } catch (NumberFormatException e) {
  -                            throw new MalformedCookieException( "Invalid max-age 
attribute: " + e.getMessage());
  -                        }
  -                        cookie.setExpiryDate(new Date(System.currentTimeMillis() +
  -                                age * 1000L));
  -
  -                    } 
  -                    else if (param_name.equals("secure")) {
  -
  -                        cookie.setSecure(true);
  -
  -                    } 
  -                    else if (param_name.equals("comment")) {
  -
  -                        cookie.setComment(param_value);
  -
  -                    } 
  -                    else if (param_name.equals("expires")) {
  -
  -                        if (param_value == null) {
  -                            throw new MalformedCookieException("Missing value for 
expires attribute");
  -                        }
  -                        boolean set = false;
  -                        // trim single quotes around expiry if present
  -                        // see 
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5279
  -                        if(param_value.length() > 1 &&
  -                                param_value.startsWith("'") &&
  -                                param_value.endsWith("'")) {
  -                            param_value = 
param_value.substring(1,param_value.length()-1);
  -                        }
  -
  -                        for(int k=0;k<expiryFormats.length;k++) {
  -
  -                            try {
  -                                Date date = expiryFormats[k].parse(param_value);
  -                                cookie.setExpiryDate(date);
  -                                set = true;
  -                                break;
  -                            } catch (ParseException e) {
  -                                //Ignore and move on
  -                            }
  -                        }
  -                        if(!set) {
  -                            throw new MalformedCookieException("Unable to parse 
expiration date parameter: " + param_value);
  -                        }
  -                    }
  +                for (int j = 0; j < parameters.length; j++)
  +                {
  +                    parseAttribute(parameters[j], cookie);
                   }
               }
               cookies[i] = cookie;
  @@ -349,6 +257,7 @@
       public Cookie[] parse(String host, int port, String path, boolean secure, final 
Header header)
         throws MalformedCookieException
       {
  +        log.trace("enter CookieSpecBase.parse(String, port, path, boolean, 
String)");
           if(header == null)
           {
               throw new IllegalArgumentException("Header may not be null.");
  @@ -356,6 +265,116 @@
           return parse(host, port, path, secure, header.getValue());
       }
   
  +
  +    /**
  +      * Parse the cookie attribute and update the corresponsing {@link Cookie} 
properties.
  +      *
  +      * @param attribute {@link HeaderElement} cookie attribute from the 
<tt>Set-Cookie</tt>
  +      * @param cookie {@link Cookie} to be updated
  +      * @throws MalformedCookieException if an exception occurs during parsing
  +      * @throws java.lang.IllegalArgumentException if an input parameter is illegal
  +      */
  +
  +    public void parseAttribute(final NameValuePair attribute, final Cookie cookie)
  +      throws MalformedCookieException
  +    {
  +        if(attribute == null)
  +        {
  +            throw new IllegalArgumentException("Attribute may not be null.");
  +        }
  +        if(cookie == null)
  +        {
  +            throw new IllegalArgumentException("Cookie may not be null.");
  +        }
  +        String param_name = attribute.getName().toLowerCase();
  +        String param_value = attribute.getValue();
  +
  +        if (param_name.equals("path")) {
  +
  +            if (param_value == null) {
  +                throw new MalformedCookieException("Missing value for path 
attribute");
  +            }
  +            if (param_value.trim().equals("")) {
  +                throw new MalformedCookieException("Blank value for path 
attribute");
  +            }
  +            cookie.setPath(param_value);
  +            cookie.setPathAttributeSpecified(true);
  +
  +        } 
  +        else if (param_name.equals("domain")) {
  +
  +            if (param_value == null) {
  +                throw new MalformedCookieException("Missing value for domain 
attribute");
  +            }
  +            if (param_value.trim().equals("")) {
  +                throw new MalformedCookieException("Blank value for domain 
attribute");
  +            }
  +            cookie.setDomain(param_value);
  +            cookie.setDomainAttributeSpecified(true);
  +
  +        } 
  +        else if (param_name.equals("max-age")) {
  +
  +            if (param_value == null) {
  +                throw new MalformedCookieException("Missing value for max-age 
attribute");
  +            }
  +            int age;
  +            try {
  +                age = Integer.parseInt(param_value);
  +            } catch (NumberFormatException e) {
  +                throw new MalformedCookieException( "Invalid max-age attribute: " + 
e.getMessage());
  +            }
  +            cookie.setExpiryDate(new Date(System.currentTimeMillis() +
  +                    age * 1000L));
  +
  +        } 
  +        else if (param_name.equals("secure")) {
  +
  +            cookie.setSecure(true);
  +
  +        } 
  +        else if (param_name.equals("comment")) {
  +
  +            cookie.setComment(param_value);
  +
  +        } 
  +        else if (param_name.equals("expires")) {
  +
  +            if (param_value == null) {
  +                throw new MalformedCookieException("Missing value for expires 
attribute");
  +            }
  +            boolean set = false;
  +            // trim single quotes around expiry if present
  +            // see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5279
  +            if(param_value.length() > 1 &&
  +                    param_value.startsWith("'") &&
  +                    param_value.endsWith("'")) {
  +                param_value = param_value.substring(1,param_value.length()-1);
  +            }
  +
  +            for(int k=0;k<expiryFormats.length;k++) {
  +
  +                try {
  +                    Date date = expiryFormats[k].parse(param_value);
  +                    cookie.setExpiryDate(date);
  +                    set = true;
  +                    break;
  +                } catch (ParseException e) {
  +                    //Ignore and move on
  +                }
  +            }
  +            if(!set) {
  +                throw new MalformedCookieException("Unable to parse expiration date 
parameter: " + param_value);
  +            }
  +        }
  +        else {
  +            if (log.isWarnEnabled())
  +            {
  +                log.warn("Unrecognized cookie attribute: " + attribute.toString());
  +            }
  +        }
  +    }
  +
       
       /**
         * Performs most common {@link Cookie} validation
  @@ -599,7 +618,7 @@
   
       public String formatCookie(Cookie cookie)
       {
  -        log.trace("enter CookieSpecBase.toExternalForm(Cookie)");
  +        log.trace("enter CookieSpecBase.formatCookie(Cookie)");
           if(cookie == null)
           {
               throw new IllegalArgumentException("Cookie may not be null");
  
  
  
  1.3       +50 -0     
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java
  
  Index: NetscapeDraftSpec.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/NetscapeDraftSpec.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- NetscapeDraftSpec.java    9 Dec 2002 12:48:40 -0000       1.2
  +++ NetscapeDraftSpec.java    16 Dec 2002 01:40:52 -0000      1.3
  @@ -58,6 +58,12 @@
   package org.apache.commons.httpclient.cookie;
   
   import java.util.StringTokenizer;
  +import java.util.Date;
  +import java.util.Locale;   
  +import java.text.DateFormat; 
  +import java.text.SimpleDateFormat;  
  +import java.text.ParseException; 
  +import org.apache.commons.httpclient.NameValuePair;
   import org.apache.commons.httpclient.Cookie;
   
   /**
  @@ -86,6 +92,50 @@
           super();
       }
   
  +
  +    /**
  +      * Parse the cookie attribute and update the corresponsing {@link Cookie} 
properties
  +      * as defined by the Netscape draft specification
  +      *
  +      * @param attribute {@link HeaderElement} cookie attribute from the 
<tt>Set-Cookie</tt>
  +      * @param cookie {@link Cookie} to be updated
  +      * @throws MalformedCookieException if an exception occurs during parsing
  +      * @throws java.lang.IllegalArgumentException if an input parameter is illegal
  +      */
  +
  +    public void parseAttribute(final NameValuePair attribute, final Cookie cookie)
  +      throws MalformedCookieException
  +    {
  +        if(attribute == null)
  +        {
  +            throw new IllegalArgumentException("Attribute may not be null.");
  +        }
  +        if(cookie == null)
  +        {
  +            throw new IllegalArgumentException("Cookie may not be null.");
  +        }
  +        String param_name = attribute.getName().toLowerCase();
  +        String param_value = attribute.getValue();
  +
  +        if (param_name.equals("expires")) {
  +
  +            if (param_value == null) {
  +                throw new MalformedCookieException("Missing value for expires 
attribute");
  +            }
  +            try
  +            {
  +                DateFormat expiryFormat = new SimpleDateFormat("EEE, dd-MMM-yyyy 
HH:mm:ss z", Locale.US);
  +                Date date = expiryFormat.parse(param_value);
  +                cookie.setExpiryDate(date);
  +            } 
  +            catch (ParseException e) {
  +                throw new MalformedCookieException("Invalid expires attribute: " + 
e.getMessage());
  +            }
  +        }
  +        else {
  +            super.parseAttribute(attribute, cookie);
  +        }
  +    }
   
       /**
         * Performs Netscape draft compliant {@link Cookie} validation
  
  
  
  1.3       +99 -23    
jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/RFC2109Spec.java
  
  Index: RFC2109Spec.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/cookie/RFC2109Spec.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- RFC2109Spec.java  9 Dec 2002 12:48:40 -0000       1.2
  +++ RFC2109Spec.java  16 Dec 2002 01:40:52 -0000      1.3
  @@ -58,6 +58,7 @@
   package org.apache.commons.httpclient.cookie;
   
   import org.apache.commons.httpclient.Header;
  +import org.apache.commons.httpclient.NameValuePair;
   import org.apache.commons.httpclient.Cookie;
   
   /**
  @@ -86,6 +87,48 @@
           super();
       }
   
  +
  +    /**
  +      * Parse RFC 2109 specific cookie attribute and update the corresponsing 
{@link Cookie}
  +      * properties.
  +      *
  +      * @param attribute {@link HeaderElement} cookie attribute from the 
<tt>Set-Cookie</tt>
  +      * @param cookie {@link Cookie} to be updated
  +      * @throws MalformedCookieException if an exception occurs during parsing
  +      * @throws java.lang.IllegalArgumentException if an input parameter is illegal
  +      */
  +
  +    public void parseAttribute(final NameValuePair attribute, final Cookie cookie)
  +      throws MalformedCookieException
  +    {
  +        if(attribute == null)
  +        {
  +            throw new IllegalArgumentException("Attribute may not be null.");
  +        }
  +        if(cookie == null)
  +        {
  +            throw new IllegalArgumentException("Cookie may not be null.");
  +        }
  +        String param_name = attribute.getName().toLowerCase();
  +        String param_value = attribute.getValue();
  +
  +        if (param_name.equals("version")) {
  +
  +            if (param_value == null) {
  +                throw new MalformedCookieException("Missing value for version 
attribute");
  +            }
  +            try {
  +               cookie.setVersion(Integer.parseInt(param_value));
  +            } catch (NumberFormatException e) {
  +                throw new MalformedCookieException( "Invalid version attribute: " + 
e.getMessage());
  +            }
  +
  +        } 
  +        else {
  +            super.parseAttribute(attribute, cookie);
  +        }
  +    }
  +
       /**
         * Performs RFC 2109 compliant {@link Cookie} validation
         *
  @@ -128,38 +171,72 @@
   
   
       /**
  -     * Return a string suitable for sending in a <tt>"Cookie"</tt> header as 
defined in RFC 2109
  +     * Return a name/value string suitable for sending in a <tt>"Cookie"</tt> 
header 
  +     * as defined in RFC 2109 for backward compatibility with cookie version 0
        * @param a {@link Cookie} to be formatted as string
        * @return a string suitable for sending in a <tt>"Cookie"</tt> header.
        */
   
  -    public String formatCookie(Cookie cookie)
  +    private String formatNameValuePair(final String name, final String value, int 
version)
       {
  -        log.trace("enter RFC2109Spec.formatCookie(Cookie)");
  +        StringBuffer buffer = new StringBuffer();
  +        if (version < 1)
  +        {
  +            buffer.append(name).append("=").append(value);   
  +        }
  +        else
  +        {
  +            buffer.append(name).append("=\"").append(value).append("\"");
  +        }
  +        return buffer.toString(); 
  +        
  +    }
  +
  +    /**
  +     * Return a string suitable for sending in a <tt>"Cookie"</tt> header 
  +     * as defined in RFC 2109 for backward compatibility with cookie version 0
  +     * @param a {@link Cookie} to be formatted as string
  +     * @return a string suitable for sending in a <tt>"Cookie"</tt> header.
  +     */
  +
  +    private String formatCookieAsVer(Cookie cookie, int version)
  +    {
  +        log.trace("enter RFC2109Spec.formatCookieAsVer(Cookie)");
           if(cookie == null)
           {
               throw new IllegalArgumentException("Cookie may not be null");
           }
           StringBuffer buf = new StringBuffer();
  -        buf.append(cookie.getName());
  -        buf.append("=\"");
  -        buf.append(cookie.getValue());
  -        buf.append("\"");
  +        buf.append(formatNameValuePair(cookie.getName(), cookie.getValue(), 
version));
           if (cookie.getDomain() != null && cookie.isDomainAttributeSpecified()) {
  -            buf.append("; $Domain=\"");
  -            buf.append(cookie.getDomain());
  -            buf.append("\"");
  +            buf.append("; ");
  +            buf.append(formatNameValuePair("$Domain", cookie.getDomain(), version));
           }
           if (cookie.getPath() != null && cookie.isPathAttributeSpecified()) {
  -            buf.append("; $Path=\"");
  -            buf.append(cookie.getPath());
  -            buf.append("\"");
  +            buf.append("; ");
  +            buf.append(formatNameValuePair("$Path", cookie.getPath(), version));
           }
           return buf.toString();
       }
   
   
       /**
  +     * Return a string suitable for sending in a <tt>"Cookie"</tt> header as 
defined in RFC 2109
  +     * @param a {@link Cookie} to be formatted as string
  +     * @return a string suitable for sending in a <tt>"Cookie"</tt> header.
  +     */
  +
  +    public String formatCookie(Cookie cookie)
  +    {
  +        log.trace("enter RFC2109Spec.formatCookie(Cookie)");
  +        if(cookie == null)
  +        {
  +            throw new IllegalArgumentException("Cookie may not be null");
  +        }
  +        return formatCookieAsVer(cookie, cookie.getVersion());
  +    }
  +
  +    /**
        * Create a RFC 2109 compliant <tt>"Cookie"</tt> header value containing all 
        * {@link Cookie}s in <i>cookies</i> suitable for sending in a 
<tt>"Cookie"</tt> header
        * @param an array of {@link Cookie}s to be formatted
  @@ -170,7 +247,6 @@
       public String formatCookies(Cookie[] cookies)
       {
           log.trace("enter RFC2109Spec.formatCookieHeader(Cookie[])");
  -        String value = super.formatCookies(cookies);
           int version = Integer.MAX_VALUE;
           // Pick the lowerest common denominator
           for (int i = 0; i < cookies.length; i++)
  @@ -182,10 +258,12 @@
               }
           }
           StringBuffer buffer = new StringBuffer();
  -        buffer.append("$Version=\"");
  -        buffer.append(version);
  -        buffer.append("\"; ");
  -        buffer.append(value);
  +        buffer.append(formatNameValuePair("$Version", Integer.toString(version), 
version));
  +        for (int i = 0; i < cookies.length; i++)
  +        {
  +            buffer.append("; ");
  +            buffer.append(formatCookieAsVer(cookies[i], version));
  +        }
           return buffer.toString();
       }
       
  @@ -200,12 +278,10 @@
       public Header formatCookieHeader(Cookie cookie)
       {
           log.trace("enter RFC2109Spec.formatCookieHeader(Cookie)");
  -        String value = formatCookie(cookie);
           StringBuffer buffer = new StringBuffer();
  -        buffer.append("$Version=\"");
  -        buffer.append(cookie.getVersion());
  -        buffer.append("\"; ");
  -        buffer.append(value);
  +        buffer.append(formatNameValuePair("$Version", 
Integer.toString(cookie.getVersion()), cookie.getVersion()));
  +        buffer.append("; ");
  +        buffer.append(formatCookie(cookie));
           return new Header("Cookie", buffer.toString());
       }
   }
  
  
  
  1.17      +75 -15    
jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestCookie.java
  
  Index: TestCookie.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestCookie.java,v
  retrieving revision 1.16
  retrieving revision 1.17
  diff -u -r1.16 -r1.17
  --- TestCookie.java   11 Dec 2002 13:16:09 -0000      1.16
  +++ TestCookie.java   16 Dec 2002 01:40:52 -0000      1.17
  @@ -223,7 +223,7 @@
           assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
           assertEquals("Path","/path",parsed[0].getPath());
           assertTrue("Secure",!parsed[0].getSecure());
  -        assertEquals("Version",1,parsed[0].getVersion());
  +        assertEquals("Version",0,parsed[0].getVersion());
       }
    
    
  @@ -240,7 +240,7 @@
           assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
           assertEquals("Path","/",parsed[0].getPath());
           assertTrue("Secure",!parsed[0].getSecure());
  -        assertEquals("Version",1,parsed[0].getVersion());
  +        assertEquals("Version",0,parsed[0].getVersion());
       }
   
       public void testParseWithWhiteSpace() throws Exception {
  @@ -429,7 +429,7 @@
       }
   
       public void testParseWithWrongNetscapeDomain2() throws Exception {
  -        Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; 
version=0; domain=.y.z");
  +        Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; 
domain=.y.z");
           try {
               Cookie[] parsed = Cookie.parse("x.y.z","/",setCookie);
               fail("HttpException exception should have been thrown");
  @@ -673,7 +673,7 @@
        */
       public void testDefaultConsttuctor() {
           Cookie dummy = new Cookie();
  -        assertEquals( "noname=\"null\"", dummy.toExternalForm() );
  +        assertEquals( "noname=null", dummy.toExternalForm() );
       }
   
       /**
  @@ -747,6 +747,26 @@
       }
   
       /**
  +     * Tests generic cookie formatting.
  +     */
  +    
  +    public void testGenericCookieFormatting() {
  +        Header setCookie = new Header(
  +          "Set-Cookie", "name=value; path=/; domain=.mydomain.com");
  +        try {
  +            CookieSpec parser = 
CookiePolicy.getSpecByPolicy(CookiePolicy.COMPATIBILITY);
  +            Cookie[] cookies = parser.parse("myhost.mydomain.com", 80, "/", false, 
setCookie );
  +            parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
  +            String s = parser.formatCookie(cookies[0]);
  +            assertEquals("name=value", s);
  +        }
  +        catch(HttpException e) {
  +            e.printStackTrace();
  +            fail("Unexpected exception: " + e.toString());
  +        }
  +    }    
  +
  +    /**
        * Tests Netscape specific cookie formatting.
        */
       
  @@ -772,22 +792,62 @@
        */
       
       public void testRFC2109CookieFormatting() {
  -        Header setCookie = new Header(
  -          "Set-Cookie", "name=\"value\"; path=\"/\"; domain=\".mydomain.com\"");
  +        CookieSpec parser = CookiePolicy.getSpecByPolicy(CookiePolicy.RFC2109);
  +        Header setCookie = null;
  +        Cookie[] cookies = null;
           try {
  -            CookieSpec parser = CookiePolicy.getSpecByPolicy(CookiePolicy.RFC2109);
  -            Cookie[] cookies = parser.parse("myhost.mydomain.com", 80, "/", false, 
setCookie );
  +            setCookie = new Header(
  +            "Set-Cookie", "name=\"value\"; version=\"1\"; path=\"/\"; 
domain=\".mydomain.com\"");
  +            cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie 
);
               parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
  -            String s = parser.formatCookie(cookies[0]);
  -            assertEquals(s, "name=\"value\"; $Domain=\".mydomain.com\"; 
$Path=\"/\"");
  +            String s1 = parser.formatCookie(cookies[0]);
  +            assertEquals(s1, "name=\"value\"; $Domain=\".mydomain.com\"; 
$Path=\"/\"");
  +
  +            setCookie = new Header(
  +            "Set-Cookie", "name=value; path=/; domain=.mydomain.com");
  +            cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie 
);
  +            parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
  +            String s2 = parser.formatCookie(cookies[0]);
  +            assertEquals(s2, "name=value; $Domain=.mydomain.com; $Path=/");
           }
           catch(HttpException e) {
               e.printStackTrace();
               fail("Unexpected exception: " + e.toString());
           }
       }
  -    
   
  +
  +    /**
  +     * Tests Netscape specific expire attribute parsing.
  +     */
       
  +    public void testNetscapeCookieExpireAttribute() {
  +        CookieSpec parser = 
CookiePolicy.getSpecByPolicy(CookiePolicy.NETSCAPE_DRAFT);
  +        Header setCookie = null;
  +        Cookie[] cookies = null;
  +        try {
  +            setCookie = new Header(
  +              "Set-Cookie", "name=value; path=/; domain=.mydomain.com; expires=Thu, 
01-Jan-2070 00:00:10 GMT; comment=no_comment");
  +            cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie 
);
  +            parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
  +        }
  +        catch(MalformedCookieException e) {
  +            e.printStackTrace();
  +            fail("Unexpected exception: " + e.toString());
  +        }
  +        try {
  +            setCookie = new Header(
  +              "Set-Cookie", "name=value; path=/; domain=.mydomain.com; expires=Thu 
01-Jan-2070 00:00:10 GMT; comment=no_comment");
  +            cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie 
);
  +            parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
  +            fail("MalformedCookieException must have been thrown");
  +        }
  +        catch(MalformedCookieException e) {
  +            //expected
  +        }
  +    }
  +    
  +
  +
   }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:[EMAIL PROTECTED]>
For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>

Reply via email to