    1   /*
    2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/cookie/TestCookieNetscapeDraft.java,v 1.2 2004/04/24 23:28:04 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 junit.framework.Test;
    33  import junit.framework.TestSuite;
    34
    35  import org.apache.commons.httpclient.Cookie;
    36  import org.apache.commons.httpclient.Header;
    37  import org.apache.commons.httpclient.HttpException;
    38  import org.apache.commons.httpclient.NameValuePair;
    39
    40
    41  /**
    42   * Test cases for Netscape cookie draft
    43   *
    44   * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
    45   *
    46   * @version $Revision: 480424 $
    47   */
    48  public class TestCookieNetscapeDraft extends TestCookieBase {
    49
    50      // ------------------------------------------------------------ Constructor
    51
    52      public TestCookieNetscapeDraft(String name) {
    53          super(name);
    54      }
    55
    56
    57      // ------------------------------------------------------- TestCase Methods
    58
    59
    60      public static Test suite() {
    61          return new TestSuite(TestCookieNetscapeDraft.class);
    62      }
    63
    64      public void testParseAttributeInvalidAttrib() throws Exception {
    65          CookieSpec cookiespec = new NetscapeDraftSpec();
    66          try {
    67              cookiespec.parseAttribute(null, null);
    68              fail("IllegalArgumentException must have been thrown");
    69          } catch (IllegalArgumentException expected) {
    70          }
    71      }
    72
    73      public void testParseAttributeInvalidCookie() throws Exception {
    74          CookieSpec cookiespec = new NetscapeDraftSpec();
    75          try {
    76              cookiespec.parseAttribute(new NameValuePair("name", "value"), null);
    77              fail("IllegalArgumentException must have been thrown");
    78          } catch (IllegalArgumentException expected) {
    79          }
    80      }
    81
    82      public void testParseAttributeInvalidCookieExpires() throws Exception {
    83          CookieSpec cookiespec = new NetscapeDraftSpec();
    84          Cookie cookie = new Cookie();
    85          try {
    86              cookiespec.parseAttribute(new NameValuePair("expires", null), cookie);
    87              fail("MalformedCookieException must have been thrown");
    88          } catch (MalformedCookieException expected) {
    89          }
    90      }
    91
    92      public void testParseWithNullHost() throws Exception {
    93          Header header = new Header("Set-Cookie",
    94              "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
    95
    96          CookieSpec cookiespec = new NetscapeDraftSpec();
    97          try {
    98              Cookie[] parsed = cookieParse(cookiespec, null, 80, "/", false, header);
    99              fail("IllegalArgumentException should have been thrown");
    100         } catch (IllegalArgumentException e) {
    101             // expected
    102         }
    103     }
    104
    105     public void testParseWithBlankHost() throws Exception {
    106         Header header = new Header("Set-Cookie",
    107             "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
    108
    109         CookieSpec cookiespec = new NetscapeDraftSpec();
    110         try {
    111             Cookie[] parsed = cookieParse(cookiespec, "  ", 80, "/", false, header);
    112             fail("IllegalArgumentException should have been thrown");
    113         } catch (IllegalArgumentException e) {
    114             // expected
    115         }
    116     }
    117
    118     public void testParseWithNullPath() throws Exception {
    119         Header header = new Header("Set-Cookie",
    120             "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
    121
    122         CookieSpec cookiespec = new NetscapeDraftSpec();
    123         try {
    124             Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, null, false, header);
    125             fail("IllegalArgumentException should have been thrown");
    126         } catch (IllegalArgumentException e) {
    127             // expected
    128         }
    129     }
    130
    131     public void testParseWithBlankPath() throws Exception {
    132         Header header = new Header("Set-Cookie",
    133             "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
    134
    135         CookieSpec cookiespec = new NetscapeDraftSpec();
    136         Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "  ", false, header);
    137         assertNotNull(parsed);
    138         assertEquals(1, parsed.length);
    139         assertEquals("/", parsed[0].getPath());
    140     }
    141
    142     public void testParseWithNegativePort() throws Exception {
    143         Header header = new Header("Set-Cookie",
    144             "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
    145
    146         CookieSpec cookiespec = new NetscapeDraftSpec();
    147         try {
    148             Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", -80, null, false, header);
    149             fail("IllegalArgumentException should have been thrown");
    150         } catch (IllegalArgumentException e) {
    151             // expected
    152         }
    153     }
    154
    155     public void testParseWithInvalidHeader1() throws Exception {
    156         CookieSpec cookiespec = new NetscapeDraftSpec();
    157         try {
    158             Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo", false, (String)null);
    159             fail("IllegalArgumentException should have been thrown.");
    160         } catch (IllegalArgumentException e) {
    161             // expected
    162         }
    163     }
    164
    165     public void testParseAbsPath() throws Exception {
    166         Header header = new Header("Set-Cookie", "name1=value1;Path=/path/");
    167
    168         CookieSpec cookiespec = new NetscapeDraftSpec();
    169         Cookie[] parsed = cookieParse(cookiespec, "host", 80, "/path/", true, header);
    170         assertEquals("Found 1 cookies.",1,parsed.length);
    171         assertEquals("Name","name1",parsed[0].getName());
    172         assertEquals("Value","value1",parsed[0].getValue());
    173         assertEquals("Domain","host",parsed[0].getDomain());
    174         assertEquals("Path","/path/",parsed[0].getPath());
    175     }
    176
    177     public void testParseAbsPath2() throws Exception {
    178         Header header = new Header("Set-Cookie", "name1=value1;Path=/");
    179
    180         CookieSpec cookiespec = new NetscapeDraftSpec();
    181         Cookie[] parsed = cookieParse(cookiespec, "host", 80, "/", true, header);
    182         assertEquals("Found 1 cookies.",1,parsed.length);
    183         assertEquals("Name","name1",parsed[0].getName());
    184         assertEquals("Value","value1",parsed[0].getValue());
    185         assertEquals("Domain","host",parsed[0].getDomain());
    186         assertEquals("Path","/",parsed[0].getPath());
    187     }
    188
    189     public void testParseRelativePath() throws Exception {
    190         Header header = new Header("Set-Cookie", "name1=value1;Path=whatever");
    191
    192         CookieSpec cookiespec = new NetscapeDraftSpec();
    193         Cookie[] parsed = cookieParse(cookiespec, "host", 80, "whatever", true, header);
    194         assertEquals("Found 1 cookies.",1,parsed.length);
    195         assertEquals("Name","name1",parsed[0].getName());
    196         assertEquals("Value","value1",parsed[0].getValue());
    197         assertEquals("Domain","host",parsed[0].getDomain());
    198         assertEquals("Path","whatever",parsed[0].getPath());
    199     }
    200
    201     public void testParseWithIllegalNetscapeDomain1() throws Exception {
    202         Header header = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com");
    203
    204         CookieSpec cookiespec = new NetscapeDraftSpec();
    205         try {
    206             Cookie[] parsed = cookieParse(cookiespec, "a.com", 80, "/", false, header);
    207             fail("HttpException exception should have been thrown");
    208         } catch (HttpException e) {
    209             // expected
    210         }
    211     }
    212
    213     public void testParseWithWrongNetscapeDomain2() throws Exception {
    214         Header header = new Header("Set-Cookie","cookie-name=cookie-value; domain=.y.z");
    215
    216         CookieSpec cookiespec = new NetscapeDraftSpec();
    217         try {
    218             Cookie[] parsed = cookieParse(cookiespec, "x.y.z", 80, "/", false, header);
    219             fail("HttpException exception should have been thrown");
    220         } catch (HttpException e) {
    221             // expected
    222         }
    223     }
    224
    225     /**
    226      * Tests Netscape specific cookie formatting.
    227      */
    228
    229     public void testNetscapeCookieFormatting() throws Exception {
    230         Header header = new Header(
    231           "Set-Cookie", "name=value; path=/; domain=.mydomain.com");
    232         CookieSpec cookiespec = new NetscapeDraftSpec();
    233         Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
    234         cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
    235         String s = cookiespec.formatCookie(cookies[0]);
    236         assertEquals("name=value", s);
    237     }
    238
    239     /**
    240      * Tests Netscape specific expire attribute parsing.
    241      */
    242     public void testNetscapeCookieExpireAttribute() throws Exception {
    243         CookieSpec cookiespec = new NetscapeDraftSpec();
    244         Header header = new Header("Set-Cookie",
    245             "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment");
    246         Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
    247         cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
    248         header = new Header("Set-Cookie",
    249             "name=value; path=/; domain=.mydomain.com; expires=Thu 01-Jan-2070 00:00:10 GMT; comment=no_comment");
    250         try {
    251             cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
    252             cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
    253             fail("MalformedCookieException must have been thrown");
    254         }
    255         catch (MalformedCookieException expected) {
    256         }
    257     }
    258
    259     /**
    260      * Tests Netscape specific expire attribute without a time zone.
    261      */
    262     public void testNetscapeCookieExpireAttributeNoTimeZone() throws Exception {
    263         CookieSpec cookiespec = new NetscapeDraftSpec();
    264         Header header = new Header("Set-Cookie",
    265             "name=value; expires=Thu, 01-Jan-2006 00:00:00 ");
    266         try {
    267             cookiespec.parse("myhost.mydomain.com", 80, "/", false, header );
    268             fail("MalformedCookieException should have been thrown");
    269         } catch (MalformedCookieException ex) {
    270             // expected
    271         }
    272     }
    273
    274     /**
    275      * Tests if cookie values with embedded comma are handled correctly.
    276      */
    277     public void testCookieWithComma() throws Exception {
    278         Header header = new Header("Set-Cookie", "a=b,c");
    279
    280         CookieSpec cookiespec = new NetscapeDraftSpec();
    281         Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
    282         assertEquals("number of cookies", 1, cookies.length);
    283         assertEquals("a", cookies[0].getName());
    284         assertEquals("b,c", cookies[0].getValue());
    285     }
    286
    287 }
    288