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 TestCookieNetscapeDraft.java
Date Sat, 24 Apr 2004 23:28:05 GMT
olegk       2004/04/24 16:28:05

  Modified:    httpclient/src/java/org/apache/commons/httpclient/cookie
                        CookieSpecBase.java
               httpclient/src/test/org/apache/commons/httpclient/cookie
                        TestCookieCompatibilitySpec.java
                        TestCookieNetscapeDraft.java
  Log:
  100% test coverage for browser compatibility & netscape draft cookie specs
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.24      +4 -27     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.23
  retrieving revision 1.24
  diff -u -r1.23 -r1.24
  --- CookieSpecBase.java	18 Apr 2004 23:51:37 -0000	1.23
  +++ CookieSpecBase.java	24 Apr 2004 23:28:04 -0000	1.24
  @@ -505,7 +505,7 @@
        * @return true if the paths match
        */
       private static boolean pathMatch(
  -        final String path, final String topmostPath) {
  +        final String topmostPath, final String path) {
               
           boolean match = path.startsWith (topmostPath);
           
  @@ -537,30 +537,7 @@
           LOG.trace("enter CookieSpecBase.match("
               + "String, int, String, boolean, Cookie[])");
   
  -        if (host == null) {
  -            throw new IllegalArgumentException(
  -                "Host of origin may not be null");
  -        }
  -        if (host.trim().equals("")) {
  -            throw new IllegalArgumentException(
  -                "Host of origin may not be blank");
  -        }
  -        if (port < 0) {
  -            throw new IllegalArgumentException("Invalid port: " + port);
  -        }
  -        if (path == null) {
  -            throw new IllegalArgumentException(
  -                "Path of origin may not be null.");
  -        }
           if (cookies == null) {
  -            throw new IllegalArgumentException("Cookie array may not be null");
  -        }
  -        if (path.trim().equals("")) {
  -            path = PATH_DELIM;
  -        }
  -        host = host.toLowerCase();
  -
  -        if (cookies.length <= 0) {
               return null;
           }
           List matching = new LinkedList();
  
  
  
  1.2       +455 -84   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.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestCookieCompatibilitySpec.java	24 Apr 2004 19:39:24 -0000	1.1
  +++ TestCookieCompatibilitySpec.java	24 Apr 2004 23:28:04 -0000	1.2
  @@ -37,6 +37,7 @@
   import org.apache.commons.httpclient.Header;
   import org.apache.commons.httpclient.HttpException;
   import org.apache.commons.httpclient.HttpState;
  +import org.apache.commons.httpclient.NameValuePair;
   
   
   /**
  @@ -68,6 +69,165 @@
           return new TestSuite(TestCookieCompatibilitySpec.class);
       }
   
  +    public void testParseAttributeInvalidAttrib() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        try {
  +            cookiespec.parseAttribute(null, null);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeInvalidCookie() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        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 CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        cookiespec.parseAttribute(new NameValuePair("path", null), cookie);
  +        assertEquals("/", cookie.getPath());
  +    }
  +
  +    public void testParseAttributeBlankPath() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        cookiespec.parseAttribute(new NameValuePair("path", "   "), cookie);
  +        assertEquals("/", cookie.getPath());
  +    }
  +
  +    public void testParseAttributeNullDomain() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.parseAttribute(new NameValuePair("domain", null), cookie);
  +            fail("MalformedCookieException must have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeBlankDomain() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.parseAttribute(new NameValuePair("domain", "   "), cookie);
  +            fail("MalformedCookieException must have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeNullMaxAge() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.parseAttribute(new NameValuePair("max-age", null), cookie);
  +            fail("MalformedCookieException must have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeInvalidMaxAge() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.parseAttribute(new NameValuePair("max-age", "crap"), cookie);
  +            fail("MalformedCookieException must have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeNullExpires() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.parseAttribute(new NameValuePair("expires", null), cookie);
  +            fail("MalformedCookieException must have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeUnknownValue() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        cookiespec.parseAttribute(new NameValuePair("nonsense", null), cookie);
  +    }
  +    
  +    public void testValidateNullHost() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.validate(null, 80, "/", false, cookie);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testValidateBlankHost() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.validate("   ", 80, "/", false, cookie);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testValidateNullPath() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.validate("host", 80, null, false, cookie);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testValidateBlankPath() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
  +        cookiespec.validate("host", 80, "   ", false, cookie);
  +    }
  +
  +    public void testValidateInvalidPort() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.validate("host", -80, "/", false, cookie);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testValidateInvalidCookieVersion() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        cookie.setVersion(-1);
  +        try {
  +            cookiespec.validate("host", 80, "/", false, cookie);
  +            fail("MalformedCookieException must have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +
  +    /**
  +     * Tests whether domain attribute check is case-insensitive.
  +     */
  +    public void testDomainCaseInsensitivity() throws Exception {
  +        Header header = new Header("Set-Cookie", 
  +            "name=value; path=/; domain=.whatever.com");
  +
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie[] parsed = cookieParse(cookiespec, "www.WhatEver.com", 80, "/", false, header);
  +        assertNotNull(parsed);
  +        assertEquals(1, parsed.length);
  +        assertEquals(".whatever.com", parsed[0].getDomain());
  +    }
  +    
       /**
        * Test basic parse (with various spacings
        */
  @@ -392,6 +552,17 @@
           assertEquals("Value","value2",cookies[0].getValue());
       }
   
  +    public void testParseRelativePath() throws Exception {
  +        Header header = new Header("Set-Cookie", "name1=value1;Path=whatever");
  +
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "whatever", true,
header);
  +        assertEquals("Found 1 cookies.",1,parsed.length);
  +        assertEquals("Name","name1",parsed[0].getName());
  +        assertEquals("Value","value1",parsed[0].getValue());
  +        assertEquals("Path","whatever",parsed[0].getPath());
  +    }
  +
       public void testParseWithWrongDomain() throws Exception {
           Header header = new Header("Set-Cookie",
               "cookie-name=cookie-value; domain=127.0.0.1; version=1");
  @@ -405,7 +576,7 @@
           }
       }
   
  -    public void testParseWithNullDomain() throws Exception {
  +    public void testParseWithNullHost() throws Exception {
           Header header = new Header("Set-Cookie",
               "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
   
  @@ -418,128 +589,116 @@
           }
       }
   
  -    public void testParseWithNullPath() throws Exception {
  +    public void testParseWithBlankHost() throws Exception {
           Header header = new Header("Set-Cookie",
               "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
   
           CookieSpec cookiespec = new CookieSpecBase();
           try {
  -            Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, null, false, header);
  +            Cookie[] parsed = cookieParse(cookiespec, "  ", 80, "/", false, header);
               fail("IllegalArgumentException should have been thrown");
           } catch (IllegalArgumentException e) {
               // expected
           }
       }
   
  -    public void testParseWithNullDomainAndPath() throws Exception {
  +    public void testParseWithNullPath() throws Exception {
           Header header = new Header("Set-Cookie",
               "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
   
           CookieSpec cookiespec = new CookieSpecBase();
           try {
  -            Cookie[] parsed = cookieParse(cookiespec, null, 80, null, false, header);
  +            Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, null, false, header);
               fail("IllegalArgumentException should have been thrown");
           } catch (IllegalArgumentException e) {
               // expected
           }
       }
   
  -    public void testParseWithPathMismatch() throws Exception {
  +    public void testParseWithBlankPath() throws Exception {
           Header header = new Header("Set-Cookie",
  -            "cookie-name=cookie-value; path=/path/path/path");
  +            "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
  +
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "  ", false, header);
  +        assertNotNull(parsed);
  +        assertEquals(1, parsed.length);
  +        assertEquals("/", parsed[0].getPath());
  +    }
  +
  +    public void testParseWithNegativePort() throws Exception {
  +        Header header = new Header("Set-Cookie",
  +            "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
   
           CookieSpec cookiespec = new CookieSpecBase();
           try {
  -            Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path", false,
header);
  -            fail("HttpException should have been thrown.");
  -        } catch (HttpException e) {
  +            Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", -80, null, false, header);
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
               // expected
           }
       }
  -    
  -    public void testParseWithPathMismatch2() throws Exception {
  +
  +    public void testParseWithNullHostAndPath() throws Exception {
           Header header = new Header("Set-Cookie",
  -            "cookie-name=cookie-value; path=/foobar");
  +            "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
   
           CookieSpec cookiespec = new CookieSpecBase();
           try {
  -            Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/foo", false, header);
  -            fail("HttpException should have been thrown.");
  -        } catch (HttpException e) {
  +            Cookie[] parsed = cookieParse(cookiespec, null, 80, null, false, header);
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
               // expected
           }
       }
   
  -
  -    /** Call CookieSpec#match providing null for domain to match on
  -     */
  -    public void testCreateCookieHeaderWithNullDomain() throws Exception {
  +    public void testParseWithPathMismatch() throws Exception {
           Header header = new Header("Set-Cookie",
  -            "cookie-name=cookie-value; Expires=Thu, 01-Jan-1970 00:00:10 GMT");
  +            "cookie-name=cookie-value; path=/path/path/path");
   
           CookieSpec cookiespec = new CookieSpecBase();
  -        Cookie[] parsed = cookieParse(cookiespec, "www.apache.org", 80, "/", true, header);
  -
           try {
  -            Cookie[] matched = cookiespec.match(null, 80, "/", true, parsed);
  -            header = cookiespec.formatCookieHeader(matched);
  -            fail("IllegalArgumentException should have been thrown.");
  -        } catch(IllegalArgumentException e){
  -            // Expected
  +            Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path", false,
header);
  +            fail("MalformedCookieException should have been thrown.");
  +        } catch (MalformedCookieException e) {
  +            // expected
           }
       }
       
  -    /** Call CookieSpec#match providing null for path to match on
  -     */
  -    public void testCreateCookieHeaderWithNullPath() throws Exception{
  +    public void testParseWithPathMismatch2() throws Exception {
           Header header = new Header("Set-Cookie",
  -            "cookie-name=cookie-value; Expires=Thu, 01-Jan-1970 00:00:10 GMT");
  +            "cookie-name=cookie-value; path=/foobar");
   
           CookieSpec cookiespec = new CookieSpecBase();
  -        Cookie[] parsed = cookieParse(cookiespec, "www.apache.org", 80, "/", true, header);
  -
           try {
  -            Cookie[] matched = cookiespec.match("www.apache.org", 80, null, true, parsed);
  -            header = cookiespec.formatCookieHeader(matched);
  -            fail("IllegalArgumentException should have been thrown.");
  -        } catch(IllegalArgumentException e){
  -            // Expected
  +            Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/foo", false, header);
  +            fail("MalformedCookieException should have been thrown.");
  +        } catch (MalformedCookieException e) {
  +            // expected
           }
       }
   
  -    /** Call CookieSpec#match providing null for domain and path to
  -     * match on
  -     */
  -    public void testCreateCookieHeaderWithNullDomainAndPath() throws Exception {
  -        Header header = new Header("Set-Cookie",
  -            "cookie-name=cookie-value; Expires=Thu, 01-Jan-1970 00:00:10 GMT");
   
  +    public void testParseWithInvalidHeader1() throws Exception {
           CookieSpec cookiespec = new CookieSpecBase();
  -        Cookie[] parsed = cookieParse(cookiespec, "www.apache.org", 80, "/", true, header);
  -
           try {
  -            Cookie[] matched = cookiespec.match(null, 80, null, true, parsed);
  -            header = cookiespec.formatCookieHeader(matched);
  +            Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo", false, (Header)null);
               fail("IllegalArgumentException should have been thrown.");
  -        } catch(IllegalArgumentException e){
  -            // Expected
  +        } catch (IllegalArgumentException e) {
  +            // expected
           }
       }
   
  -    /**
  -     * Tests whether domain attribute check is case-insensitive.
  -     */
  -    public void testDomainCaseInsensitivity() throws Exception {
  -        Header header = new Header("Set-Cookie", 
  -            "name=value; path=/; domain=.whatever.com");
  -
  +    public void testParseWithInvalidHeader2() throws Exception {
           CookieSpec cookiespec = new CookieSpecBase();
  -        Cookie[] parsed = cookieParse(cookiespec, "www.WhatEver.com", 80, "/", false, header);
  -        assertNotNull(parsed);
  -        assertEquals(1, parsed.length);
  -        assertEquals(".whatever.com", parsed[0].getDomain());
  +        try {
  +            Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo", false, (String)null);
  +            fail("IllegalArgumentException should have been thrown.");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
       }
  -    
  +
       /**
        * Tests if cookie constructor rejects cookie name containing blanks.
        */
  @@ -570,6 +729,21 @@
       }
   
       /**
  +     * Tests if malformatted expires attribute is parsed correctly.
  +     */
  +    public void testCookieWithComma() throws Exception {
  +        Header header = new Header("Set-Cookie", "name=value; expires=\"Thu, 01-Jan-1970
00:00:00 GMT");
  +
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        try {
  +            Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
  +            fail("MalformedCookieException should have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +    
  +
  +    /**
        * Tests several date formats.
        */
       public void testDateFormats() throws Exception {
  @@ -598,23 +772,176 @@
       }
   
       private void checkDate(String date) throws Exception {
  -        Header header = new Header("Set-Cookie", "custno=12345;Expires='"+date+"'");
  +        Header header = new Header("Set-Cookie", "custno=12345;Expires='"+date+"';");
           CookieSpec cookiespec = new CookieSpecBase();
           cookieParse(cookiespec, "localhost", 80, "/", false, header);
       }
   
       /**
  -     * Makes sure that a cookie matches with a path of the same value.
  +     * Tests if that invalid second domain level cookie gets 
  +     * rejected in the strict mode, but gets accepted in the
  +     * browser compatibility mode.
        */
  -    public void testMatchWithEqualPaths() {
  -        
  -        Cookie cookie = new Cookie(".test.com", "test", "1", "/test", null, false);
  -        
  +    public void testSecondDomainLevelCookie() throws Exception {
  +        Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);

  +        cookie.setDomainAttributeSpecified(true);
  +        cookie.setPathAttributeSpecified(true);
  +
           CookieSpec cookiespec = new CookieSpecBase();
  -        boolean match = cookiespec.match("test.test.com", 80, "/test", false, cookie);
  -        assertTrue("Cookie paths did not match", match);
  +        cookiespec.validate("sourceforge.net", 80, "/", false, cookie);
  +    }
  +
  +    public void testMatchNullHost() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.match(null, 80, "/", false, cookie);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testMatchBlankHost() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.match("   ", 80, "/", false, cookie);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testMatchInvalidPort() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.match("host", -80, "/", false, cookie);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testMatchNullPath() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.match("host", 80, null, false, cookie);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testMatchBlankPath() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
  +        assertTrue(cookiespec.match("host", 80, "  ", false, cookie));
  +    }
  +
  +    public void testMatchNullCookie() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        try {
  +            cookiespec.match("host", 80, "/", false, (Cookie)null);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testMatchNullCookieDomain() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie(null, "name", "value", "/", null, false);
  +        assertFalse(cookiespec.match("host", 80, "/", false, cookie));
       }
   
  +    public void testMatchNullCookiePath() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie("host", "name", "value", null, null, false);
  +        assertFalse(cookiespec.match("host", 80, "/", false, cookie));
  +    }
  +    
  +    public void testCookieMatch1() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
  +        assertTrue(cookiespec.match("host", 80, "/", false, cookie));
  +    }
  +    
  +    public void testCookieMatch2() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie(".whatever.com", "name", "value", "/", null, false);
  +        assertTrue(cookiespec.match(".whatever.com", 80, "/", false, cookie));
  +    }
  +    
  +    public void testCookieMatch3() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie(".whatever.com", "name", "value", "/", null, false);
  +        assertTrue(cookiespec.match(".really.whatever.com", 80, "/", false, cookie));
  +    }
  +    
  +    public void testCookieMatch4() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie("host", "name", "value", "/foobar/r", null, false);
  +        assertTrue(cookiespec.match("host", 80, "/foobar/", false, cookie));
  +    }
  +    
  +    public void testCookieMismatch1() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie("host1", "name", "value", "/", null, false);
  +        assertFalse(cookiespec.match("host2", 80, "/", false, cookie));
  +    }
  +    
  +    public void testCookieMismatch2() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie(".aaaaaaaaa.com", "name", "value", "/", null, false);
  +        assertFalse(cookiespec.match(".bbbbbbbb.com", 80, "/", false, cookie));
  +    }
  +    
  +    public void testCookieMismatch3() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, false);
  +        assertFalse(cookiespec.match("host", 80, "/foo", false, cookie));
  +    }
  +    
  +    public void testCookieMismatch4() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, true);
  +        assertFalse(cookiespec.match("host", 80, "/foobar/", false, cookie));
  +    }
  +    
  +    public void testCookieMismatch5() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, true);
  +        assertFalse(cookiespec.match("host", 80, "/foobar", false, cookie));
  +    }
  +    
  +    public void testMatchNullCookies() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie[] matched = cookiespec.match("host", 80, "/foobar", false, (Cookie[])null);
  +        assertNull(matched);
  +    }
  +    
  +    public void testMatchedCookiesOrder() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie[] cookies = {
  +            new Cookie("host", "nomatch", "value", "/noway", null, false),
  +            new Cookie("host", "name2", "value", "/foobar/yada", null, false),
  +            new Cookie("host", "name3", "value", "/foobar", null, false),
  +            new Cookie("host", "name1", "value", "/foobar/yada/yada", null, false)};
  +        Cookie[] matched = cookiespec.match("host", 80, "/foobar", false, cookies);
  +        assertNotNull(matched);
  +        assertEquals(3, matched.length);
  +        assertEquals("name1", matched[0].getName());
  +        assertEquals("name2", matched[1].getName());
  +        assertEquals("name3", matched[2].getName());
  +    }
  +
  +    public void testFormatInvalidCookie() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        try {
  +            String s = cookiespec.formatCookie(null);
  +            fail("IllegalArgumentException nust have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }    
  +
       /**
        * Tests generic cookie formatting.
        */
  @@ -628,6 +955,16 @@
           assertEquals("name=value", s);
       }    
   
  +    public void testGenericCookieFormattingAsHeader() throws Exception {
  +        Header header = new Header("Set-Cookie", 
  +            "name=value; path=/; domain=.mydomain.com");
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
  +        cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
  +        Header cookieheader = cookiespec.formatCookieHeader(cookies[0]);
  +        assertEquals("name=value", cookieheader.getValue());
  +    }    
  +
       /**
        * Tests if null cookie values are handled correctly.
        */
  @@ -641,19 +978,53 @@
           assertEquals("name=", s);
       }
   
  +    public void testFormatInvalidCookies() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        try {
  +            String s = cookiespec.formatCookies(null);
  +            fail("IllegalArgumentException nust have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }    
  +
  +    public void testFormatZeroCookies() throws Exception {
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        try {
  +            String s = cookiespec.formatCookies(new Cookie[] {});
  +            fail("IllegalArgumentException nust have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }    
  +
       /**
  -     * Tests if that invalid second domain level cookie gets 
  -     * rejected in the strict mode, but gets accepted in the
  -     * browser compatibility mode.
  +     * Tests generic cookie formatting.
        */
  -    public void testSecondDomainLevelCookie() throws Exception {
  -        Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);

  -        cookie.setDomainAttributeSpecified(true);
  -        cookie.setPathAttributeSpecified(true);
  +    public void testFormatSeveralCookies() throws Exception {
  +        Header header = new Header("Set-Cookie", 
  +            "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
  +        String s = cookiespec.formatCookies(cookies);
  +        assertEquals("name1=value1; name2=value2", s);
  +    }    
  +
  +    public void testFormatOneCookie() throws Exception {
  +        Header header = new Header("Set-Cookie", 
  +            "name1=value1; path=/; domain=.mydomain.com;");
  +        CookieSpec cookiespec = new CookieSpecBase();
  +        Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
  +        String s = cookiespec.formatCookies(cookies);
  +        assertEquals("name1=value1", s);
  +    }    
   
  +    public void testFormatSeveralCookiesAsHeader() throws Exception {
  +        Header header = new Header("Set-Cookie", 
  +            "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
           CookieSpec cookiespec = new CookieSpecBase();
  -        cookiespec.validate("sourceforge.net", 80, "/", false, cookie);
  -    }
  +        Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
  +        Header cookieheader = cookiespec.formatCookieHeader(cookies);
  +        assertEquals("name1=value1; name2=value2", cookieheader.getValue());
  +    }    
   
   }
   
  
  
  
  1.2       +142 -4    jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieNetscapeDraft.java
  
  Index: TestCookieNetscapeDraft.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieNetscapeDraft.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- TestCookieNetscapeDraft.java	24 Apr 2004 19:39:24 -0000	1.1
  +++ TestCookieNetscapeDraft.java	24 Apr 2004 23:28:04 -0000	1.2
  @@ -34,6 +34,7 @@
   import org.apache.commons.httpclient.Cookie;
   import org.apache.commons.httpclient.Header;
   import org.apache.commons.httpclient.HttpException;
  +import org.apache.commons.httpclient.NameValuePair;
   
   
   /**
  @@ -57,6 +58,143 @@
   
       public static Test suite() {
           return new TestSuite(TestCookieNetscapeDraft.class);
  +    }
  +
  +    public void testParseAttributeInvalidAttrib() throws Exception {
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        try {
  +            cookiespec.parseAttribute(null, null);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeInvalidCookie() throws Exception {
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        try {
  +            cookiespec.parseAttribute(new NameValuePair("name", "value"), null);
  +            fail("IllegalArgumentException must have been thrown");
  +        } catch (IllegalArgumentException expected) {
  +        }
  +    }
  +
  +    public void testParseAttributeInvalidCookieExpires() throws Exception {
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        Cookie cookie = new Cookie();
  +        try {
  +            cookiespec.parseAttribute(new NameValuePair("expires", null), cookie);
  +            fail("MalformedCookieException must have been thrown");
  +        } catch (MalformedCookieException expected) {
  +        }
  +    }
  +
  +    public void testParseWithNullHost() throws Exception {
  +        Header header = new Header("Set-Cookie",
  +            "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
  +
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        try {
  +            Cookie[] parsed = cookieParse(cookiespec, null, 80, "/", false, header);
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +    }
  +
  +    public void testParseWithBlankHost() throws Exception {
  +        Header header = new Header("Set-Cookie",
  +            "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
  +
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        try {
  +            Cookie[] parsed = cookieParse(cookiespec, "  ", 80, "/", false, header);
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +    }
  +
  +    public void testParseWithNullPath() throws Exception {
  +        Header header = new Header("Set-Cookie",
  +            "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
  +
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        try {
  +            Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, null, false, header);
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +    }
  +
  +    public void testParseWithBlankPath() throws Exception {
  +        Header header = new Header("Set-Cookie",
  +            "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
  +
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "  ", false, header);
  +        assertNotNull(parsed);
  +        assertEquals(1, parsed.length);
  +        assertEquals("/", parsed[0].getPath());
  +    }
  +
  +    public void testParseWithNegativePort() throws Exception {
  +        Header header = new Header("Set-Cookie",
  +            "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
  +
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        try {
  +            Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", -80, null, false, header);
  +            fail("IllegalArgumentException should have been thrown");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +    }
  +
  +    public void testParseWithInvalidHeader1() throws Exception {
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        try {
  +            Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo", false, (String)null);
  +            fail("IllegalArgumentException should have been thrown.");
  +        } catch (IllegalArgumentException e) {
  +            // expected
  +        }
  +    }
  +
  +    public void testParseAbsPath() throws Exception {
  +        Header header = new Header("Set-Cookie", "name1=value1;Path=/path/");
  +
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        Cookie[] parsed = cookieParse(cookiespec, "host", 80, "/path/", true, header);
  +        assertEquals("Found 1 cookies.",1,parsed.length);
  +        assertEquals("Name","name1",parsed[0].getName());
  +        assertEquals("Value","value1",parsed[0].getValue());
  +        assertEquals("Domain","host",parsed[0].getDomain());
  +        assertEquals("Path","/path/",parsed[0].getPath());
  +    }
  +
  +    public void testParseAbsPath2() throws Exception {
  +        Header header = new Header("Set-Cookie", "name1=value1;Path=/");
  +
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        Cookie[] parsed = cookieParse(cookiespec, "host", 80, "/", true, header);
  +        assertEquals("Found 1 cookies.",1,parsed.length);
  +        assertEquals("Name","name1",parsed[0].getName());
  +        assertEquals("Value","value1",parsed[0].getValue());
  +        assertEquals("Domain","host",parsed[0].getDomain());
  +        assertEquals("Path","/",parsed[0].getPath());
  +    }
  +
  +    public void testParseRelativePath() throws Exception {
  +        Header header = new Header("Set-Cookie", "name1=value1;Path=whatever");
  +
  +        CookieSpec cookiespec = new NetscapeDraftSpec();
  +        Cookie[] parsed = cookieParse(cookiespec, "host", 80, "whatever", true, header);
  +        assertEquals("Found 1 cookies.",1,parsed.length);
  +        assertEquals("Name","name1",parsed[0].getName());
  +        assertEquals("Value","value1",parsed[0].getValue());
  +        assertEquals("Domain","host",parsed[0].getDomain());
  +        assertEquals("Path","whatever",parsed[0].getPath());
       }
   
       public void testParseWithIllegalNetscapeDomain1() throws Exception {
  
  
  

---------------------------------------------------------------------
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