commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ol...@apache.org
Subject cvs commit: jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie TestCookieCompatibilitySpec.java TestCookieIgnoreSpec.java TestCookieRFC2109Spec.java
Date Sun, 25 Apr 2004 11:57:39 GMT
olegk       2004/04/25 04:57:39

  Modified:    httpclient/src/java/org/apache/commons/httpclient/cookie
                        RFC2109Spec.java
               httpclient/src/test/org/apache/commons/httpclient/cookie
                        TestCookieCompatibilitySpec.java
                        TestCookieIgnoreSpec.java
                        TestCookieRFC2109Spec.java
  Log:
  100% test coverage for the RFC2109 cookie spec
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.18      +15 -20    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.17
  retrieving revision 1.18
  diff -u -r1.17 -r1.18
  --- RFC2109Spec.java	18 Apr 2004 23:51:37 -0000	1.17
  +++ RFC2109Spec.java	25 Apr 2004 11:57:39 -0000	1.18
  @@ -146,20 +146,18 @@
                       + "\" violates RFC 2109: domain must contain an embedded dot");
               }
               host = host.toLowerCase();
  -            if (host.indexOf('.') >= 0) {
  -                if (!host.endsWith(cookie.getDomain())) {
  -                    throw new MalformedCookieException(
  -                        "Illegal domain attribute \"" + cookie.getDomain() 
  -                        + "\". Domain of origin: \"" + host + "\"");
  -                }
  -                // host minus domain may not contain any dots
  -                String hostWithoutDomain = host.substring(0, host.length() 
  -                    - cookie.getDomain().length());
  -                if (hostWithoutDomain.indexOf('.') != -1) {
  -                    throw new MalformedCookieException("Domain attribute \"" 
  -                        + cookie.getDomain() 
  -                        + "\" violates RFC 2109: host minus domain may not contain any
dots");
  -                }
  +            if (!host.endsWith(cookie.getDomain())) {
  +                throw new MalformedCookieException(
  +                    "Illegal domain attribute \"" + cookie.getDomain() 
  +                    + "\". Domain of origin: \"" + host + "\"");
  +            }
  +            // host minus domain may not contain any dots
  +            String hostWithoutDomain = host.substring(0, host.length() 
  +                - cookie.getDomain().length());
  +            if (hostWithoutDomain.indexOf('.') != -1) {
  +                throw new MalformedCookieException("Domain attribute \"" 
  +                    + cookie.getDomain() 
  +                    + "\" violates RFC 2109: host minus domain may not contain any dots");
               }
           }
       }
  @@ -205,9 +203,6 @@
        */
       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(formatNameValuePair(cookie.getName(), 
               cookie.getValue(), version));
  
  
  
  1.4       +10 -4     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieCompatibilitySpec.java
  
  Index: TestCookieCompatibilitySpec.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieCompatibilitySpec.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- TestCookieCompatibilitySpec.java	24 Apr 2004 23:51:57 -0000	1.3
  +++ TestCookieCompatibilitySpec.java	25 Apr 2004 11:57:39 -0000	1.4
  @@ -1032,5 +1032,11 @@
           assertEquals("name1=value1; name2=value2", cookieheader.getValue());
       }    
   
  +    public void testKeepCloverHappy() throws Exception {
  +        MalformedCookieException ex1 = new MalformedCookieException(); 
  +        MalformedCookieException ex2 = new MalformedCookieException("whatever"); 
  +        MalformedCookieException ex3 = new MalformedCookieException("whatever", null);

  +    }
  +
   }
   
  
  
  
  1.2       +19 -5     jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieIgnoreSpec.java
  
  Index: TestCookieIgnoreSpec.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieIgnoreSpec.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestCookieIgnoreSpec.java	24 Apr 2004 19:39:24 -0000	1.1
  +++ TestCookieIgnoreSpec.java	25 Apr 2004 11:57:39 -0000	1.2
  @@ -33,6 +33,7 @@
   import junit.framework.Test;
   import junit.framework.TestSuite;
   
  +import org.apache.commons.httpclient.Cookie;
   import org.apache.commons.httpclient.Header;
   import org.apache.commons.httpclient.HttpClientTestBase;
   import org.apache.commons.httpclient.methods.GetMethod;
  @@ -80,7 +81,7 @@
           }
       }
   
  -    public void testCustomAuthorizationHeader() throws Exception {
  +    public void testIgnoreCookies() throws Exception {
           this.server.setHttpService(new BasicAuthService());
   
           GetMethod httpget = new GetMethod("/");
  @@ -95,5 +96,18 @@
                   0, this.client.getState().getCookies().length);
       }
   
  +    public void testKeepCloverHappy() throws Exception {
  +        CookieSpec cookiespec = new IgnoreCookiesSpec();
  +        cookiespec.parseAttribute(null, null);
  +        cookiespec.parse("host", 80, "/", false, (String)null);
  +        cookiespec.parse("host", 80, "/", false, (Header)null);
  +        cookiespec.validate("host", 80, "/", false, (Cookie)null);
  +        cookiespec.match("host", 80, "/", false, (Cookie)null);
  +        cookiespec.match("host", 80, "/", false, (Cookie [])null);
  +        cookiespec.formatCookie(null);
  +        cookiespec.formatCookies(null);
  +        cookiespec.formatCookieHeader((Cookie)null);
  +        cookiespec.formatCookieHeader((Cookie [])null);
  +    }
   }
   
  
  
  
  1.2       +187 -44   jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2109Spec.java
  
  Index: TestCookieRFC2109Spec.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2109Spec.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestCookieRFC2109Spec.java	24 Apr 2004 19:39:24 -0000	1.1
  +++ TestCookieRFC2109Spec.java	25 Apr 2004 11:57:39 -0000	1.2
  @@ -33,6 +33,7 @@
   
   import org.apache.commons.httpclient.Cookie;
   import org.apache.commons.httpclient.Header;
  +import org.apache.commons.httpclient.NameValuePair;
   
   /**
    * Test cases for RFC2109 cookie spec
  @@ -56,6 +57,64 @@
           return new TestSuite(TestCookieRFC2109Spec.class);
       }
   
  +    public void testParseAttributeInvalidAttrib() throws Exception {
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        try {
  +            cookiespec.parseAttribute(null, null);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeInvalidCookie() throws Exception {
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        try {
  +            cookiespec.parseAttribute(new NameValuePair("name", "value"), null);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeNullPath() throws Exception {
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        try {
  +            Cookie cookie = new Cookie();
  +            cookiespec.parseAttribute(new NameValuePair("path", null), cookie);
  +            fail("MalformedCookieException must have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeBlankPath() throws Exception {
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        try {
  +            Cookie cookie = new Cookie();
  +            cookiespec.parseAttribute(new NameValuePair("path", "   "), cookie);
  +            fail("MalformedCookieException must have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeNullVersion() throws Exception {
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        try {
  +            Cookie cookie = new Cookie();
  +            cookiespec.parseAttribute(new NameValuePair("version", null), cookie);
  +            fail("MalformedCookieException must have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeInvalidVersion() throws Exception {
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        try {
  +            Cookie cookie = new Cookie();
  +            cookiespec.parseAttribute(new NameValuePair("version", "nonsense"), cookie);
  +            fail("MalformedCookieException must have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +
       public void testParseVersion() throws Exception {
           Header header = new Header("Set-Cookie","cookie-name=cookie-value; version=1");
   
  @@ -67,39 +126,55 @@
           assertEquals("Version",1,parsed[0].getVersion());
       }
   
  -    public void testParseWithWrongDomain2() throws Exception {
  +    /**
  +     * Test domain equals host 
  +     */
  +    public void testParseDomainEqualsHost() throws Exception {
           Header header = new Header("Set-Cookie",
  -            "cookie-name=cookie-value; domain=.c.com; version=1");
  +            "cookie-name=cookie-value; domain=www.b.com; version=1");
  +
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        Cookie[] parsed = cookieParse(cookiespec, "www.b.com", 80, "/", false, header);
  +        assertNotNull(parsed);
  +        assertEquals(1, parsed.length);
  +        assertEquals("www.b.com", parsed[0].getDomain());
  +    }
  +
  +    /**
  +     * Domain does not start with a dot
  +     */
  +    public void testParseWithIllegalDomain1() throws Exception {
  +        Header header = new Header("Set-Cookie",
  +            "cookie-name=cookie-value; domain=a.b.com; version=1");
   
           CookieSpec cookiespec = new RFC2109Spec();
           try {
  -            Cookie[] parsed = cookieParse(cookiespec, "a.b.c.com", 80, "/", false, header);
  -            fail("HttpException exception should have been thrown");
  +            Cookie[] parsed = cookieParse(cookiespec, "www.a.b.com", 80, "/", false, header);
  +            fail("MalformedCookieException should have been thrown");
           } catch (MalformedCookieException e) {
               // expected
           }
       }
   
       /**
  -     * Domain has no embedded dots
  +     * Domain must have alt least one embedded dot
        */
  -    public void testParseWithIllegalDomain() throws Exception {
  +    public void testParseWithIllegalDomain2() throws Exception {
           Header header = new Header("Set-Cookie",
               "cookie-name=cookie-value; domain=.com; version=1");
   
           CookieSpec cookiespec = new RFC2109Spec();
           try {
               Cookie[] parsed = cookieParse(cookiespec, "b.com", 80, "/", false, header);
  -            fail("HttpException exception should have been thrown");
  +            fail("MalformedCookieException should have been thrown");
           } catch (MalformedCookieException e) {
               // expected
           }
       }
  -
       /**
  -     * Domain has no embedded dots again
  +     * Domain must have alt least one embedded dot
        */
  -    public void testParseWithIllegalDomain2() throws Exception {
  +    public void testParseWithIllegalDomain3() throws Exception {
           Header header = new Header("Set-Cookie",
               "cookie-name=cookie-value; domain=.com.; version=1");
   
  @@ -112,6 +187,41 @@
           }
       }
   
  +    /**
  +     * Host minus domain may not contain any dots
  +     */
  +    public void testParseWithIllegalDomain4() throws Exception {
  +        Header header = new Header("Set-Cookie",
  +            "cookie-name=cookie-value; domain=.c.com; version=1");
  +
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        try {
  +            Cookie[] parsed = cookieParse(cookiespec, "a.b.c.com", 80, "/", false, header);
  +            fail("MalformedCookieException should have been thrown");
  +        } catch (MalformedCookieException e) {
  +            // expected
  +        }
  +    }
  +
  +    /**
  +     * Tests if that invalid second domain level cookie gets 
  +     * rejected in the strict mode, but gets accepted in the
  +     * browser compatibility mode.
  +     */
  +    public void testSecondDomainLevelCookie() throws Exception {
  +        Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);

  +        cookie.setDomainAttributeSpecified(true);
  +        cookie.setPathAttributeSpecified(true);
  +
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        try {
  +            cookiespec.validate("sourceforge.net", 80, "/", false, cookie);
  +            fail("MalformedCookieException should have been thrown");
  +        } catch (MalformedCookieException e) {
  +            // Expected
  +        }
  +    }    
  +
       public void testParseWithWrongPath() throws Exception {
           Header header = new Header("Set-Cookie",
               "cookie-name=cookie-value; domain=127.0.0.1; path=/not/just/root");
  @@ -129,7 +239,6 @@
        * Tests if default cookie validator rejects cookies originating from a host without
domain
        * where domain attribute does not match the host of origin 
        */
  -    
       public void testInvalidDomainWithSimpleHostName() throws Exception {    
           CookieSpec cookiespec = new RFC2109Spec();
           Header header = new Header("Set-Cookie", 
  @@ -153,9 +262,32 @@
       }
   
       /**
  +     * Tests if cookie values with embedded comma are handled correctly.
  +     */
  +    public void testCookieWithComma() throws Exception {
  +        Header header = new Header("Set-Cookie", "a=b,c");
  +
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
  +        assertEquals("number of cookies", 2, cookies.length);
  +        assertEquals("a", cookies[0].getName());
  +        assertEquals("b", cookies[0].getValue());
  +        assertEquals("c", cookies[1].getName());
  +        assertEquals(null, cookies[1].getValue());
  +    }
  +
  +    public void testFormatInvalidCookies() throws Exception {
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        try {
  +            String s = cookiespec.formatCookie(null);
  +            fail("IllegalArgumentException nust have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }    
  +
  +    /**
        * Tests RFC 2109 compiant cookie formatting.
        */
  -    
       public void testRFC2109CookieFormatting() throws Exception {
           CookieSpec cookiespec = new RFC2109Spec();
           Header header = new Header("Set-Cookie", 
  @@ -173,6 +305,31 @@
           assertEquals(s2, "$Version=0; name=value; $Domain=.mydomain.com; $Path=/");
       }
   
  +    public void testRFC2109CookiesFormatting() throws Exception {
  +        CookieSpec cookiespec = new RFC2109Spec();
  +        Header header = new Header("Set-Cookie", 
  +            "name1=value1; path=/; domain=.mydomain.com, " + 
  +            "name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
  +        Cookie[] cookies = cookieParse(cookiespec, "myhost.mydomain.com", 80, "/", false,
header);
  +        assertNotNull(cookies);
  +        assertEquals(2, cookies.length);
  +        String s1 = cookiespec.formatCookies(cookies);
  +        assertEquals(s1, 
  +            "$Version=0; name1=value1; $Domain=.mydomain.com; $Path=/; " + 
  +            "name2=value2; $Domain=.mydomain.com; $Path=/");
  +
  +        header = new Header("Set-Cookie", 
  +            "name1=value1; version=1; path=/; domain=.mydomain.com, " + 
  +            "name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
  +        cookies = cookieParse(cookiespec, "myhost.mydomain.com", 80, "/", false, header);
  +        assertNotNull(cookies);
  +        assertEquals(2, cookies.length);
  +        String s2 = cookiespec.formatCookies(cookies);
  +        assertEquals(s2, 
  +            "$Version=\"1\"; name1=\"value1\"; $Domain=\".mydomain.com\"; $Path=\"/\";
" + 
  +            "name2=\"value2\"; $Domain=\".mydomain.com\"; $Path=\"/\"");
  +    }
  +    
       /**
        * Tests if null cookie values are handled correctly.
        */
  @@ -184,40 +341,26 @@
           CookieSpec cookiespec = new RFC2109Spec();
           String s = cookiespec.formatCookie(cookie);
           assertEquals("$Version=0; name=; $Domain=.whatever.com; $Path=/", s);
  -    }
  -
  -    /**
  -     * Tests if cookie values with embedded comma are handled correctly.
  -     */
  -    public void testCookieWithComma() throws Exception {
  -        Header header = new Header("Set-Cookie", "a=b,c");
   
  -        CookieSpec cookiespec = new RFC2109Spec();
  -        Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
  -        assertEquals("number of cookies", 2, cookies.length);
  -        assertEquals("a", cookies[0].getName());
  -        assertEquals("b", cookies[0].getValue());
  -        assertEquals("c", cookies[1].getName());
  -        assertEquals(null, cookies[1].getValue());
  +        cookie.setVersion(1);
  +        s = cookiespec.formatCookie(cookie);
  +        assertEquals("$Version=\"1\"; name=\"\"; $Domain=\".whatever.com\"; $Path=\"/\"",
s);
       }
   
  -    /**
  -     * Tests if that invalid second domain level cookie gets 
  -     * rejected in the strict mode, but gets accepted in the
  -     * browser compatibility mode.
  -     */
  -    public void testSecondDomainLevelCookie() throws Exception {
  -        Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);

  +    public void testCookieNullDomainNullPathFormatting() {
  +        Cookie cookie = new Cookie(null, "name", null, "/", null, false); 
           cookie.setDomainAttributeSpecified(true);
           cookie.setPathAttributeSpecified(true);
   
           CookieSpec cookiespec = new RFC2109Spec();
  -        try {
  -            cookiespec.validate("sourceforge.net", 80, "/", false, cookie);
  -            fail("MalformedCookieException should have been thrown");
  -        } catch (MalformedCookieException e) {
  -            // Expected
  -        }
  -    }    
  +        String s = cookiespec.formatCookie(cookie);
  +        assertEquals("$Version=0; name=; $Path=/", s);
  +
  +        cookie.setDomainAttributeSpecified(false);
  +        cookie.setPathAttributeSpecified(false);
  +        s = cookiespec.formatCookie(cookie);
  +        assertEquals("$Version=0; name=", s);
  +    }
  +
   }
   
  
  
  

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


Mime
View raw message