commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From oglu...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient TestCookie.java TestWebappCookie.java
Date Fri, 20 Dec 2002 09:22:08 GMT
oglueck     2002/12/20 01:22:08

  Modified:    httpclient/src/java/org/apache/commons/httpclient
                        Cookie.java HttpMethodBase.java HttpState.java
               httpclient/src/java/org/apache/commons/httpclient/cookie
                        CookieSpecBase.java
               httpclient/src/test/org/apache/commons/httpclient
                        TestCookie.java TestWebappCookie.java
  Log:
  - deprecating all Cookie class methods whose functionality has been moved into the cookie
package
  - Cookie policy is now set via HttState object
  - will add convenience methods later
  
  Contributed by: Oleg Kalnichevski
  
  Revision  Changes    Path
  1.31      +24 -7     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.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- Cookie.java	16 Dec 2002 01:40:51 -0000	1.30
  +++ Cookie.java	20 Dec 2002 09:22:06 -0000	1.31
  @@ -138,10 +138,10 @@
               throw new IllegalArgumentException("Cookie name may not be blank");
           }
           if (name.indexOf(' ') != -1) {
  -            throw new IllegalArgumentException("Cookie name may contain blanks");
  +            throw new IllegalArgumentException("Cookie name may not contain blanks");
           }
           if (name.startsWith("$")) {
  -            throw new IllegalArgumentException("Cookie name may start with $");
  +            throw new IllegalArgumentException("Cookie name may not start with $");
           }
           this.setPath(path);
           this.setDomain(domain);
  @@ -456,6 +456,8 @@
        * @param path the path to which the request is being submitted
        * @param secure <tt>true</tt> if the request is using the HTTPS protocol
        * @param date the time at which the request is submitted
  +     * 
  +     * @deprecated use {@link CookieSpec} interface
        */
       public boolean matches(String domain, int port, String path, boolean secure, Date date)
       {
  @@ -470,6 +472,8 @@
        * @param domain the host to which the request is being submitted
        * @param port the port to which the request is being submitted (currenlty ignored)
        * @param path the path to which the request is being submitted
  +     * 
  +     * @deprecated use {@link CookieSpec} interface
        */
       public boolean matches(String domain, int port, String path, boolean secure) {
           log.trace("enter Cookie.matches(String, int, String, boolean");
  @@ -484,6 +488,8 @@
        * secure.
        * <p>
        * If no cookies match, returns null.
  +     * 
  +     * @deprecated use {@link CookieSpec} interface
        */
       public static Header createCookieHeader(String domain, String path, Cookie[] cookies)
{
           log.trace("enter Cookie.createCookieHeader(String, String, Cookie[])");
  @@ -498,7 +504,8 @@
        * <p>
        * If no cookies match, returns null.
        * @exception java.lang.IllegalArgumentException if domain or path is null
  -     * @deprecated use the version which includes port number and date
  +     * 
  +     * @deprecated use {@link CookieSpec} interface
        */
       public static Header createCookieHeader(String domain, String path, boolean secure,
Cookie[] cookies)
       throws IllegalArgumentException {
  @@ -530,6 +537,8 @@
        * <p>
        * If no cookies match, returns null.
        * @exception java.lang.IllegalArgumentException if domain or path is null
  +     * 
  +     * @deprecated use {@link CookieSpec} interface
        */
       public static Header createCookieHeader(String domain, int port, String path, boolean
secure, Cookie[] cookies) 
       throws IllegalArgumentException {
  @@ -546,6 +555,8 @@
        * <p>
        * If no cookies match, returns null.
        * @exception java.lang.IllegalArgumentException if domain or path is null
  +     * 
  +     * @deprecated use {@link CookieSpec} interface
        */
   
       public static Header createCookieHeader(String domain, int port, String path, boolean
secure, Date now, Cookie[] cookies) 
  @@ -642,6 +653,8 @@
        * @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie {@link
Header}
        * @throws HttpException if an exception occurs during parsing
        * @throws java.lang.IllegalArgumentException if domain or path are null
  +     * 
  +     * @deprecated use {@link CookieSpec} interface
        */
       public static Cookie[] parse(String domain, String path, Header setCookie) 
       throws HttpException, IllegalArgumentException {
  @@ -660,6 +673,8 @@
        * @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie {@link
Header}
        * @throws HttpException if an exception occurs during parsing
        * @throws java.lang.IllegalArgumentException if domain or path are null
  +     * 
  +     * @deprecated use {@link CookieSpec} interface
        */
       public static Cookie[] parse(String domain, String path, boolean secure, Header setCookie)

       throws HttpException {
  @@ -694,6 +709,8 @@
         * @return an array of <tt>Cookie</tt>s parsed from the Set-Cookie {@link
Header}
         * @throws HttpException if an exception occurs during parsing
         * @throws java.lang.IllegalArgumentException if domain or path are null
  +      * 
  +      * @deprecated use {@link CookieSpec} interface
         */
       public static Cookie[] parse(String domain, int port, String path, boolean secure,
Header setCookie) 
           throws HttpException, IllegalArgumentException {
  
  
  
  1.93      +6 -6      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.92
  retrieving revision 1.93
  diff -u -r1.92 -r1.93
  --- HttpMethodBase.java	19 Dec 2002 10:33:38 -0000	1.92
  +++ HttpMethodBase.java	20 Dec 2002 09:22:07 -0000	1.93
  @@ -1276,7 +1276,7 @@
           log.trace("enter HttpMethodBase.addCookieRequestHeader(HttpState, "
                     + "HttpConnection)");
   
  -        CookieSpec matcher = CookiePolicy.getDefaultSpec();
  +        CookieSpec matcher = CookiePolicy.getSpecByPolicy(state.getCookiePolicy());
           Cookie[] cookies = matcher.match(
             conn.getHost(),
             conn.getPort(),
  @@ -1565,7 +1565,7 @@
           if (setCookieHeader == null) return;
           try {
   
  -            CookieSpec parser = CookiePolicy.getDefaultSpec();
  +            CookieSpec parser = CookiePolicy.getSpecByPolicy(state.getCookiePolicy());
               Cookie[] cookies = parser.parse(
                 conn.getHost(),
                 conn.getPort(),
  
  
  
  1.14      +62 -9     jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpState.java
  
  Index: HttpState.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/java/org/apache/commons/httpclient/HttpState.java,v
  retrieving revision 1.13
  retrieving revision 1.14
  diff -u -r1.13 -r1.14
  --- HttpState.java	3 Dec 2002 05:46:15 -0000	1.13
  +++ HttpState.java	20 Dec 2002 09:22:07 -0000	1.14
  @@ -69,6 +69,8 @@
   import java.util.List;
   import java.util.Iterator;
   
  +import org.apache.commons.httpclient.cookie.CookieSpec;
  +import org.apache.commons.httpclient.cookie.CookiePolicy;
   import org.apache.commons.logging.Log;
   import org.apache.commons.logging.LogFactory;
   
  @@ -83,6 +85,7 @@
    * @author <a href="mailto:jsdever@apache.org">Jeff Dever</a>
    * @author Sean C. Sullivan
    * @author <a href="mailto:becke@u.washington.edu">Michael Becke</a>
  + * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
    * 
    * @version $Revision$ $Date$
    * 
  @@ -105,6 +108,10 @@
        * My {@link Cookie Cookie}s.
        */
       private ArrayList cookies = new ArrayList();
  +    /**
  +     * My cookie policy.
  +     */
  +    private int cookiePolicy = CookiePolicy.RFC2109;
   
       private HttpConnectionManager httpConnectionManager;
   
  @@ -121,6 +128,7 @@
           super();
           
           this.httpConnectionManager = new SimpleHttpConnectionManager();
  +        this.cookiePolicy = CookiePolicy.getDefaultPolicy();
           
       }
   
  @@ -202,6 +210,7 @@
        * @see Cookie#matches
        * @see #getCookies()
        * 
  +     * @deprecated use HttpState.getCookies(String, int, String, boolean)
        */
       public synchronized Cookie[] getCookies(
           String domain, 
  @@ -210,19 +219,43 @@
           boolean secure, 
           Date now
       ) {
  -        log.trace("enter HttpState.getCookies(String, int, String, boolean, Date)");
  +        return getCookies(domain, port, path, secure);
  +    }
  +
  +
  +    /**
  +     * Obtain an array of my {@link Cookie}s that
  +     * match the given request parameters.
  +     * 
  +     * @param domain the request domain
  +     * @param port the request port
  +     * @param path the request path
  +     * @param secure <code>true</code> when using HTTPS
  +     * @return an array of my {@link Cookie}s.
  +     * 
  +     * @see Cookie#matches
  +     * @see #getCookies()
  +     * 
  +     */
  +    public synchronized Cookie[] getCookies(
  +        String domain, 
  +        int port, 
  +        String path, 
  +        boolean secure
  +    ) {
  +        log.trace("enter HttpState.getCookies(String, int, String, boolean)");
   
  +		CookieSpec matcher = CookiePolicy.getDefaultSpec();
           ArrayList list = new ArrayList(cookies.size());
           for(int i=0,m=cookies.size();i<m;i++) {
  -            Cookie c = (Cookie)(cookies.get(i));
  -            if(c.matches(domain,port,path,secure,now)) {
  -                list.add(c);
  +            Cookie cookie = (Cookie)(cookies.get(i));
  +            if(matcher.match(domain, port, path, secure, cookie)) {
  +                list.add(cookie);
               }
           }
           return (Cookie[])(list.toArray(new Cookie[list.size()]));
       }
   
  -
       /**
        * Remove all of my {@link Cookie}s that
        * have expired according to the current
  @@ -255,6 +288,26 @@
               }
           }
           return removed;
  +    }
  +
  +
  +    /**
  +     * @return cookie policy <tt>(COMPATIBILITY | NETSCAPE_DRAFT | RFC2109)</tt>
  +     */
  +    
  +    public int getCookiePolicy()
  +    {
  +        return this.cookiePolicy;
  +    }
  +    
  +
  +    /**
  +     * @param new cookie policy <tt>(COMPATIBILITY | NETSCAPE_DRAFT | RFC2109)</tt>
  +     */
  +    
  +    public void setCookiePolicy(int policy)
  +    {
  +        this.cookiePolicy = policy;
       }
   
   
  
  
  
  1.5       +1 -1      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.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- CookieSpecBase.java	16 Dec 2002 01:40:52 -0000	1.4
  +++ CookieSpecBase.java	20 Dec 2002 09:22:07 -0000	1.5
  @@ -579,7 +579,7 @@
           List matching = new LinkedList();
           for(int i=0;i<cookies.length;i++)
           {
  -            if(cookies[i].matches(host, port, path, secure))
  +            if(match(host, port, path, secure, cookies[i]))
               {
                   addInPathOrder(matching, cookies[i]);
               }
  
  
  
  1.18      +121 -50   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.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- TestCookie.java	16 Dec 2002 01:40:52 -0000	1.17
  +++ TestCookie.java	20 Dec 2002 09:22:07 -0000	1.18
  @@ -117,6 +117,77 @@
       }
   
   
  +    // ------------------------------------------------------- Helper Methods
  +
  +	private static Cookie[] cookieParse(int policy, String host, String path, boolean isSecure,
Header setHeader)
  +	  throws MalformedCookieException 
  +	{
  +		CookieSpec parser = CookiePolicy.getSpecByPolicy(policy);
  +		Cookie[] cookies = parser.parse(host, DEFAULT_PORT, path, isSecure, setHeader);
  +		if (cookies != null)
  +		{
  +			for(int i = 0; i < cookies.length; i++)
  +			{
  +				parser.validate(host, DEFAULT_PORT, path, isSecure, cookies[i]);
  +			}
  +		}
  +		return cookies;
  +	}
  +
  +
  +	private static Cookie[] cookieParse(String host, String path, boolean isSecure, Header
setHeader)
  +	  throws MalformedCookieException 
  +	{
  +		return cookieParse(CookiePolicy.RFC2109, host, path, isSecure, setHeader);
  +	}
  +
  +
  +	private static Cookie[] cookieParse(String host, String path, Header setHeader)
  +	  throws MalformedCookieException 
  +	{
  +		return cookieParse(CookiePolicy.RFC2109, host, path, false, setHeader);
  +	}
  +
  +
  +	private static Cookie[] netscapeCcookieParse(String host, String path, Header setHeader)
  +	  throws MalformedCookieException 
  +	{
  +		return cookieParse(CookiePolicy.NETSCAPE_DRAFT, host, path, false, setHeader);
  +	}
  +
  +
  +    public static Header cookieCreateHeader(int policy, String domain, int port, String
path, boolean secure, Cookie[] cookies)
  +    {
  +        CookieSpec matcher = CookiePolicy.getSpecByPolicy(policy);
  +        cookies = matcher.match(domain, port, path, secure, cookies);
  +        if ((cookies != null) && (cookies.length > 0))
  +        {
  +            return matcher.formatCookieHeader(cookies);
  +        }
  +        else
  +        {
  +            return null;
  +        } 
  +    }
  +
  +    public static Header cookieCreateHeader(String domain, int port, String path, boolean
secure, Cookie[] cookies)
  +    {
  +    	return cookieCreateHeader(CookiePolicy.RFC2109, domain, port, path, secure, cookies);
  +    }
  +
  +
  +    public boolean cookieMatch(int policy, String domain, int port, String path, boolean
secure, Cookie cookie)
  +    {
  +        CookieSpec matcher = CookiePolicy.getSpecByPolicy(policy);
  +        return matcher.match(domain, port, path, secure, cookie);
  +    }
  +
  +
  +    public boolean cookieMatch(String domain, int port, String path, boolean secure, Cookie
cookie)
  +    {
  +        return cookieMatch(CookiePolicy.RFC2109, domain, port, path, secure, cookie);
  +    }
  +
       // ------------------------------------------------------------ Parse1 Test
   
   
  @@ -126,7 +197,7 @@
       public void testParse1() throws Exception {
           String headerValue = "custno = 12345; comment=test; version=1," +
               " name=John; version=1; max-age=600; secure; domain=.apache.org";
  -        Cookie[] cookies = Cookie.parse(DOMAIN_NAME,"/", true, new Header(
  +        Cookie[] cookies = cookieParse(DOMAIN_NAME,"/", true, new Header(
               "set-cookie", headerValue));
           checkResultsOfParse(cookies, 2, 0);
       }
  @@ -166,7 +237,7 @@
       public void testParse2() throws Exception {
           String headerValue = "custno=12345;comment=test; version=1," +
               "name=John;version=1;max-age=600;secure;domain=.apache.org";
  -        Cookie[] cookies = Cookie.parse(DOMAIN_NAME, "/", true, new Header(
  +        Cookie[] cookies = cookieParse(DOMAIN_NAME, "/", true, new Header(
               "set-cookie", headerValue));
           checkResultsOfParse(cookies, 2, 0);
       }
  @@ -181,7 +252,7 @@
       public void testParse3() throws Exception {
           String headerValue =
               "name=\"Doe, John\";version=1;max-age=600;secure;domain=.apache.org";
  -        Cookie[] cookies = Cookie.parse(DOMAIN_NAME,"/", true, new Header(
  +        Cookie[] cookies = cookieParse(DOMAIN_NAME,"/", true, new Header(
               "set-cookie", headerValue));
           checkResultsOfParse(cookies, 1, 2);
       }
  @@ -191,7 +262,7 @@
       // see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5279
       public void testQuotedExpiresAttribute() throws Exception {
           String headerValue = "custno=12345;Expires='Thu, 01-Jan-2070 00:00:10 GMT'";
  -        Cookie[] cookies = Cookie.parse(DOMAIN_NAME,"/",true,new Header(
  +        Cookie[] cookies = cookieParse(DOMAIN_NAME,"/",true,new Header(
               "set-cookie", headerValue));
           assertNotNull("Expected some cookies",cookies);
           assertEquals("Expected 1 cookie",1,cookies.length);
  @@ -202,7 +273,7 @@
           String headerValue = "custno=12345;comment=test; version=1," +
               "name=John;version=1;max-age=600;secure;domain=jakarta.apache.org";
           try {
  -            Cookie[] cookies = Cookie.parse(DOMAIN_NAME, "/", new Header(
  +            Cookie[] cookies = cookieParse(DOMAIN_NAME, "/", new Header(
                   "set-cookie", headerValue));
               fail("HttpException exception should have been thrown");
           } catch (HttpException e) {
  @@ -212,7 +283,7 @@
   
       public void testParseSimple() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value");
  -        Cookie[] parsed = Cookie.parse("127.0.0.1","/path/path",setCookie);
  +        Cookie[] parsed = cookieParse("127.0.0.1","/path/path",setCookie);
           assertEquals("Found 1 cookie.",1,parsed.length);
           assertEquals("Name","cookie-name",parsed[0].getName());
           assertEquals("Value","cookie-value",parsed[0].getValue());
  @@ -229,7 +300,7 @@
    
       public void testParseNoValue() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=");
  -        Cookie[] parsed = Cookie.parse("127.0.0.1","/",setCookie);
  +        Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie);
           assertEquals("Found 1 cookie.",1,parsed.length);
           assertEquals("Name","cookie-name",parsed[0].getName());
           assertTrue("Value",null == parsed[0].getValue());
  @@ -245,7 +316,7 @@
   
       public void testParseWithWhiteSpace() throws Exception {
           Header setCookie = new Header("Set-Cookie"," cookie-name  =    cookie-value  ");
  -        Cookie[] parsed = Cookie.parse("127.0.0.1","/",setCookie);
  +        Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie);
           assertEquals("Found 1 cookie.",1,parsed.length);
           assertEquals("Name","cookie-name",parsed[0].getName());
           assertEquals("Value","cookie-value",parsed[0].getValue());
  @@ -258,7 +329,7 @@
   
       public void testParseWithQuotes() throws Exception {
           Header setCookie = new Header("Set-Cookie"," cookie-name  =  \" cookie-value \"
;path=/");
  -        Cookie[] parsed = Cookie.parse("127.0.0.1","/",setCookie);
  +        Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie);
           assertEquals("Found 1 cookie.",1,parsed.length);
           assertEquals("Name","cookie-name",parsed[0].getName());
           assertEquals("Value"," cookie-value ",parsed[0].getValue());
  @@ -271,7 +342,7 @@
   
       public void testParseWithPath() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; Path=/path/");
  -        Cookie[] parsed = Cookie.parse("127.0.0.1","/path/path",setCookie);
  +        Cookie[] parsed = cookieParse("127.0.0.1","/path/path",setCookie);
           assertEquals("Found 1 cookie.",1,parsed.length);
           assertEquals("Name","cookie-name",parsed[0].getName());
           assertEquals("Value","cookie-value",parsed[0].getValue());
  @@ -284,7 +355,7 @@
   
       public void testParseWithDomain() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; Domain=127.0.0.1");
  -        Cookie[] parsed = Cookie.parse("127.0.0.1","/",setCookie);
  +        Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie);
           assertEquals("Found 1 cookie.",1,parsed.length);
           assertEquals("Name","cookie-name",parsed[0].getName());
           assertEquals("Value","cookie-value",parsed[0].getValue());
  @@ -297,7 +368,7 @@
   
       public void testParseWithSecure() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; secure");
  -        Cookie[] parsed = Cookie.parse("127.0.0.1","/",true,setCookie);
  +        Cookie[] parsed = cookieParse("127.0.0.1","/",true,setCookie);
           assertEquals("Found 1 cookie.",1,parsed.length);
           assertEquals("Name","cookie-name",parsed[0].getName());
           assertEquals("Value","cookie-value",parsed[0].getValue());
  @@ -310,7 +381,7 @@
   
       public void testParseWithComment() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; comment=\"This
is a comment.\"");
  -        Cookie[] parsed = Cookie.parse("127.0.0.1","/",true,setCookie);
  +        Cookie[] parsed = cookieParse("127.0.0.1","/",true,setCookie);
           assertEquals("Found 1 cookie.",1,parsed.length);
           assertEquals("Name","cookie-name",parsed[0].getName());
           assertEquals("Value","cookie-value",parsed[0].getValue());
  @@ -323,7 +394,7 @@
   
       public void testParseWithExpires() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Expires=Thu,
01-Jan-1970 00:00:10 GMT");
  -        Cookie[] parsed = Cookie.parse("127.0.0.1","/",true,setCookie);
  +        Cookie[] parsed = cookieParse("127.0.0.1","/",true,setCookie);
           assertEquals("Found 1 cookie.",1,parsed.length);
           assertEquals("Name","cookie-name",parsed[0].getName());
           assertEquals("Value","cookie-value",parsed[0].getValue());
  @@ -336,7 +407,7 @@
   
       public void testParseWithAll() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Version=1;Path=/commons;Domain=.apache.org;Comment=This
is a comment.;secure;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
  -        Cookie[] parsed = Cookie.parse(".apache.org","/commons/httpclient",true,setCookie);
  +        Cookie[] parsed = cookieParse(".apache.org","/commons/httpclient",true,setCookie);
           assertEquals("Found 1 cookie.",1,parsed.length);
           assertEquals("Name","cookie-name",parsed[0].getName());
           assertEquals("Value","cookie-value",parsed[0].getValue());
  @@ -350,7 +421,7 @@
   
       public void testParseMultipleDifferentPaths() throws Exception {
           Header setCookie = new Header("Set-Cookie","name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons/httpclient;Version=1");
  -        Cookie[] parsed = Cookie.parse(".apache.org","/commons/httpclient",true,setCookie);
  +        Cookie[] parsed = cookieParse(".apache.org","/commons/httpclient",true,setCookie);
           HttpState state = new HttpState();
           state.addCookies(parsed);
           Cookie[] cookies = state.getCookies();
  @@ -363,7 +434,7 @@
   
       public void testParseMultipleSamePaths() throws Exception {
           Header setCookie = new Header("Set-Cookie","name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons");
  -        Cookie[] parsed = Cookie.parse(".apache.org","/commons/httpclient",true,setCookie);
  +        Cookie[] parsed = cookieParse(".apache.org","/commons/httpclient",true,setCookie);
           HttpState state = new HttpState();
           state.addCookies(parsed);
           Cookie[] cookies = state.getCookies();
  @@ -375,7 +446,7 @@
       public void testParseWithWrongDomain() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1;
version=1");
           try {
  -            Cookie[] parsed = Cookie.parse("127.0.0.2","/",setCookie);
  +            Cookie[] parsed = cookieParse("127.0.0.2","/",setCookie);
               fail("HttpException exception should have been thrown");
           } catch (HttpException e) {
               // expected
  @@ -385,7 +456,7 @@
       public void testParseWithWrongDomain2() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.c.com;
version=1");
           try {
  -            Cookie[] parsed = Cookie.parse("a.b.c.com","/",setCookie);
  +            Cookie[] parsed = cookieParse("a.b.c.com","/",setCookie);
               fail("HttpException exception should have been thrown");
           } catch (HttpException e) {
               // expected
  @@ -398,7 +469,7 @@
       public void testParseWithIllegalDomain() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com;
version=1");
           try {
  -            Cookie[] parsed = Cookie.parse("b.com","/",setCookie);
  +            Cookie[] parsed = cookieParse("b.com","/",setCookie);
               fail("HttpException exception should have been thrown");
           } catch (HttpException e) {
               // expected
  @@ -411,7 +482,7 @@
       public void testParseWithIllegalDomain2() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com.;
version=1");
           try {
  -            Cookie[] parsed = Cookie.parse("b.com","/",setCookie);
  +            Cookie[] parsed = cookieParse("b.com","/",setCookie);
               fail("HttpException exception should have been thrown");
           } catch (HttpException e) {
               // expected
  @@ -421,7 +492,7 @@
       public void testParseWithIllegalNetscapeDomain1() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com");
           try {
  -            Cookie[] parsed = Cookie.parse("a.com","/",setCookie);
  +            Cookie[] parsed = netscapeCcookieParse("a.com","/",setCookie);
               fail("HttpException exception should have been thrown");
           } catch (HttpException e) {
               // expected
  @@ -431,7 +502,7 @@
       public void testParseWithWrongNetscapeDomain2() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.y.z");
           try {
  -            Cookie[] parsed = Cookie.parse("x.y.z","/",setCookie);
  +            Cookie[] parsed = netscapeCcookieParse("x.y.z","/",setCookie);
               fail("HttpException exception should have been thrown");
           } catch (HttpException e) {
               // expected
  @@ -441,7 +512,7 @@
       public void testParseWithWrongPath() throws Exception {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1;
path=/not/just/root");
           try {
  -            Cookie[] parsed = Cookie.parse("127.0.0.1","/",setCookie);
  +            Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie);
               fail("HttpException exception should have been thrown");
           } catch (HttpException e) {
               // expected
  @@ -451,7 +522,7 @@
       public void testParseWithNullDomain() {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1;
path=/; secure");
           try {
  -            Cookie[] parsed = Cookie.parse(null,"/",false,setCookie);
  +            Cookie[] parsed = cookieParse(null,"/",false,setCookie);
               fail("IllegalArgumentException should have been thrown");
           } catch (IllegalArgumentException e) {
               // expected
  @@ -463,7 +534,7 @@
       public void testParseWithNullPath() {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1;
path=/; secure");
           try {
  -            Cookie[] parsed = Cookie.parse("127.0.0.1",null,false,setCookie);
  +            Cookie[] parsed = cookieParse("127.0.0.1",null,false,setCookie);
               fail("IllegalArgumentException should have been thrown");
           } catch (IllegalArgumentException e) {
               // expected
  @@ -475,7 +546,7 @@
       public void testParseWithNullDomainAndPath() {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1;
path=/; secure");
           try {
  -            Cookie[] parsed = Cookie.parse(null,null,false,setCookie);
  +            Cookie[] parsed = cookieParse(null,null,false,setCookie);
               fail("IllegalArgumentException should have been thrown");
           } catch (IllegalArgumentException e) {
               // expected
  @@ -487,7 +558,7 @@
       public void testParseWithPathMismatch() {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; path=/path/path/path");
           try {
  -            Cookie[] parsed = Cookie.parse("127.0.0.1","/path",false,setCookie);
  +            Cookie[] parsed = cookieParse("127.0.0.1","/path",false,setCookie);
               fail("HttpException should have been thrown.");
           } catch (HttpException e) {
               // expected
  @@ -499,7 +570,7 @@
       public void testParseWithPathMismatch2() {
           Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; path=/foobar");
           try {
  -            Cookie[] parsed = Cookie.parse("127.0.0.1","/foo",false,setCookie);
  +            Cookie[] parsed = cookieParse("127.0.0.1","/foo",false,setCookie);
               fail("HttpException should have been thrown.");
           } catch (HttpException e) {
               // expected
  @@ -514,24 +585,24 @@
           Vector cookies = new Vector();
           // Cookie 0
           setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.apache.org;Expires=Thu,
01-Jan-1970 00:00:10 GMT");
  -        parsed = Cookie.parse(".apache.org", "/commons/httpclient", true,
  +        parsed = cookieParse(".apache.org", "/commons/httpclient", true,
                                 setCookie);
           cookies.add(parsed[0]);
           // Cookie 1
           setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons/bif;Domain=.apache.org;Expires=Thu,
01-Jan-1970 00:00:10 GMT");
  -        parsed = Cookie.parse(".apache.org","/commons/bif/httpclient",true,setCookie);
  +        parsed = cookieParse(".apache.org","/commons/bif/httpclient",true,setCookie);
           cookies.add(parsed[0]);
           // Cookie 2
           setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.baz.org;Expires=Thu,
01-Jan-1970 00:00:10 GMT");
  -        parsed = Cookie.parse(".baz.org","/commons/httpclient",true,setCookie);
  +        parsed = cookieParse(".baz.org","/commons/httpclient",true,setCookie);
           cookies.add(parsed[0]);
           // Cookie 3
           setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons/bif;Domain=.baz.org;Expires=Thu,
01-Jan-1970 00:00:10 GMT");
  -        parsed = Cookie.parse(".baz.org","/commons/bif/httpclient",true,setCookie);
  +        parsed = cookieParse(".baz.org","/commons/bif/httpclient",true,setCookie);
           cookies.add(parsed[0]);
           // Cookie 4
           setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.baz.com;Expires=Thu,
01-Jan-1970 00:00:10 GMT");
  -        parsed = Cookie.parse(".baz.com","/commons/httpclient",true,setCookie);
  +        parsed = cookieParse(".baz.com","/commons/httpclient",true,setCookie);
           cookies.add(parsed[0]);
           // The order should be:
           // 1, 0, 3, 2, 4
  @@ -575,10 +646,10 @@
       public void testCreateCookieHeaderWithNullDomain() throws Exception {
           Header setCookie = new Header("Set-Cookie",
                                         TEST_COOKIE + SEP + OLD_EXPIRY);
  -        Cookie[] parsed = Cookie.parse(DOMAIN_NAME, ROOT_PATH, true, setCookie);
  +        Cookie[] parsed = cookieParse(DOMAIN_NAME, ROOT_PATH, true, setCookie);
   
           try{
  -            Header header = Cookie.createCookieHeader(null, DEFAULT_PORT, ROOT_PATH, false,
parsed);
  +            Header header = cookieCreateHeader(null, DEFAULT_PORT, ROOT_PATH, false, parsed);
               fail("IllegalArgumentException should have been thrown.");
           }catch(IllegalArgumentException e){
               // Expected
  @@ -592,10 +663,10 @@
       public void testCreateCookieHeaderWithNullPath() throws Exception{
           Header setCookie = new Header("Set-Cookie",
                                         TEST_COOKIE + SEP + OLD_EXPIRY);
  -        Cookie[] parsed = Cookie.parse(DOMAIN_NAME, ROOT_PATH, false, setCookie);
  +        Cookie[] parsed = cookieParse(DOMAIN_NAME, ROOT_PATH, false, setCookie);
   
           try{
  -            Header header = Cookie.createCookieHeader(DOMAIN_NAME, DEFAULT_PORT, null,
false, parsed);
  +            Header header = cookieCreateHeader(DOMAIN_NAME, DEFAULT_PORT, null, false,
parsed);
               fail("IllegalArgumentException should have been thrown.");
           }catch(IllegalArgumentException e){
               // Expected
  @@ -612,7 +683,7 @@
           cookies[0] = new Cookie(null, "name0", "value0");
           cookies[1] = new Cookie(null, "name1", "value1", null, null, false);
   
  -        Header header = Cookie.createCookieHeader(DOMAIN_NAME, DEFAULT_PORT, ROOT_PATH,
false, cookies);
  +        Header header = cookieCreateHeader(DOMAIN_NAME, DEFAULT_PORT, ROOT_PATH, false,
cookies);
           assertEquals("createCookieHeader added cookies with null domains or paths", null,
header);
       }
   
  @@ -622,10 +693,10 @@
       public void testCreateCookieHeaderWithNullDomainAndPath() throws Exception {
           Header setCookie = new Header("Set-Cookie",
                                         TEST_COOKIE + SEP + OLD_EXPIRY);
  -        Cookie[] parsed = Cookie.parse(DOMAIN_NAME, ROOT_PATH, true, setCookie);
  +        Cookie[] parsed = cookieParse(DOMAIN_NAME, ROOT_PATH, true, setCookie);
   
           try{
  -            Header header = Cookie.createCookieHeader(null, DEFAULT_PORT, null, false,
parsed);
  +            Header header = cookieCreateHeader(null, DEFAULT_PORT, null, false, parsed);
               fail("IllegalArgumentException should have been thrown.");
           }catch(IllegalArgumentException e){
               // Expected
  @@ -664,7 +735,7 @@
   
       private void checkDate(String date) throws Exception {
           Header setCookie = new Header("Set-Cookie", "custno=12345;Expires='"+date+"'");
  -        Cookie.parse("localhost","/",setCookie);
  +        cookieParse("localhost","/",setCookie);
       }
       
       
  @@ -683,7 +754,7 @@
           Header setCookie = new Header(
             "Set-Cookie", "name=value; path=/; domain=.sybase.com");
           try {
  -            Cookie[] parsed = Cookie.parse("www.Sybase.com", "/", false, setCookie );
  +            Cookie[] parsed = cookieParse("www.Sybase.com", "/", false, setCookie );
           }
           catch(HttpException e) {
               e.printStackTrace();
  @@ -730,12 +801,12 @@
           Cookie cookie = new Cookie(".test.com", "test", "1", "/test", null, false);
           
           try {
  -            boolean match = cookie.matches(
  +            boolean match = cookieMatch(
                   "test.test.com", 
                   80, 
                   "/test", 
  -                false, 
  -                new Date()
  +                false,
  +                cookie
               );
               
               assertTrue("Cookie paths did not match", match);
  
  
  
  1.6       +81 -20    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappCookie.java
  
  Index: TestWebappCookie.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappCookie.java,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- TestWebappCookie.java	8 Dec 2002 06:09:46 -0000	1.5
  +++ TestWebappCookie.java	20 Dec 2002 09:22:07 -0000	1.6
  @@ -63,6 +63,8 @@
   package org.apache.commons.httpclient;
   
   import junit.framework.*;
  +
  +import org.apache.commons.httpclient.cookie.CookiePolicy;
   import org.apache.commons.httpclient.methods.*;
   
   /**
  @@ -82,6 +84,8 @@
    * "httpclient.test.webappContext" property.
    *
    * @author Rodney Waldhoff
  + * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
  + * 
    * @version $Id$
    */
   public class TestWebappCookie extends TestWebappBase {
  @@ -106,7 +110,6 @@
       public void testSetCookieGet() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
           GetMethod method = new GetMethod("/" + context + "/cookie/write");
           method.setQueryString("simple=set");
           method.setUseDisk(false);
  @@ -147,7 +150,6 @@
       public void testSetCookiePut() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
           PutMethod method = new PutMethod("/" + context + "/cookie/write");
           method.setQueryString("simple=set");
           method.setRequestBody("data to be sent via http post");
  @@ -168,7 +170,6 @@
       public void testSetExpiredCookieGet() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
           GetMethod method = new GetMethod("/" + context + "/cookie/write");
           method.setQueryString("simple=unset");
           method.setUseDisk(false);
  @@ -187,7 +188,6 @@
       public void testSetExpiredCookiePut() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
           PutMethod method = new PutMethod("/" + context + "/cookie/write");
           method.setQueryString("simple=unset");
           method.setRequestBody("data to be sent via http post");
  @@ -206,7 +206,6 @@
       public void testSetUnsetCookieGet() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
           GetMethod method = new GetMethod("/" + context + "/cookie/write");
           method.setQueryString("simple=set");
           method.setUseDisk(false);
  @@ -242,7 +241,6 @@
       public void testSetMultiCookieGet() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
           GetMethod method = new GetMethod("/" + context + "/cookie/write");
           method.setQueryString("simple=set&domain=set");
           method.setUseDisk(false);
  @@ -266,7 +264,6 @@
       public void testSetMultiCookiePut() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
           PutMethod method = new PutMethod("/" + context + "/cookie/write");
           method.setQueryString("simple=set&domain=set");
           method.setRequestBody("data to be sent via http post");
  @@ -290,7 +287,6 @@
       public void testSendCookieGet() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
           GetMethod method = new GetMethod("/" + context + "/cookie/write");
           method.setQueryString("simple=set");
           method.setUseDisk(false);
  @@ -324,9 +320,7 @@
   
       public void testMultiSendCookieGet() throws Exception {
           HttpClient client = new HttpClient();
  -        client.setStrictMode(true);
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
           GetMethod method = new GetMethod("/" + context + "/cookie/write");
           method.setQueryString("simple=set&domain=set");
           method.setUseDisk(false);
  @@ -365,7 +359,6 @@
       public void testDeleteCookieGet() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
   
           {
               GetMethod method = new GetMethod("/" + context + "/cookie/write");
  @@ -443,7 +436,6 @@
       public void testDeleteCookiePut() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
   
           {
               PutMethod method = new PutMethod("/" + context + "/cookie/write");
  @@ -521,7 +513,6 @@
       public void testPathCookie1() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
   
           {
               GetMethod method = new GetMethod("/" + context + "/cookie/write");
  @@ -560,7 +551,6 @@
       public void testPathCookie2() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
   
           {
               GetMethod method = new GetMethod("/" + context + "/cookie/write");
  @@ -599,7 +589,6 @@
       public void testPathCookie3() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
           {
               GetMethod method = new GetMethod("/" + context + "/cookie/write");
               method.setQueryString("path=/" + context + "/cookie");
  @@ -637,7 +626,6 @@
       public void testPathCookie4() throws Exception {
           HttpClient client = new HttpClient();
           client.getHostConfiguration().setHost(host, port, "http");
  -        client.setStrictMode(true);
   
           {
               GetMethod method = new GetMethod("/" + context + "/cookie/write");
  @@ -671,5 +659,78 @@
               assertTrue(method.getResponseBodyAsString().indexOf("<tt>pathcookie=value</tt><br>")
== -1);
           }
       }
  +    
  +    
  +    public void testCookiePolicies() {
  +        HttpClient client = new HttpClient();
  +        client.getHostConfiguration().setHost(host, port, "http");
  +
  +        {
  +        	client.getState().setCookiePolicy(CookiePolicy.RFC2109);
  +	        GetMethod method = new GetMethod("/" + context + "/cookie/write");
  +	        method.setQueryString("simple=set");
  +	        method.setUseDisk(false);
  +	        try {
  +	            client.executeMethod(method);
  +	        } catch (Throwable t) {
  +	            t.printStackTrace();
  +	            fail("Unable to execute method : " + t.toString());
  +	        }
  +	        assertEquals(200,method.getStatusCode());
  +	        assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet:
GET</title>") >= 0);
  +	        assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>")
>= 0);
  +	        assertEquals(1,client.getState().getCookies().length);
  +	        assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName());
  +	        assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue());
  +	
  +	        GetMethod method2 = new GetMethod("/" + context + "/cookie/read");
  +	        method2.setUseDisk(false);
  +	        try {
  +	            client.executeMethod(method2);
  +	        } catch (Throwable t) {
  +	            t.printStackTrace();
  +	            fail("Unable to execute method : " + t.toString());
  +	        }
  +	        assertEquals(200,method2.getStatusCode());
  +	        String s = method2.getResponseBodyAsString();
  +	        assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >=
0);
  +	        assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; simplecookie=\"value\"</tt></p>")
>= 0);
  +	        assertTrue(s, s.indexOf("<tt>simplecookie=\"value\"</tt><br>")
>= 0);
  +        }
  +
  +        {
  +        	client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY);
  +	        GetMethod method = new GetMethod("/" + context + "/cookie/write");
  +	        method.setQueryString("simple=set");
  +	        method.setUseDisk(false);
  +	        try {
  +	            client.executeMethod(method);
  +	        } catch (Throwable t) {
  +	            t.printStackTrace();
  +	            fail("Unable to execute method : " + t.toString());
  +	        }
  +	        assertEquals(200,method.getStatusCode());
  +	        assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet:
GET</title>") >= 0);
  +	        assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>")
>= 0);
  +	        assertEquals(1,client.getState().getCookies().length);
  +	        assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName());
  +	        assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue());
  +	
  +	        GetMethod method2 = new GetMethod("/" + context + "/cookie/read");
  +	        method2.setUseDisk(false);
  +	        try {
  +	            client.executeMethod(method2);
  +	        } catch (Throwable t) {
  +	            t.printStackTrace();
  +	            fail("Unable to execute method : " + t.toString());
  +	        }
  +	        assertEquals(200,method2.getStatusCode());
  +	        String s = method2.getResponseBodyAsString();
  +	        assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >=
0);
  +	        assertTrue(s, s.indexOf("<p><tt>Cookie: simplecookie=value</tt></p>")
>= 0);
  +	        assertTrue(s, s.indexOf("<tt>simplecookie=value</tt><br>") >=
0);
  +        }
  +    }
  +    
   }
   
  
  
  

--
To unsubscribe, e-mail:   <mailto:commons-dev-unsubscribe@jakarta.apache.org>
For additional commands, e-mail: <mailto:commons-dev-help@jakarta.apache.org>


Mime
View raw message