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     /**
136      * Tests Netscape specific cookie formatting.
137      */
138     @Test
139     public void testNetscapeCookieFormatting() throws Exception {
140         final Header header = new BasicHeader(
141           "Set-Cookie", "name=value; path=/; domain=.mydomain.com");
142         final CookieSpec cookiespec = new NetscapeDraftSpec();
143         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
144         final List<Cookie> cookies = cookiespec.parse(header, origin);
145         cookiespec.validate(cookies.get(0), origin);
146         final List<Header> headers = cookiespec.formatCookies(cookies);
147         Assert.assertEquals(1, headers.size());
148         Assert.assertEquals("name=value", headers.get(0).getValue());
149     }
150 
151     /**
152      * Tests Netscape specific expire attribute parsing.
153      */
154     @Test
155     public void testNetscapeCookieExpireAttribute() throws Exception {
156         final CookieSpec cookiespec = new NetscapeDraftSpec();
157         final Header header = new BasicHeader("Set-Cookie",
158             "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment");
159         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
160         final List<Cookie> cookies = cookiespec.parse(header, origin);
161         cookiespec.validate(cookies.get(0), origin);
162         Assert.assertNotNull(cookies);
163         Assert.assertEquals(1, cookies.size());
164         final Cookie cookie = cookies.get(0);
165         final Calendar c = Calendar.getInstance();
166         c.setTimeZone(TimeZone.getTimeZone("GMT"));
167         c.setTime(cookie.getExpiryDate());
168         final int year = c.get(Calendar.YEAR);
169         Assert.assertEquals(2070, year);
170     }
171 
172     /**
173      * Expire attribute with two digit year.
174      */
175     @Test
176     public void testNetscapeCookieExpireAttributeTwoDigitYear() throws Exception {
177         final CookieSpec cookiespec = new NetscapeDraftSpec();
178         final Header header = new BasicHeader("Set-Cookie",
179             "name=value; path=/; domain=.mydomain.com; expires=Thursday, 01-Jan-70 00:00:10 GMT; comment=no_comment");
180         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
181         final List<Cookie> cookies = cookiespec.parse(header, origin);
182         cookiespec.validate(cookies.get(0), origin);
183         Assert.assertNotNull(cookies);
184         Assert.assertEquals(1, cookies.size());
185         final Cookie cookie = cookies.get(0);
186         final Calendar c = Calendar.getInstance();
187         c.setTimeZone(TimeZone.getTimeZone("GMT"));
188         c.setTime(cookie.getExpiryDate());
189         final int year = c.get(Calendar.YEAR);
190         Assert.assertEquals(2070, year);
191     }
192 
193     /**
194      * Invalid expire attribute.
195      */
196     @Test
197     public void testNetscapeCookieInvalidExpireAttribute() throws Exception {
198         final CookieSpec cookiespec = new NetscapeDraftSpec();
199         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
200         final Header header = new BasicHeader("Set-Cookie",
201             "name=value; path=/; domain=.mydomain.com; expires=Thu 01-Jan-2070 00:00:10 GMT; comment=no_comment");
202         try {
203             final List<Cookie> cookies = cookiespec.parse(header, origin);
204             cookiespec.validate(cookies.get(0), origin);
205             Assert.fail("MalformedCookieException exception should have been thrown");
206         } catch (final MalformedCookieException e) {
207             // expected
208         }
209     }
210 
211     /**
212      * Tests Netscape specific expire attribute without a time zone.
213      */
214     @Test
215     public void testNetscapeCookieExpireAttributeNoTimeZone() throws Exception {
216         final CookieSpec cookiespec = new NetscapeDraftSpec();
217         final Header header = new BasicHeader("Set-Cookie",
218             "name=value; expires=Thu, 01-Jan-2006 00:00:00 ");
219         final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
220         try {
221             cookiespec.parse(header, origin);
222             Assert.fail("MalformedCookieException should have been thrown");
223         } catch (final MalformedCookieException ex) {
224             // expected
225         }
226     }
227 
228     /**
229      * Tests if cookie values with embedded comma are handled correctly.
230      */
231     @Test
232     public void testCookieWithComma() throws Exception {
233         final Header header = new BasicHeader("Set-Cookie", "a=b,c");
234 
235         final CookieSpec cookiespec = new NetscapeDraftSpec();
236         final CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
237         final List<Cookie> cookies = cookiespec.parse(header, origin);
238         Assert.assertEquals("number of cookies", 1, cookies.size());
239         Assert.assertEquals("a", cookies.get(0).getName());
240         Assert.assertEquals("b,c", cookies.get(0).getValue());
241     }
242 
243     @Test
244     public void testFormatCookies() throws Exception {
245         final BasicClientCookie c1 = new BasicClientCookie("name1", "value1");
246         c1.setDomain(".whatever.com");
247         c1.setAttribute(ClientCookie.DOMAIN_ATTR, c1.getDomain());
248         c1.setPath("/");
249         c1.setAttribute(ClientCookie.PATH_ATTR, c1.getPath());
250 
251         final Cookie c2 = new BasicClientCookie("name2", "value2");
252         final Cookie c3 = new BasicClientCookie("name3", null);
253 
254         final CookieSpec cookiespec = new NetscapeDraftSpec();
255         final List<Cookie> cookies = new ArrayList<Cookie>();
256         cookies.add(c1);
257         cookies.add(c2);
258         cookies.add(c3);
259         final List<Header> headers = cookiespec.formatCookies(cookies);
260         Assert.assertNotNull(headers);
261         Assert.assertEquals(1, headers.size());
262         Assert.assertEquals("name1=value1; name2=value2; name3", headers.get(0).getValue());
263     }
264 
265     @Test
266     public void testInvalidInput() throws Exception {
267         final CookieSpec cookiespec = new NetscapeDraftSpec();
268         try {
269             cookiespec.parse(null, null);
270             Assert.fail("IllegalArgumentException must have been thrown");
271         } catch (final IllegalArgumentException ex) {
272             // expected
273         }
274         try {
275             cookiespec.parse(new BasicHeader("Set-Cookie", "name=value"), null);
276             Assert.fail("IllegalArgumentException must have been thrown");
277         } catch (final IllegalArgumentException ex) {
278             // expected
279         }
280         try {
281             cookiespec.formatCookies(null);
282             Assert.fail("IllegalArgumentException must have been thrown");
283         } catch (final IllegalArgumentException ex) {
284             // expected
285         }
286         try {
287             final List<Cookie> cookies = new ArrayList<Cookie>();
288             cookiespec.formatCookies(cookies);
289             Assert.fail("IllegalArgumentException must have been thrown");
290         } catch (final IllegalArgumentException ex) {
291             // expected
292         }
293     }
294 
295 }
296