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.text.DateFormat;
31  import java.text.SimpleDateFormat;
32  import java.util.Arrays;
33  import java.util.Date;
34  import java.util.Locale;
35  
36  import org.apache.http.client.utils.DateUtils;
37  import org.apache.http.cookie.CookieAttributeHandler;
38  import org.apache.http.cookie.CookieOrigin;
39  import org.apache.http.cookie.MalformedCookieException;
40  import org.junit.Assert;
41  import org.junit.Test;
42  
43  public class TestBasicCookieAttribHandlers {
44  
45      @Test
46      public void testBasicDomainParse() throws Exception {
47          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
48          final CookieAttributeHandler h = new BasicDomainHandler();
49          h.parse(cookie, "www.somedomain.com");
50          Assert.assertEquals("www.somedomain.com", cookie.getDomain());
51      }
52  
53      @Test(expected=MalformedCookieException.class)
54      public void testBasicDomainParseInvalid1() throws Exception {
55          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
56          final CookieAttributeHandler h = new BasicDomainHandler();
57          h.parse(cookie, "");
58      }
59  
60      @Test(expected=MalformedCookieException.class)
61      public void testBasicDomainParseInvalid2() throws Exception {
62          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
63          final CookieAttributeHandler h = new BasicDomainHandler();
64          h.parse(cookie, null);
65      }
66  
67      @Test
68      public void testBasicDomainValidate1() throws Exception {
69          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
70          final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
71          final CookieAttributeHandler h = new BasicDomainHandler();
72  
73          cookie.setDomain(".somedomain.com");
74          h.validate(cookie, origin);
75  
76          cookie.setDomain(".otherdomain.com");
77          try {
78              h.validate(cookie, origin);
79              Assert.fail("MalformedCookieException should have been thrown");
80          } catch (final MalformedCookieException ex) {
81              // expected
82          }
83          cookie.setDomain("www.otherdomain.com");
84          try {
85              h.validate(cookie, origin);
86              Assert.fail("MalformedCookieException should have been thrown");
87          } catch (final MalformedCookieException ex) {
88              // expected
89          }
90      }
91  
92      @Test
93      public void testBasicDomainValidate2() throws Exception {
94          final BasicClientCookie cookie = new BasicClientCookie("name", "value");
95          final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false);
96          final CookieAttributeHandler h = new BasicDomainHandler();
97  
98          cookie.setDomain("somehost");
99          h.validate(cookie, origin);
100 
101         cookie.setDomain("otherhost");
102         try {
103             h.validate(cookie, origin);
104             Assert.fail("MalformedCookieException should have been thrown");
105         } catch (final MalformedCookieException ex) {
106             // expected
107         }
108     }
109 
110     @Test
111     public void testBasicDomainValidate3() throws Exception {
112         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
113         final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
114         final CookieAttributeHandler h = new BasicDomainHandler();
115 
116         cookie.setDomain(".somedomain.com");
117         h.validate(cookie, origin);
118     }
119 
120     @Test
121     public void testBasicDomainValidate4() throws Exception {
122         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
123         final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
124         final CookieAttributeHandler h = new BasicDomainHandler();
125 
126         cookie.setDomain(null);
127         try {
128             h.validate(cookie, origin);
129             Assert.fail("MalformedCookieException should have been thrown");
130         } catch (final MalformedCookieException ex) {
131             // expected
132         }
133     }
134 
135     @Test
136     public void testBasicDomainMatch1() throws Exception {
137         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
138         final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false);
139         final CookieAttributeHandler h = new BasicDomainHandler();
140 
141         cookie.setDomain("somedomain.com");
142         Assert.assertTrue(h.match(cookie, origin));
143 
144         cookie.setDomain(".somedomain.com");
145         Assert.assertTrue(h.match(cookie, origin));
146     }
147 
148     @Test
149     public void testBasicDomainMatch2() throws Exception {
150         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
151         final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false);
152         final CookieAttributeHandler h = new BasicDomainHandler();
153 
154         cookie.setDomain("somedomain.com");
155         Assert.assertTrue(h.match(cookie, origin));
156 
157         cookie.setDomain(".somedomain.com");
158         Assert.assertTrue(h.match(cookie, origin));
159 
160         cookie.setDomain(null);
161         Assert.assertFalse(h.match(cookie, origin));
162     }
163 
164     @Test
165     public void testBasicDomainInvalidInput() throws Exception {
166         final CookieAttributeHandler h = new BasicDomainHandler();
167         try {
168             h.parse(null, null);
169             Assert.fail("IllegalArgumentException must have been thrown");
170         } catch (final IllegalArgumentException ex) {
171             // expected
172         }
173         try {
174             h.validate(null, null);
175             Assert.fail("IllegalArgumentException must have been thrown");
176         } catch (final IllegalArgumentException ex) {
177             // expected
178         }
179         try {
180             h.validate(new BasicClientCookie("name", "value"), null);
181             Assert.fail("IllegalArgumentException must have been thrown");
182         } catch (final IllegalArgumentException ex) {
183             // expected
184         }
185         try {
186             h.match(null, null);
187             Assert.fail("IllegalArgumentException must have been thrown");
188         } catch (final IllegalArgumentException ex) {
189             // expected
190         }
191         try {
192             h.match(new BasicClientCookie("name", "value"), null);
193             Assert.fail("IllegalArgumentException must have been thrown");
194         } catch (final IllegalArgumentException ex) {
195             // expected
196         }
197     }
198 
199     @Test
200     public void testBasicPathParse() throws Exception {
201         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
202         final CookieAttributeHandler h = new BasicPathHandler();
203         h.parse(cookie, "stuff");
204         Assert.assertEquals("stuff", cookie.getPath());
205         h.parse(cookie, "");
206         Assert.assertEquals("/", cookie.getPath());
207         h.parse(cookie, null);
208         Assert.assertEquals("/", cookie.getPath());
209     }
210 
211     @Test
212     public void testBasicPathMatch1() throws Exception {
213         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
214         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
215         final CookieAttributeHandler h = new BasicPathHandler();
216         cookie.setPath("/stuff");
217         Assert.assertTrue(h.match(cookie, origin));
218     }
219 
220     @Test
221     public void testBasicPathMatch2() throws Exception {
222         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
223         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff/", false);
224         final CookieAttributeHandler h = new BasicPathHandler();
225         cookie.setPath("/stuff");
226         Assert.assertTrue(h.match(cookie, origin));
227     }
228 
229     @Test
230     public void testBasicPathMatch3() throws Exception {
231         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
232         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff/more-stuff", false);
233         final CookieAttributeHandler h = new BasicPathHandler();
234         cookie.setPath("/stuff");
235         Assert.assertTrue(h.match(cookie, origin));
236     }
237 
238     @Test
239     public void testBasicPathMatch4() throws Exception {
240         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
241         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuffed", false);
242         final CookieAttributeHandler h = new BasicPathHandler();
243         cookie.setPath("/stuff");
244         Assert.assertFalse(h.match(cookie, origin));
245     }
246 
247     @Test
248     public void testBasicPathMatch5() throws Exception {
249         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
250         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/otherstuff", false);
251         final CookieAttributeHandler h = new BasicPathHandler();
252         cookie.setPath("/stuff");
253         Assert.assertFalse(h.match(cookie, origin));
254     }
255 
256     @Test
257     public void testBasicPathMatch6() throws Exception {
258         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
259         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
260         final CookieAttributeHandler h = new BasicPathHandler();
261         cookie.setPath("/stuff/");
262         Assert.assertTrue(h.match(cookie, origin));
263     }
264 
265     @Test
266     public void testBasicPathMatch7() throws Exception {
267         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
268         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
269         final CookieAttributeHandler h = new BasicPathHandler();
270         Assert.assertTrue(h.match(cookie, origin));
271     }
272 
273     @Test
274     public void testBasicPathValidate() throws Exception {
275         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
276         final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
277         final CookieAttributeHandler h = new BasicPathHandler();
278         cookie.setPath("/stuff");
279         h.validate(cookie, origin);
280         cookie.setPath("/stuffed");
281         try {
282             h.validate(cookie, origin);
283             Assert.fail("MalformedCookieException must have been thrown");
284         } catch (final MalformedCookieException ex) {
285             // expected
286         }
287     }
288 
289     @Test
290     public void testBasicPathInvalidInput() throws Exception {
291         final CookieAttributeHandler h = new BasicPathHandler();
292         try {
293             h.parse(null, null);
294             Assert.fail("IllegalArgumentException must have been thrown");
295         } catch (final IllegalArgumentException ex) {
296             // expected
297         }
298         try {
299             h.match(null, null);
300             Assert.fail("IllegalArgumentException must have been thrown");
301         } catch (final IllegalArgumentException ex) {
302             // expected
303         }
304         try {
305             h.match(new BasicClientCookie("name", "value"), null);
306             Assert.fail("IllegalArgumentException must have been thrown");
307         } catch (final IllegalArgumentException ex) {
308             // expected
309         }
310     }
311 
312     @Test
313     public void testBasicMaxAgeParse() throws Exception {
314         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
315         final CookieAttributeHandler h = new BasicMaxAgeHandler();
316         h.parse(cookie, "2000");
317         Assert.assertNotNull(cookie.getExpiryDate());
318     }
319 
320     @Test
321     public void testBasicMaxAgeParseInvalid() throws Exception {
322         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
323         final CookieAttributeHandler h = new BasicMaxAgeHandler();
324         try {
325             h.parse(cookie, "garbage");
326             Assert.fail("MalformedCookieException must have been thrown");
327         } catch (final MalformedCookieException ex) {
328             // expected
329         }
330         try {
331             h.parse(cookie, null);
332             Assert.fail("MalformedCookieException must have been thrown");
333         } catch (final MalformedCookieException ex) {
334             // expected
335         }
336     }
337 
338     @Test
339     public void testBasicMaxAgeInvalidInput() throws Exception {
340         final CookieAttributeHandler h = new BasicMaxAgeHandler();
341         try {
342             h.parse(null, null);
343             Assert.fail("IllegalArgumentException must have been thrown");
344         } catch (final IllegalArgumentException ex) {
345             // expected
346         }
347     }
348 
349     @Test
350     public void testBasicCommentParse() throws Exception {
351         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
352         final CookieAttributeHandler h = new BasicCommentHandler();
353         h.parse(cookie, "whatever");
354         Assert.assertEquals("whatever", cookie.getComment());
355         h.parse(cookie, null);
356         Assert.assertEquals(null, cookie.getComment());
357     }
358 
359     @Test
360     public void testBasicCommentInvalidInput() throws Exception {
361         final CookieAttributeHandler h = new BasicCommentHandler();
362         try {
363             h.parse(null, null);
364             Assert.fail("IllegalArgumentException must have been thrown");
365         } catch (final IllegalArgumentException ex) {
366             // expected
367         }
368     }
369 
370     @Test
371     public void testBasicSecureParse() throws Exception {
372         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
373         final CookieAttributeHandler h = new BasicSecureHandler();
374         h.parse(cookie, "whatever");
375         Assert.assertTrue(cookie.isSecure());
376         h.parse(cookie, null);
377         Assert.assertTrue(cookie.isSecure());
378     }
379 
380     @Test
381     public void testBasicSecureMatch() throws Exception {
382         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
383         final CookieAttributeHandler h = new BasicSecureHandler();
384 
385         final CookieOrigin origin1 = new CookieOrigin("somehost", 80, "/stuff", false);
386         cookie.setSecure(false);
387         Assert.assertTrue(h.match(cookie, origin1));
388         cookie.setSecure(true);
389         Assert.assertFalse(h.match(cookie, origin1));
390 
391         final CookieOrigin origin2 = new CookieOrigin("somehost", 80, "/stuff", true);
392         cookie.setSecure(false);
393         Assert.assertTrue(h.match(cookie, origin2));
394         cookie.setSecure(true);
395         Assert.assertTrue(h.match(cookie, origin2));
396     }
397 
398     @Test
399     public void testBasicSecureInvalidInput() throws Exception {
400         final CookieAttributeHandler h = new BasicSecureHandler();
401         try {
402             h.parse(null, null);
403             Assert.fail("IllegalArgumentException must have been thrown");
404         } catch (final IllegalArgumentException ex) {
405             // expected
406         }
407         try {
408             h.match(null, null);
409             Assert.fail("IllegalArgumentException must have been thrown");
410         } catch (final IllegalArgumentException ex) {
411             // expected
412         }
413         try {
414             h.match(new BasicClientCookie("name", "value"), null);
415             Assert.fail("IllegalArgumentException must have been thrown");
416         } catch (final IllegalArgumentException ex) {
417             // expected
418         }
419     }
420 
421     @Test
422     public void testBasicExpiresParse() throws Exception {
423         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
424         final CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
425 
426         final DateFormat dateformat = new SimpleDateFormat(DateUtils.PATTERN_RFC1123, Locale.US);
427         dateformat.setTimeZone(DateUtils.GMT);
428 
429         final Date now = new Date();
430 
431         h.parse(cookie, dateformat.format(now));
432         Assert.assertNotNull(cookie.getExpiryDate());
433     }
434 
435     @Test
436     public void testBasicExpiresParseInvalid() throws Exception {
437         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
438         final CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
439         try {
440             h.parse(cookie, "garbage");
441             Assert.fail("MalformedCookieException must have been thrown");
442         } catch (final MalformedCookieException ex) {
443             // expected
444         }
445         try {
446             h.parse(cookie, null);
447             Assert.fail("MalformedCookieException must have been thrown");
448         } catch (final MalformedCookieException ex) {
449             // expected
450         }
451     }
452 
453     @SuppressWarnings("unused")
454     @Test
455     public void testBasicExpiresInvalidInput() throws Exception {
456         try {
457             new BasicExpiresHandler(null);
458             Assert.fail("IllegalArgumentException must have been thrown");
459         } catch (final IllegalArgumentException ex) {
460             // expected
461         }
462         final CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
463         try {
464             h.parse(null, null);
465             Assert.fail("IllegalArgumentException must have been thrown");
466         } catch (final IllegalArgumentException ex) {
467             // expected
468         }
469     }
470 
471     @Test
472     public void testPublicSuffixFilter() throws Exception {
473         final BasicClientCookie cookie = new BasicClientCookie("name", "value");
474 
475         final PublicSuffixFilter h = new PublicSuffixFilter(new RFC2109DomainHandler());
476         h.setPublicSuffixes(Arrays.asList(new String[] { "co.uk", "com" }));
477 
478         cookie.setDomain(".co.uk");
479         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.co.uk", 80, "/stuff", false)));
480 
481         cookie.setDomain("co.uk");
482         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.co.uk", 80, "/stuff", false)));
483 
484         cookie.setDomain(".com");
485         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.com", 80, "/stuff", false)));
486 
487         cookie.setDomain("com");
488         Assert.assertFalse(h.match(cookie, new CookieOrigin("apache.com", 80, "/stuff", false)));
489 
490         cookie.setDomain("localhost");
491         Assert.assertTrue(h.match(cookie, new CookieOrigin("localhost", 80, "/stuff", false)));
492     }
493 
494 }