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 java.util.ArrayList;
31  import java.util.Calendar;
32  import java.util.List;
33  import java.util.TimeZone;
34  
35  import org.apache.http.Header;
36  import org.apache.http.cookie.ClientCookie;
37  import org.apache.http.cookie.Cookie;
38  import org.apache.http.cookie.CookieOrigin;
39  import org.apache.http.cookie.CookieSpec;
40  import org.apache.http.cookie.MalformedCookieException;
41  import org.apache.http.message.BasicHeader;
42  import org.junit.Assert;
43  import org.junit.Test;
44  
45  /**
46   * Test cases for Netscape cookie draft
47   */
48  public class TestCookieNetscapeDraft {
49  
50      @Test
51      public void testParseAbsPath() throws Exception {
52          final Header header = new BasicHeader("Set-Cookie", "name1=value1;Path=/path/");
53  
54          final CookieSpec cookiespec = new NetscapeDraftSpec();
55          final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
56          final List<Cookie> cookies = cookiespec.parse(header, origin);
57          for (int i = 0; i < cookies.size(); i++) {
58              cookiespec.validate(cookies.get(i), origin);
59          }
60          Assert.assertEquals("Found 1 cookies.",1,cookies.size());
61          Assert.assertEquals("Name","name1",cookies.get(0).getName());
62          Assert.assertEquals("Value","value1",cookies.get(0).getValue());
63          Assert.assertEquals("Domain","host",cookies.get(0).getDomain());
64          Assert.assertEquals("Path","/path/",cookies.get(0).getPath());
65      }
66  
67      @Test
68      public void testParseAbsPath2() throws Exception {
69          final Header header = new BasicHeader("Set-Cookie", "name1=value1;Path=/");
70  
71          final CookieSpec cookiespec = new NetscapeDraftSpec();
72          final CookieOrigin origin = new CookieOrigin("host", 80, "/", true);
73          final List<Cookie> cookies = cookiespec.parse(header, origin);
74          for (int i = 0; i < cookies.size(); i++) {
75              cookiespec.validate(cookies.get(i), origin);
76          }
77          Assert.assertEquals("Found 1 cookies.",1,cookies.size());
78          Assert.assertEquals("Name","name1",cookies.get(0).getName());
79          Assert.assertEquals("Value","value1",cookies.get(0).getValue());
80          Assert.assertEquals("Domain","host",cookies.get(0).getDomain());
81          Assert.assertEquals("Path","/",cookies.get(0).getPath());
82      }
83  
84      @Test
85      public void testParseRelativePath() throws Exception {
86          final Header header = new BasicHeader("Set-Cookie", "name1=value1;Path=whatever");
87  
88          final CookieSpec cookiespec = new NetscapeDraftSpec();
89          final CookieOrigin origin = new CookieOrigin("host", 80, "whatever", true);
90          final List<Cookie> cookies = cookiespec.parse(header, origin);
91          for (int i = 0; i < cookies.size(); i++) {
92              cookiespec.validate(cookies.get(i), origin);
93          }
94          Assert.assertEquals("Found 1 cookies.",1,cookies.size());
95          Assert.assertEquals("Name","name1",cookies.get(0).getName());
96          Assert.assertEquals("Value","value1",cookies.get(0).getValue());
97          Assert.assertEquals("Domain","host",cookies.get(0).getDomain());
98          Assert.assertEquals("Path","whatever",cookies.get(0).getPath());
99      }
100 
101     @Test
102     public void testParseWithIllegalNetscapeDomain1() throws Exception {
103         final Header header = new BasicHeader("Set-Cookie","cookie-name=cookie-value; domain=.com");
104 
105         final CookieSpec cookiespec = new NetscapeDraftSpec();
106         try {
107             final CookieOrigin origin = new CookieOrigin("a.com", 80, "/", false);
108             final List<Cookie> cookies = cookiespec.parse(header, origin);
109             for (int i = 0; i < cookies.size(); i++) {
110                 cookiespec.validate(cookies.get(i), origin);
111             }
112             Assert.fail("MalformedCookieException exception should have been thrown");
113         } catch (final MalformedCookieException e) {
114             // expected
115         }
116     }
117 
118     @Test
119     public void testParseWithWrongNetscapeDomain2() throws Exception {
120         final Header header = new BasicHeader("Set-Cookie","cookie-name=cookie-value; domain=.y.z");
121 
122         final CookieSpec cookiespec = new NetscapeDraftSpec();
123         try {
124             final CookieOrigin origin = new CookieOrigin("x.y.z", 80, "/", false);
125             final List<Cookie> cookies = cookiespec.parse(header, origin);
126             for (int i = 0; i < cookies.size(); i++) {
127                 cookiespec.validate(cookies.get(i), origin);
128             }
129             Assert.fail("MalformedCookieException exception should have been thrown");
130         } catch (final MalformedCookieException e) {
131             // expected
132         }
133     }
134 
135     @Test
136     public void testParseVersionIgnored() throws Exception {
137         final Header header = new BasicHeader("Set-Cookie", "name1=value1;Path=/path/;Version=1;");
138 
139         final CookieSpec cookiespec = new NetscapeDraftSpec();
140         final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true);
141         final List<Cookie> cookies = cookiespec.parse(header, origin);
142         for (int i = 0; i < cookies.size(); i++) {
143             cookiespec.validate(cookies.get(i), origin);
144         }
145         Assert.assertEquals("Found 1 cookies.",1,cookies.size());
146         final Cookie cookie = cookies.get(0);
147         Assert.assertEquals("Name","name1", cookie.getName());
148         Assert.assertEquals("Value", "value1", cookie.getValue());
149         Assert.assertEquals("Domain", "host", cookie.getDomain());
150         Assert.assertEquals("Path","/path/", cookie.getPath());
151         Assert.assertEquals(0, cookie.getVersion());
152     }
153 
154     /**
155      * Tests Netscape specific cookie formatting.
156      */
157     @Test
158     public void testNetscapeCookieFormatting() throws Exception {
159         final Header header = new BasicHeader(
160           "Set-Cookie", "name=value; path=/; domain=.mydomain.com");
161         final CookieSpec cookiespec = new NetscapeDraftSpec();
162         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
163         final List<Cookie> cookies = cookiespec.parse(header, origin);
164         cookiespec.validate(cookies.get(0), origin);
165         final List<Header> headers = cookiespec.formatCookies(cookies);
166         Assert.assertEquals(1, headers.size());
167         Assert.assertEquals("name=value", headers.get(0).getValue());
168     }
169 
170     /**
171      * Tests Netscape specific expire attribute parsing.
172      */
173     @Test
174     public void testNetscapeCookieExpireAttribute() throws Exception {
175         final CookieSpec cookiespec = new NetscapeDraftSpec();
176         final Header header = new BasicHeader("Set-Cookie",
177             "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment");
178         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
179         final List<Cookie> cookies = cookiespec.parse(header, origin);
180         cookiespec.validate(cookies.get(0), origin);
181         Assert.assertNotNull(cookies);
182         Assert.assertEquals(1, cookies.size());
183         final Cookie cookie = cookies.get(0);
184         final Calendar c = Calendar.getInstance();
185         c.setTimeZone(TimeZone.getTimeZone("GMT"));
186         c.setTime(cookie.getExpiryDate());
187         final int year = c.get(Calendar.YEAR);
188         Assert.assertEquals(2070, year);
189     }
190 
191     /**
192      * Expire attribute with two digit year.
193      */
194     @Test
195     public void testNetscapeCookieExpireAttributeTwoDigitYear() throws Exception {
196         final CookieSpec cookiespec = new NetscapeDraftSpec();
197         final Header header = new BasicHeader("Set-Cookie",
198             "name=value; path=/; domain=.mydomain.com; expires=Thursday, 01-Jan-70 00:00:10 GMT; comment=no_comment");
199         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
200         final List<Cookie> cookies = cookiespec.parse(header, origin);
201         cookiespec.validate(cookies.get(0), origin);
202         Assert.assertNotNull(cookies);
203         Assert.assertEquals(1, cookies.size());
204         final Cookie cookie = cookies.get(0);
205         final Calendar c = Calendar.getInstance();
206         c.setTimeZone(TimeZone.getTimeZone("GMT"));
207         c.setTime(cookie.getExpiryDate());
208         final int year = c.get(Calendar.YEAR);
209         Assert.assertEquals(2070, year);
210     }
211 
212     /**
213      * Invalid expire attribute.
214      */
215     @Test
216     public void testNetscapeCookieInvalidExpireAttribute() throws Exception {
217         final CookieSpec cookiespec = new NetscapeDraftSpec();
218         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
219         final Header header = new BasicHeader("Set-Cookie",
220             "name=value; path=/; domain=.mydomain.com; expires=Thu 01-Jan-2070 00:00:10 GMT; comment=no_comment");
221         try {
222             final List<Cookie> cookies = cookiespec.parse(header, origin);
223             cookiespec.validate(cookies.get(0), origin);
224             Assert.fail("MalformedCookieException exception should have been thrown");
225         } catch (final MalformedCookieException e) {
226             // expected
227         }
228     }
229 
230     /**
231      * Tests Netscape specific expire attribute without a time zone.
232      */
233     @Test
234     public void testNetscapeCookieExpireAttributeNoTimeZone() throws Exception {
235         final CookieSpec cookiespec = new NetscapeDraftSpec();
236         final Header header = new BasicHeader("Set-Cookie",
237             "name=value; expires=Thu, 01-Jan-2006 00:00:00 ");
238         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
239         try {
240             cookiespec.parse(header, origin);
241             Assert.fail("MalformedCookieException should have been thrown");
242         } catch (final MalformedCookieException ex) {
243             // expected
244         }
245     }
246 
247     /**
248      * Tests if cookie values with embedded comma are handled correctly.
249      */
250     @Test
251     public void testCookieWithComma() throws Exception {
252         final Header header = new BasicHeader("Set-Cookie", "a=b,c");
253 
254         final CookieSpec cookiespec = new NetscapeDraftSpec();
255         final CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
256         final List<Cookie> cookies = cookiespec.parse(header, origin);
257         Assert.assertEquals("number of cookies", 1, cookies.size());
258         Assert.assertEquals("a", cookies.get(0).getName());
259         Assert.assertEquals("b,c", cookies.get(0).getValue());
260     }
261 
262     @Test
263     public void testFormatCookies() throws Exception {
264         final BasicClientCookie c1 = new BasicClientCookie("name1", "value1");
265         c1.setDomain(".whatever.com");
266         c1.setAttribute(ClientCookie.DOMAIN_ATTR, c1.getDomain());
267         c1.setPath("/");
268         c1.setAttribute(ClientCookie.PATH_ATTR, c1.getPath());
269 
270         final Cookie c2 = new BasicClientCookie("name2", "value2");
271         final Cookie c3 = new BasicClientCookie("name3", null);
272 
273         final CookieSpec cookiespec = new NetscapeDraftSpec();
274         final List<Cookie> cookies = new ArrayList<Cookie>();
275         cookies.add(c1);
276         cookies.add(c2);
277         cookies.add(c3);
278         final List<Header> headers = cookiespec.formatCookies(cookies);
279         Assert.assertNotNull(headers);
280         Assert.assertEquals(1, headers.size());
281         Assert.assertEquals("name1=value1; name2=value2; name3", headers.get(0).getValue());
282     }
283 
284     @Test
285     public void testInvalidInput() throws Exception {
286         final CookieSpec cookiespec = new NetscapeDraftSpec();
287         try {
288             cookiespec.parse(null, null);
289             Assert.fail("IllegalArgumentException must have been thrown");
290         } catch (final IllegalArgumentException ex) {
291             // expected
292         }
293         try {
294             cookiespec.parse(new BasicHeader("Set-Cookie", "name=value"), null);
295             Assert.fail("IllegalArgumentException must have been thrown");
296         } catch (final IllegalArgumentException ex) {
297             // expected
298         }
299         try {
300             cookiespec.formatCookies(null);
301             Assert.fail("IllegalArgumentException must have been thrown");
302         } catch (final IllegalArgumentException ex) {
303             // expected
304         }
305         try {
306             final List<Cookie> cookies = new ArrayList<Cookie>();
307             cookiespec.formatCookies(cookies);
308             Assert.fail("IllegalArgumentException must have been thrown");
309         } catch (final IllegalArgumentException ex) {
310             // expected
311         }
312     }
313 
314 }
315