TestCookieCompatibilitySpec xref

    1   /*
    2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieCompatibilitySpec.java,v 1.7 2004/09/14 20:11:32 olegk Exp $
    3    * $Revision: 480424 $
    4    * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
    5    * ====================================================================
    6    *
    7    *  Licensed to the Apache Software Foundation (ASF) under one or more
    8    *  contributor license agreements.  See the NOTICE file distributed with
    9    *  this work for additional information regarding copyright ownership.
    10   *  The ASF licenses this file to You under the Apache License, Version 2.0
    11   *  (the "License"); you may not use this file except in compliance with
    12   *  the License.  You may obtain a copy of the License at
    13   *
    14   *      http://www.apache.org/licenses/LICENSE-2.0
    15   *
    16   *  Unless required by applicable law or agreed to in writing, software
    17   *  distributed under the License is distributed on an "AS IS" BASIS,
    18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    19   *  See the License for the specific language governing permissions and
    20   *  limitations under the License.
    21   * ====================================================================
    22   *
    23   * This software consists of voluntary contributions made by many
    24   * individuals on behalf of the Apache Software Foundation.  For more
    25   * information on the Apache Software Foundation, please see
    26   * <http://www.apache.org/>.
    27   *
    28   */
    29
    30  package org.apache.commons.httpclient.cookie;
    31
    32  import java.util.Collection;
    33  import java.util.Date;
    34
    35  import junit.framework.Test;
    36  import junit.framework.TestSuite;
    37
    38  import org.apache.commons.httpclient.Cookie;
    39  import org.apache.commons.httpclient.Header;
    40  import org.apache.commons.httpclient.HttpException;
    41  import org.apache.commons.httpclient.HttpState;
    42  import org.apache.commons.httpclient.NameValuePair;
    43  import org.apache.commons.httpclient.params.DefaultHttpParamsFactory;
    44  import org.apache.commons.httpclient.params.HttpMethodParams;
    45  import org.apache.commons.httpclient.params.HttpParams;
    46
    47
    48  /**
    49   * Test cases for Cookie
    50   *
    51   * @author BC Holmes
    52   * @author Rod Waldhoff
    53   * @author dIon Gillard
    54   * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
    55   * @author Marc A. Saegesser
    56   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
    57   * @version $Revision: 480424 $
    58   */
    59  public class TestCookieCompatibilitySpec extends TestCookieBase {
    60
    61
    62      // ------------------------------------------------------------ Constructor
    63
    64
    65      public TestCookieCompatibilitySpec(String name) {
    66          super(name);
    67      }
    68
    69
    70      // ------------------------------------------------------- TestCase Methods
    71
    72
    73      public static Test suite() {
    74          return new TestSuite(TestCookieCompatibilitySpec.class);
    75      }
    76
    77      public void testParseAttributeInvalidAttrib() throws Exception {
    78          CookieSpec cookiespec = new CookieSpecBase();
    79          try {
    80              cookiespec.parseAttribute(null, null);
    81              fail("IllegalArgumentException must have been thrown");
    82          } catch (IllegalArgumentException expected) {
    83          }
    84      }
    85
    86      public void testParseAttributeInvalidCookie() throws Exception {
    87          CookieSpec cookiespec = new CookieSpecBase();
    88          try {
    89              cookiespec.parseAttribute(new NameValuePair("name", "value"), null);
    90              fail("IllegalArgumentException must have been thrown");
    91          } catch (IllegalArgumentException expected) {
    92          }
    93      }
    94
    95      public void testParseAttributeNullPath() throws Exception {
    96          CookieSpec cookiespec = new CookieSpecBase();
    97          Cookie cookie = new Cookie();
    98          cookiespec.parseAttribute(new NameValuePair("path", null), cookie);
    99          assertEquals("/", cookie.getPath());
    100     }
    101
    102     public void testParseAttributeBlankPath() throws Exception {
    103         CookieSpec cookiespec = new CookieSpecBase();
    104         Cookie cookie = new Cookie();
    105         cookiespec.parseAttribute(new NameValuePair("path", "   "), cookie);
    106         assertEquals("/", cookie.getPath());
    107     }
    108
    109     public void testParseAttributeNullDomain() throws Exception {
    110         CookieSpec cookiespec = new CookieSpecBase();
    111         Cookie cookie = new Cookie();
    112         try {
    113             cookiespec.parseAttribute(new NameValuePair("domain", null), cookie);
    114             fail("MalformedCookieException must have been thrown");
    115         } catch (MalformedCookieException expected) {
    116         }
    117     }
    118
    119     public void testParseAttributeBlankDomain() throws Exception {
    120         CookieSpec cookiespec = new CookieSpecBase();
    121         Cookie cookie = new Cookie();
    122         try {
    123             cookiespec.parseAttribute(new NameValuePair("domain", "   "), cookie);
    124             fail("MalformedCookieException must have been thrown");
    125         } catch (MalformedCookieException expected) {
    126         }
    127     }
    128
    129     public void testParseAttributeNullMaxAge() throws Exception {
    130         CookieSpec cookiespec = new CookieSpecBase();
    131         Cookie cookie = new Cookie();
    132         try {
    133             cookiespec.parseAttribute(new NameValuePair("max-age", null), cookie);
    134             fail("MalformedCookieException must have been thrown");
    135         } catch (MalformedCookieException expected) {
    136         }
    137     }
    138
    139     public void testParseAttributeInvalidMaxAge() throws Exception {
    140         CookieSpec cookiespec = new CookieSpecBase();
    141         Cookie cookie = new Cookie();
    142         try {
    143             cookiespec.parseAttribute(new NameValuePair("max-age", "crap"), cookie);
    144             fail("MalformedCookieException must have been thrown");
    145         } catch (MalformedCookieException expected) {
    146         }
    147     }
    148
    149     public void testParseAttributeNullExpires() throws Exception {
    150         CookieSpec cookiespec = new CookieSpecBase();
    151         Cookie cookie = new Cookie();
    152         try {
    153             cookiespec.parseAttribute(new NameValuePair("expires", null), cookie);
    154             fail("MalformedCookieException must have been thrown");
    155         } catch (MalformedCookieException expected) {
    156         }
    157     }
    158
    159     public void testParseAttributeUnknownValue() throws Exception {
    160         CookieSpec cookiespec = new CookieSpecBase();
    161         Cookie cookie = new Cookie();
    162         cookiespec.parseAttribute(new NameValuePair("nonsense", null), cookie);
    163     }
    164
    165     public void testValidateNullHost() throws Exception {
    166         CookieSpec cookiespec = new CookieSpecBase();
    167         Cookie cookie = new Cookie();
    168         try {
    169             cookiespec.validate(null, 80, "/", false, cookie);
    170             fail("IllegalArgumentException must have been thrown");
    171         } catch (IllegalArgumentException expected) {
    172         }
    173     }
    174
    175     public void testValidateBlankHost() throws Exception {
    176         CookieSpec cookiespec = new CookieSpecBase();
    177         Cookie cookie = new Cookie();
    178         try {
    179             cookiespec.validate("   ", 80, "/", false, cookie);
    180             fail("IllegalArgumentException must have been thrown");
    181         } catch (IllegalArgumentException expected) {
    182         }
    183     }
    184
    185     public void testValidateNullPath() throws Exception {
    186         CookieSpec cookiespec = new CookieSpecBase();
    187         Cookie cookie = new Cookie();
    188         try {
    189             cookiespec.validate("host", 80, null, false, cookie);
    190             fail("IllegalArgumentException must have been thrown");
    191         } catch (IllegalArgumentException expected) {
    192         }
    193     }
    194
    195     public void testValidateBlankPath() throws Exception {
    196         CookieSpec cookiespec = new CookieSpecBase();
    197         Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
    198         cookiespec.validate("host", 80, "   ", false, cookie);
    199     }
    200
    201     public void testValidateInvalidPort() throws Exception {
    202         CookieSpec cookiespec = new CookieSpecBase();
    203         Cookie cookie = new Cookie();
    204         try {
    205             cookiespec.validate("host", -80, "/", false, cookie);
    206             fail("IllegalArgumentException must have been thrown");
    207         } catch (IllegalArgumentException expected) {
    208         }
    209     }
    210
    211     public void testValidateInvalidCookieVersion() throws Exception {
    212         CookieSpec cookiespec = new CookieSpecBase();
    213         Cookie cookie = new Cookie();
    214         cookie.setVersion(-1);
    215         try {
    216             cookiespec.validate("host", 80, "/", false, cookie);
    217             fail("MalformedCookieException must have been thrown");
    218         } catch (MalformedCookieException expected) {
    219         }
    220     }
    221
    222     /**
    223      * Tests whether domain attribute check is case-insensitive.
    224      */
    225     public void testDomainCaseInsensitivity() throws Exception {
    226         Header header = new Header("Set-Cookie",
    227             "name=value; path=/; domain=.whatever.com");
    228
    229         CookieSpec cookiespec = new CookieSpecBase();
    230         Cookie[] parsed = cookieParse(cookiespec, "www.WhatEver.com", 80, "/", false, header);
    231         assertNotNull(parsed);
    232         assertEquals(1, parsed.length);
    233         assertEquals(".whatever.com", parsed[0].getDomain());
    234     }
    235
    236     /**
    237      * Test basic parse (with various spacings
    238      */
    239     public void testParse1() throws Exception {
    240         String headerValue = "custno = 12345; comment=test; version=1," +
    241             " name=John; version=1; max-age=600; secure; domain=.apache.org";
    242
    243         Header header = new Header("set-cookie", headerValue);
    244
    245         CookieSpec cookiespec = new CookieSpecBase();
    246         Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
    247         assertEquals(2, cookies.length);
    248
    249         assertEquals("custno", cookies[0].getName());
    250         assertEquals("12345", cookies[0].getValue());
    251         assertEquals("test", cookies[0].getComment());
    252         assertEquals(0, cookies[0].getVersion());
    253         assertEquals("www.apache.org", cookies[0].getDomain());
    254         assertEquals("/", cookies[0].getPath());
    255         assertFalse(cookies[0].getSecure());
    256
    257         assertEquals("name", cookies[1].getName());
    258         assertEquals("John", cookies[1].getValue());
    259         assertEquals(null, cookies[1].getComment());
    260         assertEquals(0, cookies[1].getVersion());
    261         assertEquals(".apache.org", cookies[1].getDomain());
    262         assertEquals("/", cookies[1].getPath());
    263         assertTrue(cookies[1].getSecure());
    264     }
    265
    266
    267     /**
    268      * Test no spaces
    269      */
    270     public void testParse2() throws Exception {
    271         String headerValue = "custno=12345;comment=test; version=1," +
    272             "name=John;version=1;max-age=600;secure;domain=.apache.org";
    273
    274         Header header = new Header("set-cookie", headerValue);
    275
    276         CookieSpec cookiespec = new CookieSpecBase();
    277         Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
    278
    279         assertEquals(2, cookies.length);
    280
    281         assertEquals("custno", cookies[0].getName());
    282         assertEquals("12345", cookies[0].getValue());
    283         assertEquals("test", cookies[0].getComment());
    284         assertEquals(0, cookies[0].getVersion());
    285         assertEquals("www.apache.org", cookies[0].getDomain());
    286         assertEquals("/", cookies[0].getPath());
    287         assertFalse(cookies[0].getSecure());
    288
    289         assertEquals("name", cookies[1].getName());
    290         assertEquals("John", cookies[1].getValue());
    291         assertEquals(null, cookies[1].getComment());
    292         assertEquals(0, cookies[1].getVersion());
    293         assertEquals(".apache.org", cookies[1].getDomain());
    294         assertEquals("/", cookies[1].getPath());
    295         assertTrue(cookies[1].getSecure());
    296     }
    297
    298
    299     /**
    300      * Test parse with quoted text
    301      */
    302     public void testParse3() throws Exception {
    303         String headerValue =
    304             "name=\"Doe, John\";version=1;max-age=600;secure;domain=.apache.org";
    305         Header header = new Header("set-cookie", headerValue);
    306
    307         CookieSpec cookiespec = new CookieSpecBase();
    308         Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
    309
    310         assertEquals(1, cookies.length);
    311
    312         assertEquals("name", cookies[0].getName());
    313         assertEquals("Doe, John", cookies[0].getValue());
    314         assertEquals(null, cookies[0].getComment());
    315         assertEquals(0, cookies[0].getVersion());
    316         assertEquals(".apache.org", cookies[0].getDomain());
    317         assertEquals("/", cookies[0].getPath());
    318         assertTrue(cookies[0].getSecure());
    319     }
    320
    321
    322     // see issue #5279
    323     public void testQuotedExpiresAttribute() throws Exception {
    324         String headerValue = "custno=12345;Expires='Thu, 01-Jan-2070 00:00:10 GMT'";
    325
    326         Header header = new Header("set-cookie", headerValue);
    327
    328         CookieSpec cookiespec = new CookieSpecBase();
    329         Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", true, header);
    330         assertNotNull("Expected some cookies",cookies);
    331         assertEquals("Expected 1 cookie",1,cookies.length);
    332         assertNotNull("Expected cookie to have getExpiryDate",cookies[0].getExpiryDate());
    333     }
    334
    335     public void testSecurityError() throws Exception {
    336         String headerValue = "custno=12345;comment=test; version=1," +
    337             "name=John;version=1;max-age=600;secure;domain=jakarta.apache.org";
    338         Header header = new Header("set-cookie", headerValue);
    339
    340         CookieSpec cookiespec = new CookieSpecBase();
    341         try {
    342             Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
    343             fail("HttpException exception should have been thrown");
    344         } catch (HttpException e) {
    345             // expected
    346         }
    347     }
    348
    349     public void testParseSimple() throws Exception {
    350         Header header = new Header("Set-Cookie","cookie-name=cookie-value");
    351
    352         CookieSpec cookiespec = new CookieSpecBase();
    353         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path/path", false, header);
    354         assertEquals("Found 1 cookie.",1,parsed.length);
    355         assertEquals("Name","cookie-name",parsed[0].getName());
    356         assertEquals("Value","cookie-value",parsed[0].getValue());
    357         assertTrue("Comment",null == parsed[0].getComment());
    358         assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
    359         //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded());
    360         assertTrue("isPersistent",!parsed[0].isPersistent());
    361         assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
    362         assertEquals("Path","/path",parsed[0].getPath());
    363         assertTrue("Secure",!parsed[0].getSecure());
    364         assertEquals("Version",0,parsed[0].getVersion());
    365     }
    366
    367     public void testParseSimple2() throws Exception {
    368         Header header = new Header("Set-Cookie", "cookie-name=cookie-value");
    369
    370         CookieSpec cookiespec = new CookieSpecBase();
    371         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path", false, header);
    372         assertEquals("Found 1 cookie.", 1, parsed.length);
    373         assertEquals("Name", "cookie-name", parsed[0].getName());
    374         assertEquals("Value", "cookie-value", parsed[0].getValue());
    375         assertTrue("Comment", null == parsed[0].getComment());
    376         assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
    377         //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded());
    378         assertTrue("isPersistent", !parsed[0].isPersistent());
    379         assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
    380         assertEquals("Path", "/", parsed[0].getPath());
    381         assertTrue("Secure", !parsed[0].getSecure());
    382         assertEquals("Version", 0, parsed[0].getVersion());
    383     }
    384
    385     public void testParseNoName() throws Exception {
    386         Header header = new Header("Set-Cookie","=stuff; path=/");
    387
    388         CookieSpec cookiespec = new CookieSpecBase();
    389         try {
    390             Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
    391             fail("MalformedCookieException should have been thrown");
    392         } catch (MalformedCookieException ex) {
    393             // expected
    394         }
    395     }
    396
    397     public void testParseNoValue() throws Exception {
    398         Header header = new Header("Set-Cookie","cookie-name=");
    399
    400         CookieSpec cookiespec = new CookieSpecBase();
    401         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
    402         assertEquals("Found 1 cookie.",1,parsed.length);
    403         assertEquals("Name","cookie-name",parsed[0].getName());
    404         assertEquals("Value", "", parsed[0].getValue());
    405         assertTrue("Comment",null == parsed[0].getComment());
    406         assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
    407         //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded());
    408         assertTrue("isPersistent",!parsed[0].isPersistent());
    409         assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
    410         assertEquals("Path","/",parsed[0].getPath());
    411         assertTrue("Secure",!parsed[0].getSecure());
    412         assertEquals("Version",0,parsed[0].getVersion());
    413     }
    414
    415     public void testParseWithWhiteSpace() throws Exception {
    416         Header header = new Header("Set-Cookie"," cookie-name  =    cookie-value  ");
    417
    418         CookieSpec cookiespec = new CookieSpecBase();
    419         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
    420         assertEquals("Found 1 cookie.",1,parsed.length);
    421         assertEquals("Name","cookie-name",parsed[0].getName());
    422         assertEquals("Value","cookie-value",parsed[0].getValue());
    423         assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
    424         assertEquals("Path","/",parsed[0].getPath());
    425         assertTrue("Secure",!parsed[0].getSecure());
    426         assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
    427         assertTrue("Comment",null == parsed[0].getComment());
    428     }
    429
    430     public void testParseWithQuotes() throws Exception {
    431         Header header = new Header("Set-Cookie"," cookie-name  =  \" cookie-value \" ;path=/");
    432
    433         CookieSpec cookiespec = new CookieSpecBase();
    434         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1",80, "/", false, header);
    435         assertEquals("Found 1 cookie.",1,parsed.length);
    436         assertEquals("Name","cookie-name",parsed[0].getName());
    437         assertEquals("Value"," cookie-value ",parsed[0].getValue());
    438         assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
    439         assertEquals("Path","/",parsed[0].getPath());
    440         assertTrue("Secure",!parsed[0].getSecure());
    441         assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
    442         assertTrue("Comment",null == parsed[0].getComment());
    443     }
    444
    445     public void testParseWithPath() throws Exception {
    446         Header header = new Header("Set-Cookie","cookie-name=cookie-value; Path=/path/");
    447
    448         CookieSpec cookiespec = new CookieSpecBase();
    449         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1",80, "/path/path", false, header);
    450         assertEquals("Found 1 cookie.",1,parsed.length);
    451         assertEquals("Name","cookie-name",parsed[0].getName());
    452         assertEquals("Value","cookie-value",parsed[0].getValue());
    453         assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
    454         assertEquals("Path","/path/",parsed[0].getPath());
    455         assertTrue("Secure",!parsed[0].getSecure());
    456         assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
    457         assertTrue("Comment",null == parsed[0].getComment());
    458     }
    459
    460     public void testParseWithDomain() throws Exception {
    461         Header header = new Header("Set-Cookie","cookie-name=cookie-value; Domain=127.0.0.1");
    462
    463         CookieSpec cookiespec = new CookieSpecBase();
    464         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
    465         assertEquals("Found 1 cookie.",1,parsed.length);
    466         assertEquals("Name","cookie-name",parsed[0].getName());
    467         assertEquals("Value","cookie-value",parsed[0].getValue());
    468         assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
    469         assertEquals("Path","/",parsed[0].getPath());
    470         assertTrue("Secure",!parsed[0].getSecure());
    471         assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
    472         assertTrue("Comment",null == parsed[0].getComment());
    473     }
    474
    475     public void testParseWithSecure() throws Exception {
    476         Header header = new Header("Set-Cookie","cookie-name=cookie-value; secure");
    477
    478         CookieSpec cookiespec = new CookieSpecBase();
    479         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
    480         assertEquals("Found 1 cookie.",1,parsed.length);
    481         assertEquals("Name","cookie-name",parsed[0].getName());
    482         assertEquals("Value","cookie-value",parsed[0].getValue());
    483         assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
    484         assertEquals("Path","/",parsed[0].getPath());
    485         assertTrue("Secure",parsed[0].getSecure());
    486         assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
    487         assertTrue("Comment",null == parsed[0].getComment());
    488     }
    489
    490     public void testParseWithComment() throws Exception {
    491         Header header = new Header("Set-Cookie",
    492             "cookie-name=cookie-value; comment=\"This is a comment.\"");
    493
    494         CookieSpec cookiespec = new CookieSpecBase();
    495         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
    496         assertEquals("Found 1 cookie.",1,parsed.length);
    497         assertEquals("Name","cookie-name",parsed[0].getName());
    498         assertEquals("Value","cookie-value",parsed[0].getValue());
    499         assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
    500         assertEquals("Path","/",parsed[0].getPath());
    501         assertTrue("Secure",!parsed[0].getSecure());
    502         assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
    503         assertEquals("Comment","This is a comment.",parsed[0].getComment());
    504     }
    505
    506     public void testParseWithExpires() throws Exception {
    507         Header header = new Header("Set-Cookie",
    508             "cookie-name=cookie-value;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
    509
    510         CookieSpec cookiespec = new CookieSpecBase();
    511         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
    512         assertEquals("Found 1 cookie.",1,parsed.length);
    513         assertEquals("Name","cookie-name",parsed[0].getName());
    514         assertEquals("Value","cookie-value",parsed[0].getValue());
    515         assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
    516         assertEquals("Path","/",parsed[0].getPath());
    517         assertTrue("Secure",!parsed[0].getSecure());
    518         assertEquals(new Date(10000L),parsed[0].getExpiryDate());
    519         assertTrue("Comment",null == parsed[0].getComment());
    520     }
    521
    522     public void testParseWithAll() throws Exception {
    523         Header header = new Header("Set-Cookie",
    524             "cookie-name=cookie-value;Version=1;Path=/commons;Domain=.apache.org;" +
    525             "Comment=This is a comment.;secure;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
    526
    527         CookieSpec cookiespec = new CookieSpecBase();
    528         Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
    529         assertEquals("Found 1 cookie.",1,parsed.length);
    530         assertEquals("Name","cookie-name",parsed[0].getName());
    531         assertEquals("Value","cookie-value",parsed[0].getValue());
    532         assertEquals("Domain",".apache.org",parsed[0].getDomain());
    533         assertEquals("Path","/commons",parsed[0].getPath());
    534         assertTrue("Secure",parsed[0].getSecure());
    535         assertEquals(new Date(10000L),parsed[0].getExpiryDate());
    536         assertEquals("Comment","This is a comment.",parsed[0].getComment());
    537         assertEquals("Version",0,parsed[0].getVersion());
    538     }
    539
    540     public void testParseMultipleDifferentPaths() throws Exception {
    541         Header header = new Header("Set-Cookie",
    542             "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;" +
    543             "Path=/commons/httpclient;Version=1");
    544
    545         CookieSpec cookiespec = new CookieSpecBase();
    546         Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
    547         HttpState state = new HttpState();
    548         state.addCookies(parsed);
    549         Cookie[] cookies = state.getCookies();
    550         assertEquals("Wrong number of cookies.",2,cookies.length);
    551         assertEquals("Name","name1",cookies[0].getName());
    552         assertEquals("Value","value1",cookies[0].getValue());
    553         assertEquals("Name","name1",cookies[1].getName());
    554         assertEquals("Value","value2",cookies[1].getValue());
    555     }
    556
    557     public void testParseMultipleSamePaths() throws Exception {
    558         Header header = new Header("Set-Cookie",
    559             "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons");
    560
    561         CookieSpec cookiespec = new CookieSpecBase();
    562         Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
    563         HttpState state = new HttpState();
    564         state.addCookies(parsed);
    565         Cookie[] cookies = state.getCookies();
    566         assertEquals("Found 1 cookies.",1,cookies.length);
    567         assertEquals("Name","name1",cookies[0].getName());
    568         assertEquals("Value","value2",cookies[0].getValue());
    569     }
    570
    571     public void testParseRelativePath() throws Exception {
    572         Header header = new Header("Set-Cookie", "name1=value1;Path=whatever");
    573
    574         CookieSpec cookiespec = new CookieSpecBase();
    575         Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "whatever", true, header);
    576         assertEquals("Found 1 cookies.",1,parsed.length);
    577         assertEquals("Name","name1",parsed[0].getName());
    578         assertEquals("Value","value1",parsed[0].getValue());
    579         assertEquals("Path","whatever",parsed[0].getPath());
    580     }
    581
    582     public void testParseWithWrongDomain() throws Exception {
    583         Header header = new Header("Set-Cookie",
    584             "cookie-name=cookie-value; domain=127.0.0.1; version=1");
    585
    586         CookieSpec cookiespec = new CookieSpecBase();
    587         try {
    588             Cookie[] parsed = cookieParse(cookiespec, "127.0.0.2", 80, "/", false, header);
    589             fail("HttpException exception should have been thrown");
    590         } catch (HttpException e) {
    591             // expected
    592         }
    593     }
    594
    595     public void testParseWithNullHost() throws Exception {
    596         Header header = new Header("Set-Cookie",
    597             "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
    598
    599         CookieSpec cookiespec = new CookieSpecBase();
    600         try {
    601             Cookie[] parsed = cookieParse(cookiespec, null, 80, "/", false, header);
    602             fail("IllegalArgumentException should have been thrown");
    603         } catch (IllegalArgumentException e) {
    604             // expected
    605         }
    606     }
    607
    608     public void testParseWithBlankHost() throws Exception {
    609         Header header = new Header("Set-Cookie",
    610             "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
    611
    612         CookieSpec cookiespec = new CookieSpecBase();
    613         try {
    614             Cookie[] parsed = cookieParse(cookiespec, "  ", 80, "/", false, header);
    615             fail("IllegalArgumentException should have been thrown");
    616         } catch (IllegalArgumentException e) {
    617             // expected
    618         }
    619     }
    620
    621     public void testParseWithNullPath() throws Exception {
    622         Header header = new Header("Set-Cookie",
    623             "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
    624
    625         CookieSpec cookiespec = new CookieSpecBase();
    626         try {
    627             Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, null, false, header);
    628             fail("IllegalArgumentException should have been thrown");
    629         } catch (IllegalArgumentException e) {
    630             // expected
    631         }
    632     }
    633
    634     public void testParseWithBlankPath() throws Exception {
    635         Header header = new Header("Set-Cookie",
    636             "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
    637
    638         CookieSpec cookiespec = new CookieSpecBase();
    639         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "  ", false, header);
    640         assertNotNull(parsed);
    641         assertEquals(1, parsed.length);
    642         assertEquals("/", parsed[0].getPath());
    643     }
    644
    645     public void testParseWithNegativePort() throws Exception {
    646         Header header = new Header("Set-Cookie",
    647             "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
    648
    649         CookieSpec cookiespec = new CookieSpecBase();
    650         try {
    651             Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", -80, null, false, header);
    652             fail("IllegalArgumentException should have been thrown");
    653         } catch (IllegalArgumentException e) {
    654             // expected
    655         }
    656     }
    657
    658     public void testParseWithNullHostAndPath() throws Exception {
    659         Header header = new Header("Set-Cookie",
    660             "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
    661
    662         CookieSpec cookiespec = new CookieSpecBase();
    663         try {
    664             Cookie[] parsed = cookieParse(cookiespec, null, 80, null, false, header);
    665             fail("IllegalArgumentException should have been thrown");
    666         } catch (IllegalArgumentException e) {
    667             // expected
    668         }
    669     }
    670
    671     public void testParseWithPathMismatch() throws Exception {
    672         Header header = new Header("Set-Cookie",
    673             "cookie-name=cookie-value; path=/path/path/path");
    674
    675         CookieSpec cookiespec = new CookieSpecBase();
    676         try {
    677             Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path", false, header);
    678             fail("MalformedCookieException should have been thrown.");
    679         } catch (MalformedCookieException e) {
    680             // expected
    681         }
    682     }
    683
    684     public void testParseWithPathMismatch2() throws Exception {
    685         Header header = new Header("Set-Cookie",
    686             "cookie-name=cookie-value; path=/foobar");
    687
    688         CookieSpec cookiespec = new CookieSpecBase();
    689         try {
    690             Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/foo", false, header);
    691             fail("MalformedCookieException should have been thrown.");
    692         } catch (MalformedCookieException e) {
    693             // expected
    694         }
    695     }
    696
    697
    698     public void testParseWithInvalidHeader1() throws Exception {
    699         CookieSpec cookiespec = new CookieSpecBase();
    700         try {
    701             Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo", false, (Header)null);
    702             fail("IllegalArgumentException should have been thrown.");
    703         } catch (IllegalArgumentException e) {
    704             // expected
    705         }
    706     }
    707
    708     public void testParseWithInvalidHeader2() throws Exception {
    709         CookieSpec cookiespec = new CookieSpecBase();
    710         try {
    711             Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo", false, (String)null);
    712             fail("IllegalArgumentException should have been thrown.");
    713         } catch (IllegalArgumentException e) {
    714             // expected
    715         }
    716     }
    717
    718     /**
    719      * Tests if cookie constructor rejects cookie name containing blanks.
    720      */
    721     public void testCookieNameWithBlanks() throws Exception {
    722         Header setcookie = new Header("Set-Cookie", "invalid name=");
    723         CookieSpec cookiespec = new CookieSpecBase();
    724         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, setcookie);
    725         assertNotNull(parsed);
    726         assertEquals(1, parsed.length);
    727     }
    728
    729
    730     /**
    731      * Tests if cookie constructor rejects cookie name starting with $.
    732      */
    733     public void testCookieNameStartingWithDollarSign() throws Exception {
    734         Header setcookie = new Header("Set-Cookie", "$invalid_name=");
    735         CookieSpec cookiespec = new CookieSpecBase();
    736         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, setcookie);
    737         assertNotNull(parsed);
    738         assertEquals(1, parsed.length);
    739     }
    740
    741
    742     /**
    743      * Tests if malformatted expires attribute is parsed correctly.
    744      */
    745     public void testCookieWithComma() throws Exception {
    746         Header header = new Header("Set-Cookie", "name=value; expires=\"Thu, 01-Jan-1970 00:00:00 GMT");
    747
    748         CookieSpec cookiespec = new CookieSpecBase();
    749         try {
    750             Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
    751             fail("MalformedCookieException should have been thrown");
    752         } catch (MalformedCookieException expected) {
    753         }
    754     }
    755
    756
    757     /**
    758      * Tests several date formats.
    759      */
    760     public void testDateFormats() throws Exception {
    761         //comma, dashes
    762         checkDate("Thu, 01-Jan-70 00:00:10 GMT");
    763         checkDate("Thu, 01-Jan-2070 00:00:10 GMT");
    764         //no comma, dashes
    765         checkDate("Thu 01-Jan-70 00:00:10 GMT");
    766         checkDate("Thu 01-Jan-2070 00:00:10 GMT");
    767         //comma, spaces
    768         checkDate("Thu, 01 Jan 70 00:00:10 GMT");
    769         checkDate("Thu, 01 Jan 2070 00:00:10 GMT");
    770         //no comma, spaces
    771         checkDate("Thu 01 Jan 70 00:00:10 GMT");
    772         checkDate("Thu 01 Jan 2070 00:00:10 GMT");
    773         //weird stuff
    774         checkDate("Wed, 20-Nov-2002 09-38-33 GMT");
    775
    776
    777         try {
    778             checkDate("this aint a date");
    779             fail("Date check is bogous");
    780         } catch(Exception e) {
    781             /* must fail */
    782         }
    783     }
    784
    785     private void checkDate(String date) throws Exception {
    786         Header header = new Header("Set-Cookie", "custno=12345;Expires='"+date+"';");
    787         HttpParams params = new DefaultHttpParamsFactory().getDefaultParams();
    788         CookieSpec cookiespec = new CookieSpecBase();
    789         cookiespec.setValidDateFormats(
    790         		(Collection)params.getParameter(HttpMethodParams.DATE_PATTERNS));
    791         cookieParse(cookiespec, "localhost", 80, "/", false, header);
    792     }
    793
    794     /**
    795      * Tests if invalid second domain level cookie gets accepted in the
    796      * browser compatibility mode.
    797      */
    798     public void testSecondDomainLevelCookie() throws Exception {
    799         Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
    800         cookie.setDomainAttributeSpecified(true);
    801         cookie.setPathAttributeSpecified(true);
    802
    803         CookieSpec cookiespec = new CookieSpecBase();
    804         cookiespec.validate("sourceforge.net", 80, "/", false, cookie);
    805     }
    806
    807     public void testSecondDomainLevelCookieMatch1() throws Exception {
    808         Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
    809         cookie.setDomainAttributeSpecified(true);
    810         cookie.setPathAttributeSpecified(true);
    811
    812         CookieSpec cookiespec = new CookieSpecBase();
    813         assertTrue(cookiespec.match("sourceforge.net", 80, "/", false, cookie));
    814     }
    815
    816     public void testSecondDomainLevelCookieMatch2() throws Exception {
    817         Cookie cookie = new Cookie("sourceforge.net", "name", null, "/", null, false);
    818         cookie.setDomainAttributeSpecified(true);
    819         cookie.setPathAttributeSpecified(true);
    820
    821         CookieSpec cookiespec = new CookieSpecBase();
    822         assertTrue(cookiespec.match("www.sourceforge.net", 80, "/", false, cookie));
    823     }
    824
    825     public void testSecondDomainLevelCookieMatch3() throws Exception {
    826         Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
    827          cookie.setDomainAttributeSpecified(true);
    828          cookie.setPathAttributeSpecified(true);
    829
    830          CookieSpec cookiespec = new CookieSpecBase();
    831          assertTrue(cookiespec.match("www.sourceforge.net", 80, "/", false, cookie));
    832     }
    833
    834     public void testInvalidSecondDomainLevelCookieMatch1() throws Exception {
    835         Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
    836         cookie.setDomainAttributeSpecified(true);
    837         cookie.setPathAttributeSpecified(true);
    838
    839         CookieSpec cookiespec = new CookieSpecBase();
    840         assertFalse(cookiespec.match("antisourceforge.net", 80, "/", false, cookie));
    841     }
    842
    843     public void testInvalidSecondDomainLevelCookieMatch2() throws Exception {
    844         Cookie cookie = new Cookie("sourceforge.net", "name", null, "/", null, false);
    845         cookie.setDomainAttributeSpecified(true);
    846         cookie.setPathAttributeSpecified(true);
    847
    848         CookieSpec cookiespec = new CookieSpecBase();
    849         assertFalse(cookiespec.match("antisourceforge.net", 80, "/", false, cookie));
    850     }
    851
    852     public void testMatchNullHost() throws Exception {
    853         CookieSpec cookiespec = new CookieSpecBase();
    854         Cookie cookie = new Cookie();
    855         try {
    856             cookiespec.match(null, 80, "/", false, cookie);
    857             fail("IllegalArgumentException must have been thrown");
    858         } catch (IllegalArgumentException expected) {
    859         }
    860     }
    861
    862     public void testMatchBlankHost() throws Exception {
    863         CookieSpec cookiespec = new CookieSpecBase();
    864         Cookie cookie = new Cookie();
    865         try {
    866             cookiespec.match("   ", 80, "/", false, cookie);
    867             fail("IllegalArgumentException must have been thrown");
    868         } catch (IllegalArgumentException expected) {
    869         }
    870     }
    871
    872     public void testMatchInvalidPort() throws Exception {
    873         CookieSpec cookiespec = new CookieSpecBase();
    874         Cookie cookie = new Cookie();
    875         try {
    876             cookiespec.match("host", -80, "/", false, cookie);
    877             fail("IllegalArgumentException must have been thrown");
    878         } catch (IllegalArgumentException expected) {
    879         }
    880     }
    881
    882     public void testMatchNullPath() throws Exception {
    883         CookieSpec cookiespec = new CookieSpecBase();
    884         Cookie cookie = new Cookie();
    885         try {
    886             cookiespec.match("host", 80, null, false, cookie);
    887             fail("IllegalArgumentException must have been thrown");
    888         } catch (IllegalArgumentException expected) {
    889         }
    890     }
    891
    892     public void testMatchBlankPath() throws Exception {
    893         CookieSpec cookiespec = new CookieSpecBase();
    894         Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
    895         assertTrue(cookiespec.match("host", 80, "  ", false, cookie));
    896     }
    897
    898     public void testMatchNullCookie() throws Exception {
    899         CookieSpec cookiespec = new CookieSpecBase();
    900         try {
    901             cookiespec.match("host", 80, "/", false, (Cookie)null);
    902             fail("IllegalArgumentException must have been thrown");
    903         } catch (IllegalArgumentException expected) {
    904         }
    905     }
    906
    907     public void testMatchNullCookieDomain() throws Exception {
    908         CookieSpec cookiespec = new CookieSpecBase();
    909         Cookie cookie = new Cookie(null, "name", "value", "/", null, false);
    910         assertFalse(cookiespec.match("host", 80, "/", false, cookie));
    911     }
    912
    913     public void testMatchNullCookiePath() throws Exception {
    914         CookieSpec cookiespec = new CookieSpecBase();
    915         Cookie cookie = new Cookie("host", "name", "value", null, null, false);
    916         assertFalse(cookiespec.match("host", 80, "/", false, cookie));
    917     }
    918
    919     public void testCookieMatch1() throws Exception {
    920         CookieSpec cookiespec = new CookieSpecBase();
    921         Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
    922         assertTrue(cookiespec.match("host", 80, "/", false, cookie));
    923     }
    924
    925     public void testCookieMatch2() throws Exception {
    926         CookieSpec cookiespec = new CookieSpecBase();
    927         Cookie cookie = new Cookie(".whatever.com", "name", "value", "/", null, false);
    928         assertTrue(cookiespec.match(".whatever.com", 80, "/", false, cookie));
    929     }
    930
    931     public void testCookieMatch3() throws Exception {
    932         CookieSpec cookiespec = new CookieSpecBase();
    933         Cookie cookie = new Cookie(".whatever.com", "name", "value", "/", null, false);
    934         assertTrue(cookiespec.match(".really.whatever.com", 80, "/", false, cookie));
    935     }
    936
    937     public void testCookieMatch4() throws Exception {
    938         CookieSpec cookiespec = new CookieSpecBase();
    939         Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
    940         assertTrue(cookiespec.match("host", 80, "/foobar", false, cookie));
    941     }
    942
    943     public void testCookieMismatch1() throws Exception {
    944         CookieSpec cookiespec = new CookieSpecBase();
    945         Cookie cookie = new Cookie("host1", "name", "value", "/", null, false);
    946         assertFalse(cookiespec.match("host2", 80, "/", false, cookie));
    947     }
    948
    949     public void testCookieMismatch2() throws Exception {
    950         CookieSpec cookiespec = new CookieSpecBase();
    951         Cookie cookie = new Cookie(".aaaaaaaaa.com", "name", "value", "/", null, false);
    952         assertFalse(cookiespec.match(".bbbbbbbb.com", 80, "/", false, cookie));
    953     }
    954
    955     public void testCookieMismatch3() throws Exception {
    956         CookieSpec cookiespec = new CookieSpecBase();
    957         Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, false);
    958         assertFalse(cookiespec.match("host", 80, "/foo", false, cookie));
    959     }
    960
    961     public void testCookieMismatch4() throws Exception {
    962         CookieSpec cookiespec = new CookieSpecBase();
    963         Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, true);
    964         assertFalse(cookiespec.match("host", 80, "/foobar/", false, cookie));
    965     }
    966
    967     public void testCookieMatch5() throws Exception {
    968         CookieSpec cookiespec = new CookieSpecBase();
    969         Cookie cookie = new Cookie("host", "name", "value", "/foobar/r", null, false);
    970         assertFalse(cookiespec.match("host", 80, "/foobar/", false, cookie));
    971     }
    972
    973     public void testCookieMismatch6() throws Exception {
    974         CookieSpec cookiespec = new CookieSpecBase();
    975         Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, true);
    976         assertFalse(cookiespec.match("host", 80, "/foobar", false, cookie));
    977     }
    978
    979     public void testMatchNullCookies() throws Exception {
    980         CookieSpec cookiespec = new CookieSpecBase();
    981         Cookie[] matched = cookiespec.match("host", 80, "/foobar", false, (Cookie[])null);
    982         assertNull(matched);
    983     }
    984
    985     public void testMatchedCookiesOrder() throws Exception {
    986         CookieSpec cookiespec = new CookieSpecBase();
    987         Cookie[] cookies = {
    988             new Cookie("host", "nomatch", "value", "/noway", null, false),
    989             new Cookie("host", "name2", "value", "/foobar/yada", null, false),
    990             new Cookie("host", "name3", "value", "/foobar", null, false),
    991             new Cookie("host", "name1", "value", "/foobar/yada/yada", null, false)};
    992         Cookie[] matched = cookiespec.match("host", 80, "/foobar/yada/yada", false, cookies);
    993         assertNotNull(matched);
    994         assertEquals(3, matched.length);
    995         assertEquals("name1", matched[0].getName());
    996         assertEquals("name2", matched[1].getName());
    997         assertEquals("name3", matched[2].getName());
    998     }
    999
    1000     public void testInvalidMatchDomain() throws Exception {
    1001         Cookie cookie = new Cookie("beta.gamma.com", "name", null, "/", null, false);
    1002         cookie.setDomainAttributeSpecified(true);
    1003         cookie.setPathAttributeSpecified(true);
    1004
    1005         CookieSpec cookiespec = new CookieSpecBase();
    1006         cookiespec.validate("alpha.beta.gamma.com", 80, "/", false, cookie);
    1007         assertTrue(cookiespec.match("alpha.beta.gamma.com", 80, "/", false, cookie));
    1008     }
    1009
    1010     public void testFormatInvalidCookie() throws Exception {
    1011         CookieSpec cookiespec = new CookieSpecBase();
    1012         try {
    1013             String s = cookiespec.formatCookie(null);
    1014             fail("IllegalArgumentException nust have been thrown");
    1015         } catch (IllegalArgumentException expected) {
    1016         }
    1017     }
    1018
    1019     /**
    1020      * Tests generic cookie formatting.
    1021      */
    1022     public void testGenericCookieFormatting() throws Exception {
    1023         Header header = new Header("Set-Cookie",
    1024             "name=value; path=/; domain=.mydomain.com");
    1025         CookieSpec cookiespec = new CookieSpecBase();
    1026         Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
    1027         cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
    1028         String s = cookiespec.formatCookie(cookies[0]);
    1029         assertEquals("name=value", s);
    1030     }
    1031
    1032     public void testGenericCookieFormattingAsHeader() throws Exception {
    1033         Header header = new Header("Set-Cookie",
    1034             "name=value; path=/; domain=.mydomain.com");
    1035         CookieSpec cookiespec = new CookieSpecBase();
    1036         Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
    1037         cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
    1038         Header cookieheader = cookiespec.formatCookieHeader(cookies[0]);
    1039         assertEquals("name=value", cookieheader.getValue());
    1040     }
    1041
    1042     /**
    1043      * Tests if null cookie values are handled correctly.
    1044      */
    1045     public void testNullCookieValueFormatting() {
    1046         Cookie cookie = new Cookie(".whatever.com", "name", null, "/", null, false);
    1047         cookie.setDomainAttributeSpecified(true);
    1048         cookie.setPathAttributeSpecified(true);
    1049
    1050         CookieSpec cookiespec = new CookieSpecBase();
    1051         String s = cookiespec.formatCookie(cookie);
    1052         assertEquals("name=", s);
    1053     }
    1054
    1055     public void testFormatInvalidCookies() throws Exception {
    1056         CookieSpec cookiespec = new CookieSpecBase();
    1057         try {
    1058             String s = cookiespec.formatCookies(null);
    1059             fail("IllegalArgumentException nust have been thrown");
    1060         } catch (IllegalArgumentException expected) {
    1061         }
    1062     }
    1063
    1064     public void testFormatZeroCookies() throws Exception {
    1065         CookieSpec cookiespec = new CookieSpecBase();
    1066         try {
    1067             String s = cookiespec.formatCookies(new Cookie[] {});
    1068             fail("IllegalArgumentException nust have been thrown");
    1069         } catch (IllegalArgumentException expected) {
    1070         }
    1071     }
    1072
    1073     /**
    1074      * Tests generic cookie formatting.
    1075      */
    1076     public void testFormatSeveralCookies() throws Exception {
    1077         Header header = new Header("Set-Cookie",
    1078             "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
    1079         CookieSpec cookiespec = new CookieSpecBase();
    1080         Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
    1081         String s = cookiespec.formatCookies(cookies);
    1082         assertEquals("name1=value1; name2=value2", s);
    1083     }
    1084
    1085     public void testFormatOneCookie() throws Exception {
    1086         Header header = new Header("Set-Cookie",
    1087             "name1=value1; path=/; domain=.mydomain.com;");
    1088         CookieSpec cookiespec = new CookieSpecBase();
    1089         Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
    1090         String s = cookiespec.formatCookies(cookies);
    1091         assertEquals("name1=value1", s);
    1092     }
    1093
    1094     public void testFormatSeveralCookiesAsHeader() throws Exception {
    1095         Header header = new Header("Set-Cookie",
    1096             "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
    1097         CookieSpec cookiespec = new CookieSpecBase();
    1098         Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
    1099         Header cookieheader = cookiespec.formatCookieHeader(cookies);
    1100         assertEquals("name1=value1; name2=value2", cookieheader.getValue());
    1101     }
    1102
    1103     public void testKeepCloverHappy() throws Exception {
    1104         MalformedCookieException ex1 = new MalformedCookieException();
    1105         MalformedCookieException ex2 = new MalformedCookieException("whatever");
    1106         MalformedCookieException ex3 = new MalformedCookieException("whatever", null);
    1107     }
    1108
    1109 }
    1110