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