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 TestCookie.java TestCookieAll.java TestCookieBase.java TestCookieCompatibilitySpec.java TestCookieIgnoreSpec.java TestCookieNetscapeDraft.java TestCookieRFC2109Spec.java
Date Sat, 24 Apr 2004 19:39:24 GMT
olegk       2004/04/24 12:39:24

  Modified:    httpclient/src/test/org/apache/commons/httpclient
                        TestNoHost.java TestNoHostBase.java
  Added:       httpclient/src/test/org/apache/commons/httpclient/cookie
                        TestCookie.java TestCookieAll.java
                        TestCookieBase.java
                        TestCookieCompatibilitySpec.java
                        TestCookieIgnoreSpec.java
                        TestCookieNetscapeDraft.java
                        TestCookieRFC2109Spec.java
  Removed:     httpclient/src/test/org/apache/commons/httpclient
                        TestCookie.java
  Log:
  Refactoring of cookie management related test cases (initial commit)
  
  Contributed by Oleg Kalnichevski
  
  Revision  Changes    Path
  1.35      +6 -5      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java
  
  Index: TestNoHost.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHost.java,v
  retrieving revision 1.34
  retrieving revision 1.35
  diff -u -r1.34 -r1.35
  --- TestNoHost.java	12 Apr 2004 10:30:46 -0000	1.34
  +++ TestNoHost.java	24 Apr 2004 19:39:24 -0000	1.35
  @@ -37,6 +37,7 @@
   import org.apache.commons.httpclient.auth.TestBasicAuth;
   import org.apache.commons.httpclient.auth.TestChallengeParser;
   import org.apache.commons.httpclient.auth.TestChallengeProcessor;
  +import org.apache.commons.httpclient.cookie.TestCookieAll;
   
   /**
    * Tests that don't require any external host.
  @@ -57,7 +58,7 @@
       public static Test suite() {
           TestSuite suite = new TestSuite();
           suite.addTest(TestHttpStatus.suite());
  -        suite.addTest(TestCookie.suite());
  +        suite.addTest(TestCookieAll.suite());
           suite.addTest(TestNVP.suite());
           suite.addTest(TestHeader.suite());
           suite.addTest(TestParameterParser.suite());
  
  
  
  1.3       +3 -5      jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHostBase.java
  
  Index: TestNoHostBase.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestNoHostBase.java,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -r1.2 -r1.3
  --- TestNoHostBase.java	22 Feb 2004 18:08:49 -0000	1.2
  +++ TestNoHostBase.java	24 Apr 2004 19:39:24 -0000	1.3
  @@ -25,8 +25,6 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    *
  - * [Additional notices, if required by prior licensing conditions]
  - *
    */
    package org.apache.commons.httpclient;
   
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookie.java
  
  Index: TestCookie.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookie.java,v 1.1 2004/04/24 19:39:24 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/04/24 19:39:24 $
   * ====================================================================
   *
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.httpclient.cookie;
  
  import java.util.Iterator;
  import java.util.SortedSet;
  import java.util.TreeSet;
  import java.util.Vector;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.httpclient.Cookie;
  import org.apache.commons.httpclient.Header;
  
  
  /**
   * Test cases for Cookie
   *
   * @author BC Holmes
   * @author Rod Waldhoff
   * @author dIon Gillard
   * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
   * @author Marc A. Saegesser
   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
   * @version $Revision: 1.1 $
   */
  public class TestCookie extends TestCookieBase {
  
  
      // -------------------------------------------------------------- Constants
  
      private static final String DOMAIN_NAME = "www.apache.org";
      private static final String TEST_COOKIE = "cookie-name=cookie-value";
      private static final String OLD_EXPIRY = "Expires=Thu, 01-Jan-1970 00:00:10 GMT";
      private static final String SEP = ";";
      private static final String ROOT_PATH = "/";
      private static final int DEFAULT_PORT = 80;
  
      private String[] testName = { "custno", "name", "name" };
      private String[] testValue = { "12345", "John", "Doe, John" };
      private String[] testDomain = { "www.apache.org", ".apache.org",
          ".apache.org" };
  
      // ------------------------------------------------------------ Constructor
  
  
      public TestCookie(String name) {
          super(name);
      }
  
  
      // ------------------------------------------------------- TestCase Methods
  
  
      public static Test suite() {
          return new TestSuite(TestCookie.class);
      }
  
      /**
       * Tests default constructor.
       */
      public void testDefaultConstuctor() {
          Cookie dummy = new Cookie();
          assertEquals( "noname=", dummy.toExternalForm() );
      }
  
      public void testComparator() throws Exception {
          Header setCookie = null;
          Cookie[] parsed = null;
          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");
          CookieSpec cookiespec = new CookieSpecBase();
          parsed = cookieParse(cookiespec, ".apache.org", 80, "/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 = cookieParse(cookiespec, ".apache.org", 80, "/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 = cookieParse(cookiespec, ".baz.org", 80, "/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 = cookieParse(cookiespec, ".baz.org", 80, "/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 = cookieParse(cookiespec, ".baz.com", 80, "/commons/httpclient", true, setCookie);
          cookies.add(parsed[0]);
          // The order should be:
          // 1, 0, 3, 2, 4
          parsed = (Cookie[])cookies.toArray(new Cookie[0]);
          SortedSet set = new TreeSet(parsed[0]);
          int pass = 0;
          for (Iterator itr = set.iterator(); itr.hasNext();) {
              Cookie cookie = (Cookie)itr.next();
              switch (pass) {
                  case 0:
                      assertTrue("0th cookie should be cookie[1]", cookie == parsed[1]);
                      break;
                  case 1:
                      assertTrue("1st cookie should be cookie[0]", cookie == parsed[0]);
                      break;
                  case 2:
                      assertTrue("2nd cookie should be cookie[3]", cookie == parsed[3]);
                      break;
                  case 3:
                      assertTrue("3rd cookie should be cookie[2]", cookie == parsed[2]);
                      break;
                  case 4:
                      assertTrue("4th cookie should be cookie[4]", cookie == parsed[4]);
                      break;
                  default:
                      fail("This should never happen.");
              }
              pass++;
          }
          try {
              parsed[0].compare("foo", "bar");
              fail("Should have thrown an exception trying to compare non-cookies");
          }
          catch (ClassCastException ex) {
              // expected
          }
      }
      /*
      public void testIgnoreCookies() throws Exception {
          String headers = 
              "HTTP/1.1 401 OK\r\n" +
              "Connection: close\r\n" +
              "Content-Length: 0\r\n" +
              "Set-Cookie: custno = 12345; comment=test; version=1," +
              " name=John; version=1; max-age=600; secure; domain=.apache.org";
          
          conn.addResponse(headers);
          
          GetMethod get = new GetMethod("/");
          get.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
          client.executeMethod(get);
          
          assertEquals("Cookie parsing should have been disabled", 
                  0, client.getState().getCookies().length);
      }
      */
  }
  
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieAll.java
  
  Index: TestCookieAll.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieAll.java,v 1.1 2004/04/24 19:39:24 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/04/24 19:39:24 $
   * ====================================================================
   *
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   */
  
  package org.apache.commons.httpclient.cookie;
  
  import junit.framework.*;
  
  /**
   * @author oleg Kalnichevski 
   * 
   * @version $Id: TestCookieAll.java,v 1.1 2004/04/24 19:39:24 olegk Exp $
   */
  public class TestCookieAll extends TestCase {
  
      public TestCookieAll(String testName) {
          super(testName);
      }
  
      public static Test suite() {
          TestSuite suite = new TestSuite();
          suite.addTest(TestCookie.suite());
          suite.addTest(TestCookieCompatibilitySpec.suite());
          suite.addTest(TestCookieRFC2109Spec.suite());
          suite.addTest(TestCookieNetscapeDraft.suite());
          suite.addTest(TestCookieIgnoreSpec.suite());
          return suite;
      }
  
      public static void main(String args[]) {
          String[] testCaseName = { TestCookieAll.class.getName() };
          junit.textui.TestRunner.main(testCaseName);
      }
  
  }
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieBase.java
  
  Index: TestCookieBase.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieBase.java,v 1.1 2004/04/24 19:39:24 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/04/24 19:39:24 $
   * ====================================================================
   *
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.httpclient.cookie;
  
  import junit.framework.TestCase;
  
  import org.apache.commons.httpclient.Cookie;
  import org.apache.commons.httpclient.Header;
  
  /**
   * Test cases for Cookie
   *
   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
   * @version $Revision: 1.1 $
   */
  public class TestCookieBase extends TestCase {
  
      // ------------------------------------------------------------ Constructor
  
      public TestCookieBase(String name) {
          super(name);
      }
  
      public static Cookie[] cookieParse(
          final CookieSpec parser,
          String host,
          int port,
          String path,
          boolean isSecure,
          Header setHeader)
          throws MalformedCookieException {
          Cookie[] cookies = parser.parse(host, port, path, isSecure, setHeader);
          if (cookies != null) {
              for (int i = 0; i < cookies.length; i++) {
                  parser.validate(host, port, path, isSecure, cookies[i]);
              }
          }
          return cookies;
      }
  }
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieCompatibilitySpec.java
  
  Index: TestCookieCompatibilitySpec.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieCompatibilitySpec.java,v 1.1 2004/04/24 19:39:24 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/04/24 19:39:24 $
   * ====================================================================
   *
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.httpclient.cookie;
  
  import java.util.Date;
  
  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.HttpException;
  import org.apache.commons.httpclient.HttpState;
  
  
  /**
   * Test cases for Cookie
   *
   * @author BC Holmes
   * @author Rod Waldhoff
   * @author dIon Gillard
   * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
   * @author Marc A. Saegesser
   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
   * @version $Revision: 1.1 $
   */
  public class TestCookieCompatibilitySpec extends TestCookieBase {
  
  
      // ------------------------------------------------------------ Constructor
  
  
      public TestCookieCompatibilitySpec(String name) {
          super(name);
      }
  
  
      // ------------------------------------------------------- TestCase Methods
  
  
      public static Test suite() {
          return new TestSuite(TestCookieCompatibilitySpec.class);
      }
  
      /**
       * Test basic parse (with various spacings
       */
      public void testParse1() throws Exception {
          String headerValue = "custno = 12345; comment=test; version=1," +
              " name=John; version=1; max-age=600; secure; domain=.apache.org";
  
          Header header = new Header("set-cookie", headerValue);
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
          assertEquals(2, cookies.length);
  
          assertEquals("custno", cookies[0].getName());
          assertEquals("12345", cookies[0].getValue());
          assertEquals("test", cookies[0].getComment());
          assertEquals(0, cookies[0].getVersion());
          assertEquals("www.apache.org", cookies[0].getDomain());
          assertEquals("/", cookies[0].getPath());
          assertFalse(cookies[0].getSecure());
  
          assertEquals("name", cookies[1].getName());
          assertEquals("John", cookies[1].getValue());
          assertEquals(null, cookies[1].getComment());
          assertEquals(0, cookies[1].getVersion());
          assertEquals(".apache.org", cookies[1].getDomain());
          assertEquals("/", cookies[1].getPath());
          assertTrue(cookies[1].getSecure());
      }
  
  
      /**
       * Test no spaces
       */
      public void testParse2() throws Exception {
          String headerValue = "custno=12345;comment=test; version=1," +
              "name=John;version=1;max-age=600;secure;domain=.apache.org";
  
          Header header = new Header("set-cookie", headerValue);
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
  
          assertEquals(2, cookies.length);
  
          assertEquals("custno", cookies[0].getName());
          assertEquals("12345", cookies[0].getValue());
          assertEquals("test", cookies[0].getComment());
          assertEquals(0, cookies[0].getVersion());
          assertEquals("www.apache.org", cookies[0].getDomain());
          assertEquals("/", cookies[0].getPath());
          assertFalse(cookies[0].getSecure());
  
          assertEquals("name", cookies[1].getName());
          assertEquals("John", cookies[1].getValue());
          assertEquals(null, cookies[1].getComment());
          assertEquals(0, cookies[1].getVersion());
          assertEquals(".apache.org", cookies[1].getDomain());
          assertEquals("/", cookies[1].getPath());
          assertTrue(cookies[1].getSecure());
      }
  
  
      /**
       * Test parse with quoted text
       */
      public void testParse3() throws Exception {
          String headerValue =
              "name=\"Doe, John\";version=1;max-age=600;secure;domain=.apache.org";
          Header header = new Header("set-cookie", headerValue);
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
  
          assertEquals(1, cookies.length);
  
          assertEquals("name", cookies[0].getName());
          assertEquals("Doe, John", cookies[0].getValue());
          assertEquals(null, cookies[0].getComment());
          assertEquals(0, cookies[0].getVersion());
          assertEquals(".apache.org", cookies[0].getDomain());
          assertEquals("/", cookies[0].getPath());
          assertTrue(cookies[0].getSecure());
      }
  
  
      // 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'";
  
          Header header = new Header("set-cookie", headerValue);
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", true, header);
          assertNotNull("Expected some cookies",cookies);
          assertEquals("Expected 1 cookie",1,cookies.length);
          assertNotNull("Expected cookie to have getExpiryDate",cookies[0].getExpiryDate());
      }
  
      public void testSecurityError() throws Exception {
          String headerValue = "custno=12345;comment=test; version=1," +
              "name=John;version=1;max-age=600;secure;domain=jakarta.apache.org";
          Header header = new Header("set-cookie", headerValue);
  
          CookieSpec cookiespec = new CookieSpecBase();
          try {
              Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
              fail("HttpException exception should have been thrown");
          } catch (HttpException e) {
              // expected
          }
      }
  
      public void testParseSimple() throws Exception {
          Header header = new Header("Set-Cookie","cookie-name=cookie-value");
          
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path/path", false, header);
          assertEquals("Found 1 cookie.",1,parsed.length);
          assertEquals("Name","cookie-name",parsed[0].getName());
          assertEquals("Value","cookie-value",parsed[0].getValue());
          assertTrue("Comment",null == parsed[0].getComment());
          assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
          //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded());
          assertTrue("isPersistent",!parsed[0].isPersistent());
          assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
          assertEquals("Path","/path",parsed[0].getPath());
          assertTrue("Secure",!parsed[0].getSecure());
          assertEquals("Version",0,parsed[0].getVersion());
      }
   
      public void testParseSimple2() throws Exception {
          Header header = new Header("Set-Cookie", "cookie-name=cookie-value");
      
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path", false, header);
          assertEquals("Found 1 cookie.", 1, parsed.length);
          assertEquals("Name", "cookie-name", parsed[0].getName());
          assertEquals("Value", "cookie-value", parsed[0].getValue());
          assertTrue("Comment", null == parsed[0].getComment());
          assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
          //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded());
          assertTrue("isPersistent", !parsed[0].isPersistent());
          assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
          assertEquals("Path", "/", parsed[0].getPath());
          assertTrue("Secure", !parsed[0].getSecure());
          assertEquals("Version", 0, parsed[0].getVersion());
      }
   
   
      public void testParseNoValue() throws Exception {
          Header header = new Header("Set-Cookie","cookie-name=");
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
          assertEquals("Found 1 cookie.",1,parsed.length);
          assertEquals("Name","cookie-name",parsed[0].getName());
          assertTrue("Value",null == parsed[0].getValue());
          assertTrue("Comment",null == parsed[0].getComment());
          assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
          //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded());
          assertTrue("isPersistent",!parsed[0].isPersistent());
          assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
          assertEquals("Path","/",parsed[0].getPath());
          assertTrue("Secure",!parsed[0].getSecure());
          assertEquals("Version",0,parsed[0].getVersion());
      }
  
      public void testParseWithWhiteSpace() throws Exception {
          Header header = new Header("Set-Cookie"," cookie-name  =    cookie-value  ");
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
          assertEquals("Found 1 cookie.",1,parsed.length);
          assertEquals("Name","cookie-name",parsed[0].getName());
          assertEquals("Value","cookie-value",parsed[0].getValue());
          assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
          assertEquals("Path","/",parsed[0].getPath());
          assertTrue("Secure",!parsed[0].getSecure());
          assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
          assertTrue("Comment",null == parsed[0].getComment());
      }
  
      public void testParseWithQuotes() throws Exception {
          Header header = new Header("Set-Cookie"," cookie-name  =  \" cookie-value \" ;path=/");
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1",80, "/", false, header);
          assertEquals("Found 1 cookie.",1,parsed.length);
          assertEquals("Name","cookie-name",parsed[0].getName());
          assertEquals("Value"," cookie-value ",parsed[0].getValue());
          assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
          assertEquals("Path","/",parsed[0].getPath());
          assertTrue("Secure",!parsed[0].getSecure());
          assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
          assertTrue("Comment",null == parsed[0].getComment());
      }
  
      public void testParseWithPath() throws Exception {
          Header header = new Header("Set-Cookie","cookie-name=cookie-value; Path=/path/");
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1",80, "/path/path", false, header);
          assertEquals("Found 1 cookie.",1,parsed.length);
          assertEquals("Name","cookie-name",parsed[0].getName());
          assertEquals("Value","cookie-value",parsed[0].getValue());
          assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
          assertEquals("Path","/path/",parsed[0].getPath());
          assertTrue("Secure",!parsed[0].getSecure());
          assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
          assertTrue("Comment",null == parsed[0].getComment());
      }
  
      public void testParseWithDomain() throws Exception {
          Header header = new Header("Set-Cookie","cookie-name=cookie-value; Domain=127.0.0.1");
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
          assertEquals("Found 1 cookie.",1,parsed.length);
          assertEquals("Name","cookie-name",parsed[0].getName());
          assertEquals("Value","cookie-value",parsed[0].getValue());
          assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
          assertEquals("Path","/",parsed[0].getPath());
          assertTrue("Secure",!parsed[0].getSecure());
          assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
          assertTrue("Comment",null == parsed[0].getComment());
      }
  
      public void testParseWithSecure() throws Exception {
          Header header = new Header("Set-Cookie","cookie-name=cookie-value; secure");
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
          assertEquals("Found 1 cookie.",1,parsed.length);
          assertEquals("Name","cookie-name",parsed[0].getName());
          assertEquals("Value","cookie-value",parsed[0].getValue());
          assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
          assertEquals("Path","/",parsed[0].getPath());
          assertTrue("Secure",parsed[0].getSecure());
          assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
          assertTrue("Comment",null == parsed[0].getComment());
      }
  
      public void testParseWithComment() throws Exception {
          Header header = new Header("Set-Cookie",
              "cookie-name=cookie-value; comment=\"This is a comment.\"");
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
          assertEquals("Found 1 cookie.",1,parsed.length);
          assertEquals("Name","cookie-name",parsed[0].getName());
          assertEquals("Value","cookie-value",parsed[0].getValue());
          assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
          assertEquals("Path","/",parsed[0].getPath());
          assertTrue("Secure",!parsed[0].getSecure());
          assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
          assertEquals("Comment","This is a comment.",parsed[0].getComment());
      }
  
      public void testParseWithExpires() throws Exception {
          Header header = new Header("Set-Cookie",
              "cookie-name=cookie-value;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
          assertEquals("Found 1 cookie.",1,parsed.length);
          assertEquals("Name","cookie-name",parsed[0].getName());
          assertEquals("Value","cookie-value",parsed[0].getValue());
          assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
          assertEquals("Path","/",parsed[0].getPath());
          assertTrue("Secure",!parsed[0].getSecure());
          assertEquals(new Date(10000L),parsed[0].getExpiryDate());
          assertTrue("Comment",null == parsed[0].getComment());
      }
  
      public void testParseWithAll() throws Exception {
          Header header = 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");
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
          assertEquals("Found 1 cookie.",1,parsed.length);
          assertEquals("Name","cookie-name",parsed[0].getName());
          assertEquals("Value","cookie-value",parsed[0].getValue());
          assertEquals("Domain",".apache.org",parsed[0].getDomain());
          assertEquals("Path","/commons",parsed[0].getPath());
          assertTrue("Secure",parsed[0].getSecure());
          assertEquals(new Date(10000L),parsed[0].getExpiryDate());
          assertEquals("Comment","This is a comment.",parsed[0].getComment());
          assertEquals("Version",0,parsed[0].getVersion());
      }
  
      public void testParseMultipleDifferentPaths() throws Exception {
          Header header = new Header("Set-Cookie",
              "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;" +
              "Path=/commons/httpclient;Version=1");
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
          HttpState state = new HttpState();
          state.addCookies(parsed);
          Cookie[] cookies = state.getCookies();
          assertEquals("Wrong number of cookies.",2,cookies.length);
          assertEquals("Name","name1",cookies[0].getName());
          assertEquals("Value","value1",cookies[0].getValue());
          assertEquals("Name","name1",cookies[1].getName());
          assertEquals("Value","value2",cookies[1].getValue());
      }
  
      public void testParseMultipleSamePaths() throws Exception {
          Header header = new Header("Set-Cookie",
              "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons");
  
          CookieSpec cookiespec = new CookieSpecBase();
          Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
          HttpState state = new HttpState();
          state.addCookies(parsed);
          Cookie[] cookies = state.getCookies();
          assertEquals("Found 1 cookies.",1,cookies.length);
          assertEquals("Name","name1",cookies[0].getName());
          assertEquals("Value","value2",cookies[0].getValue());
      }
  
      public void testParseWithWrongDomain() throws Exception {
          Header header = new Header("Set-Cookie",
              "cookie-name=cookie-value; domain=127.0.0.1; version=1");
  
          CookieSpec cookiespec = new CookieSpecBase();
          try {
              Cookie[] parsed = cookieParse(cookiespec, "127.0.0.2", 80, "/", false, header);
              fail("HttpException exception should have been thrown");
          } catch (HttpException e) {
              // expected
          }
      }
  
      public void testParseWithNullDomain() 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, "/", 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 CookieSpecBase();
          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 testParseWithNullDomainAndPath() 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);
              fail("IllegalArgumentException should have been thrown");
          } catch (IllegalArgumentException e) {
              // expected
          }
      }
  
      public void testParseWithPathMismatch() throws Exception {
          Header header = new Header("Set-Cookie",
              "cookie-name=cookie-value; path=/path/path/path");
  
          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) {
              // expected
          }
      }
      
      public void testParseWithPathMismatch2() throws Exception {
          Header header = new Header("Set-Cookie",
              "cookie-name=cookie-value; path=/foobar");
  
          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) {
              // expected
          }
      }
  
  
      /** Call CookieSpec#match providing null for domain to match on
       */
      public void testCreateCookieHeaderWithNullDomain() throws Exception {
          Header header = new Header("Set-Cookie",
              "cookie-name=cookie-value; Expires=Thu, 01-Jan-1970 00:00:10 GMT");
  
          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
          }
      }
      
      /** Call CookieSpec#match providing null for path to match on
       */
      public void testCreateCookieHeaderWithNullPath() throws Exception{
          Header header = new Header("Set-Cookie",
              "cookie-name=cookie-value; Expires=Thu, 01-Jan-1970 00:00:10 GMT");
  
          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
          }
      }
  
      /** 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");
  
          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);
              fail("IllegalArgumentException should have been thrown.");
          } 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");
  
          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());
      }
      
      /**
       * Tests if cookie constructor rejects cookie name containing blanks.
       */
      public void testInvalidCookieName() {
          try {
              CookieSpec cookiespec = new CookieSpecBase();
              cookiespec.parse("localhost", 80, "/", false, "invalid name="); 
              fail("MalformedCookieException must have been thrown");
          }
          catch(MalformedCookieException e) {
              // Expected            
          }
      }
  
  
      /**
       * Tests if cookie constructor rejects cookie name starting with $.
       */
      public void testInvalidCookieName2() {
          try {
              CookieSpec cookiespec = new CookieSpecBase();
              cookiespec.parse("localhost", 80, "/", false, "$invalid_name="); 
              fail("MalformedCookieException must have been thrown");
          }
          catch(MalformedCookieException e) {
              // Expected            
          }
      }
  
      /**
       * Tests several date formats.
       */
      public void testDateFormats() throws Exception {
          //comma, dashes
          checkDate("Thu, 01-Jan-70 00:00:10 GMT");
          checkDate("Thu, 01-Jan-2070 00:00:10 GMT");
          //no comma, dashes
          checkDate("Thu 01-Jan-70 00:00:10 GMT");
          checkDate("Thu 01-Jan-2070 00:00:10 GMT");
          //comma, spaces
          checkDate("Thu, 01 Jan 70 00:00:10 GMT");
          checkDate("Thu, 01 Jan 2070 00:00:10 GMT");
          //no comma, spaces
          checkDate("Thu 01 Jan 70 00:00:10 GMT");
          checkDate("Thu 01 Jan 2070 00:00:10 GMT");
          //weird stuff
          checkDate("Wed, 20-Nov-2002 09-38-33 GMT");
  
  
          try {
              checkDate("this aint a date");
              fail("Date check is bogous");
          } catch(Exception e) {
              /* must fail */
          }
      }
  
      private void checkDate(String date) throws Exception {
          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.
       */
      public void testMatchWithEqualPaths() {
          
          Cookie cookie = new Cookie(".test.com", "test", "1", "/test", null, false);
          
          CookieSpec cookiespec = new CookieSpecBase();
          boolean match = cookiespec.match("test.test.com", 80, "/test", false, cookie);
          assertTrue("Cookie paths did not match", match);
      }
  
      /**
       * Tests generic cookie formatting.
       */
      public void testGenericCookieFormatting() 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]);
          String s = cookiespec.formatCookie(cookies[0]);
          assertEquals("name=value", s);
      }    
  
      /**
       * Tests if null cookie values are handled correctly.
       */
      public void testNullCookieValueFormatting() {
          Cookie cookie = new Cookie(".whatever.com", "name", null, "/", null, false); 
          cookie.setDomainAttributeSpecified(true);
          cookie.setPathAttributeSpecified(true);
  
          CookieSpec cookiespec = new CookieSpecBase();
          String s = cookiespec.formatCookie(cookie);
          assertEquals("name=", 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); 
          cookie.setDomainAttributeSpecified(true);
          cookie.setPathAttributeSpecified(true);
  
          CookieSpec cookiespec = new CookieSpecBase();
          cookiespec.validate("sourceforge.net", 80, "/", false, cookie);
      }
  
  }
  
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieIgnoreSpec.java
  
  Index: TestCookieIgnoreSpec.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieIgnoreSpec.java,v 1.1 2004/04/24 19:39:24 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/04/24 19:39:24 $
   * ====================================================================
   *
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.httpclient.cookie;
  
  import java.io.IOException;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.httpclient.Header;
  import org.apache.commons.httpclient.HttpClientTestBase;
  import org.apache.commons.httpclient.methods.GetMethod;
  import org.apache.commons.httpclient.server.HttpService;
  import org.apache.commons.httpclient.server.SimpleRequest;
  import org.apache.commons.httpclient.server.SimpleResponse;
  
  
  /**
   * Test cases for ignore cookie apec
   *
   * @author Michael Becke
   * 
   * @version $Revision: 1.1 $
   */
  public class TestCookieIgnoreSpec extends HttpClientTestBase {
  
      // ------------------------------------------------------------ Constructor
  
      public TestCookieIgnoreSpec(String name) {
          super(name);
      }
  
      // ------------------------------------------------------- TestCase Methods
  
      public static Test suite() {
          return new TestSuite(TestCookieIgnoreSpec.class);
      }
  
      private class BasicAuthService implements HttpService {
  
          public BasicAuthService() {
              super();
          }
  
          public boolean process(final SimpleRequest request, final SimpleResponse response)
              throws IOException
          {
              response.setStatusLine("HTTP/1.1 200 OK");
              response.addHeader(new Header("Connection", "close"));
              response.addHeader(new Header("Set-Cookie", 
                  "custno = 12345; comment=test; version=1," +
                  " name=John; version=1; max-age=600; secure; domain=.apache.org"));
              return true;
          }
      }
  
      public void testCustomAuthorizationHeader() throws Exception {
          this.server.setHttpService(new BasicAuthService());
  
          GetMethod httpget = new GetMethod("/");
          httpget.getParams().setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
          
          try {
              this.client.executeMethod(httpget);
          } finally {
              httpget.releaseConnection();
          }
          assertEquals("Cookie parsing should have been disabled", 
                  0, this.client.getState().getCookies().length);
      }
  
  }
  
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieNetscapeDraft.java
  
  Index: TestCookieNetscapeDraft.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieNetscapeDraft.java,v 1.1 2004/04/24 19:39:24 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/04/24 19:39:24 $
   * ====================================================================
   *
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.httpclient.cookie;
  
  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.HttpException;
  
  
  /**
   * Test cases for Netscape cookie draft
   *
   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
   * 
   * @version $Revision: 1.1 $
   */
  public class TestCookieNetscapeDraft extends TestCookieBase {
  
      // ------------------------------------------------------------ Constructor
  
      public TestCookieNetscapeDraft(String name) {
          super(name);
      }
  
  
      // ------------------------------------------------------- TestCase Methods
  
  
      public static Test suite() {
          return new TestSuite(TestCookieNetscapeDraft.class);
      }
  
      public void testParseWithIllegalNetscapeDomain1() throws Exception {
          Header header = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com");
  
          CookieSpec cookiespec = new NetscapeDraftSpec();
          try {
              Cookie[] parsed = cookieParse(cookiespec, "a.com", 80, "/", false, header);
              fail("HttpException exception should have been thrown");
          } catch (HttpException e) {
              // expected
          }
      }
  
      public void testParseWithWrongNetscapeDomain2() throws Exception {
          Header header = new Header("Set-Cookie","cookie-name=cookie-value; domain=.y.z");
          
          CookieSpec cookiespec = new NetscapeDraftSpec();
          try {
              Cookie[] parsed = cookieParse(cookiespec, "x.y.z", 80, "/", false, header);
              fail("HttpException exception should have been thrown");
          } catch (HttpException e) {
              // expected
          }
      }
  
      /**
       * Tests Netscape specific cookie formatting.
       */
      
      public void testNetscapeCookieFormatting() throws Exception {
          Header header = new Header(
            "Set-Cookie", "name=value; path=/; domain=.mydomain.com");
          CookieSpec cookiespec = new NetscapeDraftSpec();
          Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
          cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
          String s = cookiespec.formatCookie(cookies[0]);
          assertEquals("name=value", s);
      }
      
      /**
       * Tests Netscape specific expire attribute parsing.
       */
      
      public void testNetscapeCookieExpireAttribute() throws Exception {
          CookieSpec cookiespec = new NetscapeDraftSpec();
          Header header = new Header("Set-Cookie", 
              "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment");
          Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
          cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
          header = new Header("Set-Cookie", 
              "name=value; path=/; domain=.mydomain.com; expires=Thu 01-Jan-2070 00:00:10 GMT; comment=no_comment");
          try {
              cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
              cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
              fail("MalformedCookieException must have been thrown");
          }
          catch (MalformedCookieException expected) {
          }
      }
  
      /**
       * 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 NetscapeDraftSpec();
          Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
          assertEquals("number of cookies", 1, cookies.length);
          assertEquals("a", cookies[0].getName());
          assertEquals("b,c", cookies[0].getValue());
      }
      
  }
  
  
  
  
  1.1                  jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2109Spec.java
  
  Index: TestCookieRFC2109Spec.java
  ===================================================================
  /*
   * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieRFC2109Spec.java,v 1.1 2004/04/24 19:39:24 olegk Exp $
   * $Revision: 1.1 $
   * $Date: 2004/04/24 19:39:24 $
   * ====================================================================
   *
   *  Copyright 1999-2004 The Apache Software Foundation
   *
   *  Licensed under the Apache License, Version 2.0 (the "License");
   *  you may not use this file except in compliance with the License.
   *  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
   *  Unless required by applicable law or agreed to in writing, software
   *  distributed under the License is distributed on an "AS IS" BASIS,
   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   *  See the License for the specific language governing permissions and
   *  limitations under the License.
   * ====================================================================
   *
   * This software consists of voluntary contributions made by many
   * individuals on behalf of the Apache Software Foundation.  For more
   * information on the Apache Software Foundation, please see
   * <http://www.apache.org/>.
   *
   */
  
  package org.apache.commons.httpclient.cookie;
  
  import junit.framework.Test;
  import junit.framework.TestSuite;
  
  import org.apache.commons.httpclient.Cookie;
  import org.apache.commons.httpclient.Header;
  
  /**
   * Test cases for RFC2109 cookie spec
   *
   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
   * 
   * @version $Revision: 1.1 $
   */
  public class TestCookieRFC2109Spec extends TestCookieBase {
  
  
      // ------------------------------------------------------------ Constructor
  
      public TestCookieRFC2109Spec(String name) {
          super(name);
      }
  
      // ------------------------------------------------------- TestCase Methods
  
      public static Test suite() {
          return new TestSuite(TestCookieRFC2109Spec.class);
      }
  
      public void testParseVersion() throws Exception {
          Header header = new Header("Set-Cookie","cookie-name=cookie-value; version=1");
  
          CookieSpec cookiespec = new RFC2109Spec();
          Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
          assertEquals("Found 1 cookie.",1,parsed.length);
          assertEquals("Name","cookie-name",parsed[0].getName());
          assertEquals("Value","cookie-value",parsed[0].getValue());
          assertEquals("Version",1,parsed[0].getVersion());
      }
  
      public void testParseWithWrongDomain2() 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("HttpException exception should have been thrown");
          } catch (MalformedCookieException e) {
              // expected
          }
      }
  
      /**
       * Domain has no embedded dots
       */
      public void testParseWithIllegalDomain() 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");
          } catch (MalformedCookieException e) {
              // expected
          }
      }
  
      /**
       * Domain has no embedded dots again
       */
      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");
          } 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");
  
          CookieSpec cookiespec = new RFC2109Spec();
          try {
              Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
              fail("HttpException exception should have been thrown");
          } catch (MalformedCookieException e) {
              // expected
          }
      }
  
      /**
       * 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", 
              "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
          Cookie[]cookies = cookiespec.parse("host", 80, "/", false, header );
          try {
              cookiespec.validate("host", 80, "/", false, cookies[0]);
              fail("MalformedCookieException must have thrown");
          }
          catch(MalformedCookieException expected) {
          }
          header = new Header("Set-Cookie", 
              "name=\"value\"; version=\"1\"; path=\"/\"; domain=\"host1\"");
          cookies = cookiespec.parse("host2", 80, "/", false, header );
          try {
              cookiespec.validate("host2", 80, "/", false, cookies[0]);
              fail("MalformedCookieException must have thrown");
          }
          catch(MalformedCookieException expected) {
          }
      }
  
      /**
       * Tests RFC 2109 compiant cookie formatting.
       */
      
      public void testRFC2109CookieFormatting() throws Exception {
          CookieSpec cookiespec = new RFC2109Spec();
          Header header = new Header("Set-Cookie", 
              "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
          Cookie[] cookies  = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
          cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
          String s1 = cookiespec.formatCookie(cookies[0]);
          assertEquals(s1, "$Version=\"1\"; name=\"value\"; $Domain=\".mydomain.com\"; $Path=\"/\"");
  
          header = new Header( "Set-Cookie", 
              "name=value; path=/; domain=.mydomain.com");
          cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
          cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
          String s2 = cookiespec.formatCookie(cookies[0]);
          assertEquals(s2, "$Version=0; name=value; $Domain=.mydomain.com; $Path=/");
      }
  
      /**
       * Tests if null cookie values are handled correctly.
       */
      public void testNullCookieValueFormatting() {
          Cookie cookie = new Cookie(".whatever.com", "name", null, "/", null, false); 
          cookie.setDomainAttributeSpecified(true);
          cookie.setPathAttributeSpecified(true);
  
          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());
      }
  
      /**
       * 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
          }
      }    
  }
  
  
  
  

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