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