View Javadoc
1   /*
2    * ====================================================================
3    * Licensed to the Apache Software Foundation (ASF) under one
4    * or more contributor license agreements.  See the NOTICE file
5    * distributed with this work for additional information
6    * regarding copyright ownership.  The ASF licenses this file
7    * to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance
9    * with the License.  You may obtain a copy of the License at
10   *
11   *   http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing,
14   * software distributed under the License is distributed on an
15   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16   * KIND, either express or implied.  See the License for the
17   * specific language governing permissions and limitations
18   * under the License.
19   * ====================================================================
20   *
21   * This software consists of voluntary contributions made by many
22   * individuals on behalf of the Apache Software Foundation.  For more
23   * information on the Apache Software Foundation, please see
24   * <http://www.apache.org/>.
25   *
26   */
27  
28  package org.apache.http.impl.cookie;
29  
30  import org.apache.http.cookie.CookieAttributeHandler;
31  import org.apache.http.cookie.CookieOrigin;
32  import org.apache.http.cookie.MalformedCookieException;
33  import org.junit.Assert;
34  import org.junit.Test;
35  
36  public class TestNetscapeCookieAttribHandlers {
37  
38      @Test
39      public void testNetscapeDomainValidate1() throws Exception {
40          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
41          final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
42          final CookieAttributeHandler h = new NetscapeDomainHandler();
43  
44          cookie.setDomain("somehost");
45          h.validate(cookie, origin);
46  
47          cookie.setDomain("otherhost");
48          try {
49              h.validate(cookie, origin);
50              Assert.fail("MalformedCookieException should have been thrown");
51          } catch (final MalformedCookieException ex) {
52              // expected
53          }
54      }
55  
56      @Test
57      public void testNetscapeDomainValidate2() throws Exception {
58          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
59          final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
60          final CookieAttributeHandler h = new NetscapeDomainHandler();
61  
62          cookie.setDomain(".somedomain.com");
63          h.validate(cookie, origin);
64  
65          cookie.setDomain(".otherdomain.com");
66          try {
67              h.validate(cookie, origin);
68              Assert.fail("MalformedCookieException should have been thrown");
69          } catch (final MalformedCookieException ex) {
70              // expected
71          }
72          cookie.setDomain("www.otherdomain.com");
73          try {
74              h.validate(cookie, origin);
75              Assert.fail("MalformedCookieException should have been thrown");
76          } catch (final MalformedCookieException ex) {
77              // expected
78          }
79      }
80  
81      @Test
82      public void testNetscapeDomainValidate3() throws Exception {
83          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
84          final CookieOrigin origin = new CookieOrigin("www.a.com", 80, "/", false);
85          final CookieAttributeHandler h = new NetscapeDomainHandler();
86  
87          cookie.setDomain(".a.com");
88          h.validate(cookie, origin);
89  
90          cookie.setDomain(".com");
91          try {
92              h.validate(cookie, origin);
93              Assert.fail("MalformedCookieException should have been thrown");
94          } catch (final MalformedCookieException ex) {
95              // expected
96          }
97      }
98  
99      @Test
100     public void testNetscapeDomainValidate4() throws Exception {
101         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
102         final CookieOrigin origin = new CookieOrigin("www.a.b.c", 80, "/", false);
103         final CookieAttributeHandler h = new NetscapeDomainHandler();
104 
105         cookie.setDomain(".a.b.c");
106         h.validate(cookie, origin);
107 
108         cookie.setDomain(".b.c");
109         try {
110             h.validate(cookie, origin);
111             Assert.fail("MalformedCookieException should have been thrown");
112         } catch (final MalformedCookieException ex) {
113             // expected
114         }
115     }
116 
117     @Test
118     public void testNetscapeDomainMatch1() throws Exception {
119         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
120         final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
121         final CookieAttributeHandler h = new NetscapeDomainHandler();
122 
123         cookie.setDomain(null);
124         Assert.assertFalse(h.match(cookie, origin));
125 
126         cookie.setDomain(".somedomain.com");
127         Assert.assertTrue(h.match(cookie, origin));
128     }
129 
130     @Test
131     public void testNetscapeDomainMatch2() throws Exception {
132         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
133         final CookieOrigin origin = new CookieOrigin("www.whatever.somedomain.com", 80, "/", false);
134         final CookieAttributeHandler h = new NetscapeDomainHandler();
135 
136         cookie.setDomain(".somedomain.com");
137         Assert.assertTrue(h.match(cookie, origin));
138     }
139 
140     @Test
141     public void testNetscapeDomainInvalidInput() throws Exception {
142         final CookieAttributeHandler h = new NetscapeDomainHandler();
143         try {
144             h.match(null, null);
145             Assert.fail("IllegalArgumentException must have been thrown");
146         } catch (final IllegalArgumentException ex) {
147             // expected
148         }
149         try {
150             h.match(new BasicClientCookie("name", "value"), null);
151             Assert.fail("IllegalArgumentException must have been thrown");
152         } catch (final IllegalArgumentException ex) {
153             // expected
154         }
155     }
156 
157 }