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
29 package org.apache.commons.httpclient.cookie;
30
31 import java.util.Collection;
32 import java.util.Date;
33
34 import junit.framework.Test;
35 import junit.framework.TestSuite;
36
37 import org.apache.commons.httpclient.Cookie;
38 import org.apache.commons.httpclient.Header;
39 import org.apache.commons.httpclient.HttpException;
40 import org.apache.commons.httpclient.HttpState;
41 import org.apache.commons.httpclient.NameValuePair;
42 import org.apache.commons.httpclient.params.DefaultHttpParamsFactory;
43 import org.apache.commons.httpclient.params.HttpMethodParams;
44 import org.apache.commons.httpclient.params.HttpParams;
45
46
47 /***
48 * Test cases for Cookie
49 *
50 * @author BC Holmes
51 * @author Rod Waldhoff
52 * @author dIon Gillard
53 * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a>
54 * @author Marc A. Saegesser
55 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a>
56 * @version $Revision: 240442 $
57 */
58 public class TestCookieCompatibilitySpec extends TestCookieBase {
59
60
61
62
63
64 public TestCookieCompatibilitySpec(String name) {
65 super(name);
66 }
67
68
69
70
71
72 public static Test suite() {
73 return new TestSuite(TestCookieCompatibilitySpec.class);
74 }
75
76 public void testParseAttributeInvalidAttrib() throws Exception {
77 CookieSpec cookiespec = new CookieSpecBase();
78 try {
79 cookiespec.parseAttribute(null, null);
80 fail("IllegalArgumentException must have been thrown");
81 } catch (IllegalArgumentException expected) {
82 }
83 }
84
85 public void testParseAttributeInvalidCookie() throws Exception {
86 CookieSpec cookiespec = new CookieSpecBase();
87 try {
88 cookiespec.parseAttribute(new NameValuePair("name", "value"), null);
89 fail("IllegalArgumentException must have been thrown");
90 } catch (IllegalArgumentException expected) {
91 }
92 }
93
94 public void testParseAttributeNullPath() throws Exception {
95 CookieSpec cookiespec = new CookieSpecBase();
96 Cookie cookie = new Cookie();
97 cookiespec.parseAttribute(new NameValuePair("path", null), cookie);
98 assertEquals("/", cookie.getPath());
99 }
100
101 public void testParseAttributeBlankPath() throws Exception {
102 CookieSpec cookiespec = new CookieSpecBase();
103 Cookie cookie = new Cookie();
104 cookiespec.parseAttribute(new NameValuePair("path", " "), cookie);
105 assertEquals("/", cookie.getPath());
106 }
107
108 public void testParseAttributeNullDomain() throws Exception {
109 CookieSpec cookiespec = new CookieSpecBase();
110 Cookie cookie = new Cookie();
111 try {
112 cookiespec.parseAttribute(new NameValuePair("domain", null), cookie);
113 fail("MalformedCookieException must have been thrown");
114 } catch (MalformedCookieException expected) {
115 }
116 }
117
118 public void testParseAttributeBlankDomain() throws Exception {
119 CookieSpec cookiespec = new CookieSpecBase();
120 Cookie cookie = new Cookie();
121 try {
122 cookiespec.parseAttribute(new NameValuePair("domain", " "), cookie);
123 fail("MalformedCookieException must have been thrown");
124 } catch (MalformedCookieException expected) {
125 }
126 }
127
128 public void testParseAttributeNullMaxAge() throws Exception {
129 CookieSpec cookiespec = new CookieSpecBase();
130 Cookie cookie = new Cookie();
131 try {
132 cookiespec.parseAttribute(new NameValuePair("max-age", null), cookie);
133 fail("MalformedCookieException must have been thrown");
134 } catch (MalformedCookieException expected) {
135 }
136 }
137
138 public void testParseAttributeInvalidMaxAge() throws Exception {
139 CookieSpec cookiespec = new CookieSpecBase();
140 Cookie cookie = new Cookie();
141 try {
142 cookiespec.parseAttribute(new NameValuePair("max-age", "crap"), cookie);
143 fail("MalformedCookieException must have been thrown");
144 } catch (MalformedCookieException expected) {
145 }
146 }
147
148 public void testParseAttributeNullExpires() throws Exception {
149 CookieSpec cookiespec = new CookieSpecBase();
150 Cookie cookie = new Cookie();
151 try {
152 cookiespec.parseAttribute(new NameValuePair("expires", null), cookie);
153 fail("MalformedCookieException must have been thrown");
154 } catch (MalformedCookieException expected) {
155 }
156 }
157
158 public void testParseAttributeUnknownValue() throws Exception {
159 CookieSpec cookiespec = new CookieSpecBase();
160 Cookie cookie = new Cookie();
161 cookiespec.parseAttribute(new NameValuePair("nonsense", null), cookie);
162 }
163
164 public void testValidateNullHost() throws Exception {
165 CookieSpec cookiespec = new CookieSpecBase();
166 Cookie cookie = new Cookie();
167 try {
168 cookiespec.validate(null, 80, "/", false, cookie);
169 fail("IllegalArgumentException must have been thrown");
170 } catch (IllegalArgumentException expected) {
171 }
172 }
173
174 public void testValidateBlankHost() throws Exception {
175 CookieSpec cookiespec = new CookieSpecBase();
176 Cookie cookie = new Cookie();
177 try {
178 cookiespec.validate(" ", 80, "/", false, cookie);
179 fail("IllegalArgumentException must have been thrown");
180 } catch (IllegalArgumentException expected) {
181 }
182 }
183
184 public void testValidateNullPath() throws Exception {
185 CookieSpec cookiespec = new CookieSpecBase();
186 Cookie cookie = new Cookie();
187 try {
188 cookiespec.validate("host", 80, null, false, cookie);
189 fail("IllegalArgumentException must have been thrown");
190 } catch (IllegalArgumentException expected) {
191 }
192 }
193
194 public void testValidateBlankPath() throws Exception {
195 CookieSpec cookiespec = new CookieSpecBase();
196 Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
197 cookiespec.validate("host", 80, " ", false, cookie);
198 }
199
200 public void testValidateInvalidPort() throws Exception {
201 CookieSpec cookiespec = new CookieSpecBase();
202 Cookie cookie = new Cookie();
203 try {
204 cookiespec.validate("host", -80, "/", false, cookie);
205 fail("IllegalArgumentException must have been thrown");
206 } catch (IllegalArgumentException expected) {
207 }
208 }
209
210 public void testValidateInvalidCookieVersion() throws Exception {
211 CookieSpec cookiespec = new CookieSpecBase();
212 Cookie cookie = new Cookie();
213 cookie.setVersion(-1);
214 try {
215 cookiespec.validate("host", 80, "/", false, cookie);
216 fail("MalformedCookieException must have been thrown");
217 } catch (MalformedCookieException expected) {
218 }
219 }
220
221 /***
222 * Tests whether domain attribute check is case-insensitive.
223 */
224 public void testDomainCaseInsensitivity() throws Exception {
225 Header header = new Header("Set-Cookie",
226 "name=value; path=/; domain=.whatever.com");
227
228 CookieSpec cookiespec = new CookieSpecBase();
229 Cookie[] parsed = cookieParse(cookiespec, "www.WhatEver.com", 80, "/", false, header);
230 assertNotNull(parsed);
231 assertEquals(1, parsed.length);
232 assertEquals(".whatever.com", parsed[0].getDomain());
233 }
234
235 /***
236 * Test basic parse (with various spacings
237 */
238 public void testParse1() throws Exception {
239 String headerValue = "custno = 12345; comment=test; version=1," +
240 " name=John; version=1; max-age=600; secure; domain=.apache.org";
241
242 Header header = new Header("set-cookie", headerValue);
243
244 CookieSpec cookiespec = new CookieSpecBase();
245 Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
246 assertEquals(2, cookies.length);
247
248 assertEquals("custno", cookies[0].getName());
249 assertEquals("12345", cookies[0].getValue());
250 assertEquals("test", cookies[0].getComment());
251 assertEquals(0, cookies[0].getVersion());
252 assertEquals("www.apache.org", cookies[0].getDomain());
253 assertEquals("/", cookies[0].getPath());
254 assertFalse(cookies[0].getSecure());
255
256 assertEquals("name", cookies[1].getName());
257 assertEquals("John", cookies[1].getValue());
258 assertEquals(null, cookies[1].getComment());
259 assertEquals(0, cookies[1].getVersion());
260 assertEquals(".apache.org", cookies[1].getDomain());
261 assertEquals("/", cookies[1].getPath());
262 assertTrue(cookies[1].getSecure());
263 }
264
265
266 /***
267 * Test no spaces
268 */
269 public void testParse2() throws Exception {
270 String headerValue = "custno=12345;comment=test; version=1," +
271 "name=John;version=1;max-age=600;secure;domain=.apache.org";
272
273 Header header = new Header("set-cookie", headerValue);
274
275 CookieSpec cookiespec = new CookieSpecBase();
276 Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
277
278 assertEquals(2, cookies.length);
279
280 assertEquals("custno", cookies[0].getName());
281 assertEquals("12345", cookies[0].getValue());
282 assertEquals("test", cookies[0].getComment());
283 assertEquals(0, cookies[0].getVersion());
284 assertEquals("www.apache.org", cookies[0].getDomain());
285 assertEquals("/", cookies[0].getPath());
286 assertFalse(cookies[0].getSecure());
287
288 assertEquals("name", cookies[1].getName());
289 assertEquals("John", cookies[1].getValue());
290 assertEquals(null, cookies[1].getComment());
291 assertEquals(0, cookies[1].getVersion());
292 assertEquals(".apache.org", cookies[1].getDomain());
293 assertEquals("/", cookies[1].getPath());
294 assertTrue(cookies[1].getSecure());
295 }
296
297
298 /***
299 * Test parse with quoted text
300 */
301 public void testParse3() throws Exception {
302 String headerValue =
303 "name=\"Doe, John\";version=1;max-age=600;secure;domain=.apache.org";
304 Header header = new Header("set-cookie", headerValue);
305
306 CookieSpec cookiespec = new CookieSpecBase();
307 Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
308
309 assertEquals(1, cookies.length);
310
311 assertEquals("name", cookies[0].getName());
312 assertEquals("Doe, John", cookies[0].getValue());
313 assertEquals(null, cookies[0].getComment());
314 assertEquals(0, cookies[0].getVersion());
315 assertEquals(".apache.org", cookies[0].getDomain());
316 assertEquals("/", cookies[0].getPath());
317 assertTrue(cookies[0].getSecure());
318 }
319
320
321
322 public void testQuotedExpiresAttribute() throws Exception {
323 String headerValue = "custno=12345;Expires='Thu, 01-Jan-2070 00:00:10 GMT'";
324
325 Header header = new Header("set-cookie", headerValue);
326
327 CookieSpec cookiespec = new CookieSpecBase();
328 Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", true, header);
329 assertNotNull("Expected some cookies",cookies);
330 assertEquals("Expected 1 cookie",1,cookies.length);
331 assertNotNull("Expected cookie to have getExpiryDate",cookies[0].getExpiryDate());
332 }
333
334 public void testSecurityError() throws Exception {
335 String headerValue = "custno=12345;comment=test; version=1," +
336 "name=John;version=1;max-age=600;secure;domain=jakarta.apache.org";
337 Header header = new Header("set-cookie", headerValue);
338
339 CookieSpec cookiespec = new CookieSpecBase();
340 try {
341 Cookie[] cookies = cookieParse(cookiespec, "www.apache.org", 80, "/", false, header);
342 fail("HttpException exception should have been thrown");
343 } catch (HttpException e) {
344
345 }
346 }
347
348 public void testParseSimple() throws Exception {
349 Header header = new Header("Set-Cookie","cookie-name=cookie-value");
350
351 CookieSpec cookiespec = new CookieSpecBase();
352 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path/path", false, header);
353 assertEquals("Found 1 cookie.",1,parsed.length);
354 assertEquals("Name","cookie-name",parsed[0].getName());
355 assertEquals("Value","cookie-value",parsed[0].getValue());
356 assertTrue("Comment",null == parsed[0].getComment());
357 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
358
359 assertTrue("isPersistent",!parsed[0].isPersistent());
360 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
361 assertEquals("Path","/path",parsed[0].getPath());
362 assertTrue("Secure",!parsed[0].getSecure());
363 assertEquals("Version",0,parsed[0].getVersion());
364 }
365
366 public void testParseSimple2() throws Exception {
367 Header header = new Header("Set-Cookie", "cookie-name=cookie-value");
368
369 CookieSpec cookiespec = new CookieSpecBase();
370 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path", false, header);
371 assertEquals("Found 1 cookie.", 1, parsed.length);
372 assertEquals("Name", "cookie-name", parsed[0].getName());
373 assertEquals("Value", "cookie-value", parsed[0].getValue());
374 assertTrue("Comment", null == parsed[0].getComment());
375 assertTrue("ExpiryDate", null == parsed[0].getExpiryDate());
376
377 assertTrue("isPersistent", !parsed[0].isPersistent());
378 assertEquals("Domain", "127.0.0.1", parsed[0].getDomain());
379 assertEquals("Path", "/", parsed[0].getPath());
380 assertTrue("Secure", !parsed[0].getSecure());
381 assertEquals("Version", 0, parsed[0].getVersion());
382 }
383
384
385 public void testParseNoValue() throws Exception {
386 Header header = new Header("Set-Cookie","cookie-name=");
387
388 CookieSpec cookiespec = new CookieSpecBase();
389 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
390 assertEquals("Found 1 cookie.",1,parsed.length);
391 assertEquals("Name","cookie-name",parsed[0].getName());
392 assertEquals("Value", "", parsed[0].getValue());
393 assertTrue("Comment",null == parsed[0].getComment());
394 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
395
396 assertTrue("isPersistent",!parsed[0].isPersistent());
397 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
398 assertEquals("Path","/",parsed[0].getPath());
399 assertTrue("Secure",!parsed[0].getSecure());
400 assertEquals("Version",0,parsed[0].getVersion());
401 }
402
403 public void testParseWithWhiteSpace() throws Exception {
404 Header header = new Header("Set-Cookie"," cookie-name = cookie-value ");
405
406 CookieSpec cookiespec = new CookieSpecBase();
407 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
408 assertEquals("Found 1 cookie.",1,parsed.length);
409 assertEquals("Name","cookie-name",parsed[0].getName());
410 assertEquals("Value","cookie-value",parsed[0].getValue());
411 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
412 assertEquals("Path","/",parsed[0].getPath());
413 assertTrue("Secure",!parsed[0].getSecure());
414 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
415 assertTrue("Comment",null == parsed[0].getComment());
416 }
417
418 public void testParseWithQuotes() throws Exception {
419 Header header = new Header("Set-Cookie"," cookie-name = \" cookie-value \" ;path=/");
420
421 CookieSpec cookiespec = new CookieSpecBase();
422 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1",80, "/", false, header);
423 assertEquals("Found 1 cookie.",1,parsed.length);
424 assertEquals("Name","cookie-name",parsed[0].getName());
425 assertEquals("Value"," cookie-value ",parsed[0].getValue());
426 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
427 assertEquals("Path","/",parsed[0].getPath());
428 assertTrue("Secure",!parsed[0].getSecure());
429 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
430 assertTrue("Comment",null == parsed[0].getComment());
431 }
432
433 public void testParseWithPath() throws Exception {
434 Header header = new Header("Set-Cookie","cookie-name=cookie-value; Path=/path/");
435
436 CookieSpec cookiespec = new CookieSpecBase();
437 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1",80, "/path/path", false, header);
438 assertEquals("Found 1 cookie.",1,parsed.length);
439 assertEquals("Name","cookie-name",parsed[0].getName());
440 assertEquals("Value","cookie-value",parsed[0].getValue());
441 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
442 assertEquals("Path","/path/",parsed[0].getPath());
443 assertTrue("Secure",!parsed[0].getSecure());
444 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
445 assertTrue("Comment",null == parsed[0].getComment());
446 }
447
448 public void testParseWithDomain() throws Exception {
449 Header header = new Header("Set-Cookie","cookie-name=cookie-value; Domain=127.0.0.1");
450
451 CookieSpec cookiespec = new CookieSpecBase();
452 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
453 assertEquals("Found 1 cookie.",1,parsed.length);
454 assertEquals("Name","cookie-name",parsed[0].getName());
455 assertEquals("Value","cookie-value",parsed[0].getValue());
456 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
457 assertEquals("Path","/",parsed[0].getPath());
458 assertTrue("Secure",!parsed[0].getSecure());
459 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
460 assertTrue("Comment",null == parsed[0].getComment());
461 }
462
463 public void testParseWithSecure() throws Exception {
464 Header header = new Header("Set-Cookie","cookie-name=cookie-value; secure");
465
466 CookieSpec cookiespec = new CookieSpecBase();
467 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
468 assertEquals("Found 1 cookie.",1,parsed.length);
469 assertEquals("Name","cookie-name",parsed[0].getName());
470 assertEquals("Value","cookie-value",parsed[0].getValue());
471 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
472 assertEquals("Path","/",parsed[0].getPath());
473 assertTrue("Secure",parsed[0].getSecure());
474 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
475 assertTrue("Comment",null == parsed[0].getComment());
476 }
477
478 public void testParseWithComment() throws Exception {
479 Header header = new Header("Set-Cookie",
480 "cookie-name=cookie-value; comment=\"This is a comment.\"");
481
482 CookieSpec cookiespec = new CookieSpecBase();
483 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
484 assertEquals("Found 1 cookie.",1,parsed.length);
485 assertEquals("Name","cookie-name",parsed[0].getName());
486 assertEquals("Value","cookie-value",parsed[0].getValue());
487 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
488 assertEquals("Path","/",parsed[0].getPath());
489 assertTrue("Secure",!parsed[0].getSecure());
490 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
491 assertEquals("Comment","This is a comment.",parsed[0].getComment());
492 }
493
494 public void testParseWithExpires() throws Exception {
495 Header header = new Header("Set-Cookie",
496 "cookie-name=cookie-value;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
497
498 CookieSpec cookiespec = new CookieSpecBase();
499 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
500 assertEquals("Found 1 cookie.",1,parsed.length);
501 assertEquals("Name","cookie-name",parsed[0].getName());
502 assertEquals("Value","cookie-value",parsed[0].getValue());
503 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
504 assertEquals("Path","/",parsed[0].getPath());
505 assertTrue("Secure",!parsed[0].getSecure());
506 assertEquals(new Date(10000L),parsed[0].getExpiryDate());
507 assertTrue("Comment",null == parsed[0].getComment());
508 }
509
510 public void testParseWithAll() throws Exception {
511 Header header = new Header("Set-Cookie",
512 "cookie-name=cookie-value;Version=1;Path=/commons;Domain=.apache.org;" +
513 "Comment=This is a comment.;secure;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
514
515 CookieSpec cookiespec = new CookieSpecBase();
516 Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
517 assertEquals("Found 1 cookie.",1,parsed.length);
518 assertEquals("Name","cookie-name",parsed[0].getName());
519 assertEquals("Value","cookie-value",parsed[0].getValue());
520 assertEquals("Domain",".apache.org",parsed[0].getDomain());
521 assertEquals("Path","/commons",parsed[0].getPath());
522 assertTrue("Secure",parsed[0].getSecure());
523 assertEquals(new Date(10000L),parsed[0].getExpiryDate());
524 assertEquals("Comment","This is a comment.",parsed[0].getComment());
525 assertEquals("Version",0,parsed[0].getVersion());
526 }
527
528 public void testParseMultipleDifferentPaths() throws Exception {
529 Header header = new Header("Set-Cookie",
530 "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;" +
531 "Path=/commons/httpclient;Version=1");
532
533 CookieSpec cookiespec = new CookieSpecBase();
534 Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
535 HttpState state = new HttpState();
536 state.addCookies(parsed);
537 Cookie[] cookies = state.getCookies();
538 assertEquals("Wrong number of cookies.",2,cookies.length);
539 assertEquals("Name","name1",cookies[0].getName());
540 assertEquals("Value","value1",cookies[0].getValue());
541 assertEquals("Name","name1",cookies[1].getName());
542 assertEquals("Value","value2",cookies[1].getValue());
543 }
544
545 public void testParseMultipleSamePaths() throws Exception {
546 Header header = new Header("Set-Cookie",
547 "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons");
548
549 CookieSpec cookiespec = new CookieSpecBase();
550 Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
551 HttpState state = new HttpState();
552 state.addCookies(parsed);
553 Cookie[] cookies = state.getCookies();
554 assertEquals("Found 1 cookies.",1,cookies.length);
555 assertEquals("Name","name1",cookies[0].getName());
556 assertEquals("Value","value2",cookies[0].getValue());
557 }
558
559 public void testParseRelativePath() throws Exception {
560 Header header = new Header("Set-Cookie", "name1=value1;Path=whatever");
561
562 CookieSpec cookiespec = new CookieSpecBase();
563 Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "whatever", true, header);
564 assertEquals("Found 1 cookies.",1,parsed.length);
565 assertEquals("Name","name1",parsed[0].getName());
566 assertEquals("Value","value1",parsed[0].getValue());
567 assertEquals("Path","whatever",parsed[0].getPath());
568 }
569
570 public void testParseWithWrongDomain() throws Exception {
571 Header header = new Header("Set-Cookie",
572 "cookie-name=cookie-value; domain=127.0.0.1; version=1");
573
574 CookieSpec cookiespec = new CookieSpecBase();
575 try {
576 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.2", 80, "/", false, header);
577 fail("HttpException exception should have been thrown");
578 } catch (HttpException e) {
579
580 }
581 }
582
583 public void testParseWithNullHost() throws Exception {
584 Header header = new Header("Set-Cookie",
585 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
586
587 CookieSpec cookiespec = new CookieSpecBase();
588 try {
589 Cookie[] parsed = cookieParse(cookiespec, null, 80, "/", false, header);
590 fail("IllegalArgumentException should have been thrown");
591 } catch (IllegalArgumentException e) {
592
593 }
594 }
595
596 public void testParseWithBlankHost() throws Exception {
597 Header header = new Header("Set-Cookie",
598 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
599
600 CookieSpec cookiespec = new CookieSpecBase();
601 try {
602 Cookie[] parsed = cookieParse(cookiespec, " ", 80, "/", false, header);
603 fail("IllegalArgumentException should have been thrown");
604 } catch (IllegalArgumentException e) {
605
606 }
607 }
608
609 public void testParseWithNullPath() throws Exception {
610 Header header = new Header("Set-Cookie",
611 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
612
613 CookieSpec cookiespec = new CookieSpecBase();
614 try {
615 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, null, false, header);
616 fail("IllegalArgumentException should have been thrown");
617 } catch (IllegalArgumentException e) {
618
619 }
620 }
621
622 public void testParseWithBlankPath() throws Exception {
623 Header header = new Header("Set-Cookie",
624 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
625
626 CookieSpec cookiespec = new CookieSpecBase();
627 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, " ", false, header);
628 assertNotNull(parsed);
629 assertEquals(1, parsed.length);
630 assertEquals("/", parsed[0].getPath());
631 }
632
633 public void testParseWithNegativePort() throws Exception {
634 Header header = new Header("Set-Cookie",
635 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
636
637 CookieSpec cookiespec = new CookieSpecBase();
638 try {
639 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", -80, null, false, header);
640 fail("IllegalArgumentException should have been thrown");
641 } catch (IllegalArgumentException e) {
642
643 }
644 }
645
646 public void testParseWithNullHostAndPath() throws Exception {
647 Header header = new Header("Set-Cookie",
648 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
649
650 CookieSpec cookiespec = new CookieSpecBase();
651 try {
652 Cookie[] parsed = cookieParse(cookiespec, null, 80, null, false, header);
653 fail("IllegalArgumentException should have been thrown");
654 } catch (IllegalArgumentException e) {
655
656 }
657 }
658
659 public void testParseWithPathMismatch() throws Exception {
660 Header header = new Header("Set-Cookie",
661 "cookie-name=cookie-value; path=/path/path/path");
662
663 CookieSpec cookiespec = new CookieSpecBase();
664 try {
665 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path", false, header);
666 fail("MalformedCookieException should have been thrown.");
667 } catch (MalformedCookieException e) {
668
669 }
670 }
671
672 public void testParseWithPathMismatch2() throws Exception {
673 Header header = new Header("Set-Cookie",
674 "cookie-name=cookie-value; path=/foobar");
675
676 CookieSpec cookiespec = new CookieSpecBase();
677 try {
678 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/foo", false, header);
679 fail("MalformedCookieException should have been thrown.");
680 } catch (MalformedCookieException e) {
681
682 }
683 }
684
685
686 public void testParseWithInvalidHeader1() throws Exception {
687 CookieSpec cookiespec = new CookieSpecBase();
688 try {
689 Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo", false, (Header)null);
690 fail("IllegalArgumentException should have been thrown.");
691 } catch (IllegalArgumentException e) {
692
693 }
694 }
695
696 public void testParseWithInvalidHeader2() throws Exception {
697 CookieSpec cookiespec = new CookieSpecBase();
698 try {
699 Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo", false, (String)null);
700 fail("IllegalArgumentException should have been thrown.");
701 } catch (IllegalArgumentException e) {
702
703 }
704 }
705
706 /***
707 * Tests if cookie constructor rejects cookie name containing blanks.
708 */
709 public void testCookieNameWithBlanks() throws Exception {
710 Header setcookie = new Header("Set-Cookie", "invalid name=");
711 CookieSpec cookiespec = new CookieSpecBase();
712 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, setcookie);
713 assertNotNull(parsed);
714 assertEquals(1, parsed.length);
715 }
716
717
718 /***
719 * Tests if cookie constructor rejects cookie name starting with $.
720 */
721 public void testCookieNameStartingWithDollarSign() throws Exception {
722 Header setcookie = new Header("Set-Cookie", "$invalid_name=");
723 CookieSpec cookiespec = new CookieSpecBase();
724 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, setcookie);
725 assertNotNull(parsed);
726 assertEquals(1, parsed.length);
727 }
728
729
730 /***
731 * Tests if malformatted expires attribute is parsed correctly.
732 */
733 public void testCookieWithComma() throws Exception {
734 Header header = new Header("Set-Cookie", "name=value; expires=\"Thu, 01-Jan-1970 00:00:00 GMT");
735
736 CookieSpec cookiespec = new CookieSpecBase();
737 try {
738 Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
739 fail("MalformedCookieException should have been thrown");
740 } catch (MalformedCookieException expected) {
741 }
742 }
743
744
745 /***
746 * Tests several date formats.
747 */
748 public void testDateFormats() throws Exception {
749
750 checkDate("Thu, 01-Jan-70 00:00:10 GMT");
751 checkDate("Thu, 01-Jan-2070 00:00:10 GMT");
752
753 checkDate("Thu 01-Jan-70 00:00:10 GMT");
754 checkDate("Thu 01-Jan-2070 00:00:10 GMT");
755
756 checkDate("Thu, 01 Jan 70 00:00:10 GMT");
757 checkDate("Thu, 01 Jan 2070 00:00:10 GMT");
758
759 checkDate("Thu 01 Jan 70 00:00:10 GMT");
760 checkDate("Thu 01 Jan 2070 00:00:10 GMT");
761
762 checkDate("Wed, 20-Nov-2002 09-38-33 GMT");
763
764
765 try {
766 checkDate("this aint a date");
767 fail("Date check is bogous");
768 } catch(Exception e) {
769
770 }
771 }
772
773 private void checkDate(String date) throws Exception {
774 Header header = new Header("Set-Cookie", "custno=12345;Expires='"+date+"';");
775 HttpParams params = new DefaultHttpParamsFactory().getDefaultParams();
776 CookieSpec cookiespec = new CookieSpecBase();
777 cookiespec.setValidDateFormats(
778 (Collection)params.getParameter(HttpMethodParams.DATE_PATTERNS));
779 cookieParse(cookiespec, "localhost", 80, "/", false, header);
780 }
781
782 /***
783 * Tests if invalid second domain level cookie gets accepted in the
784 * browser compatibility mode.
785 */
786 public void testSecondDomainLevelCookie() throws Exception {
787 Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
788 cookie.setDomainAttributeSpecified(true);
789 cookie.setPathAttributeSpecified(true);
790
791 CookieSpec cookiespec = new CookieSpecBase();
792 cookiespec.validate("sourceforge.net", 80, "/", false, cookie);
793 }
794
795 public void testSecondDomainLevelCookieMatch1() throws Exception {
796 Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
797 cookie.setDomainAttributeSpecified(true);
798 cookie.setPathAttributeSpecified(true);
799
800 CookieSpec cookiespec = new CookieSpecBase();
801 assertTrue(cookiespec.match("sourceforge.net", 80, "/", false, cookie));
802 }
803
804 public void testSecondDomainLevelCookieMatch2() throws Exception {
805 Cookie cookie = new Cookie("sourceforge.net", "name", null, "/", null, false);
806 cookie.setDomainAttributeSpecified(true);
807 cookie.setPathAttributeSpecified(true);
808
809 CookieSpec cookiespec = new CookieSpecBase();
810 assertTrue(cookiespec.match("www.sourceforge.net", 80, "/", false, cookie));
811 }
812
813 public void testSecondDomainLevelCookieMatch3() throws Exception {
814 Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
815 cookie.setDomainAttributeSpecified(true);
816 cookie.setPathAttributeSpecified(true);
817
818 CookieSpec cookiespec = new CookieSpecBase();
819 assertTrue(cookiespec.match("www.sourceforge.net", 80, "/", false, cookie));
820 }
821
822 public void testInvalidSecondDomainLevelCookieMatch1() throws Exception {
823 Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
824 cookie.setDomainAttributeSpecified(true);
825 cookie.setPathAttributeSpecified(true);
826
827 CookieSpec cookiespec = new CookieSpecBase();
828 assertFalse(cookiespec.match("antisourceforge.net", 80, "/", false, cookie));
829 }
830
831 public void testInvalidSecondDomainLevelCookieMatch2() throws Exception {
832 Cookie cookie = new Cookie("sourceforge.net", "name", null, "/", null, false);
833 cookie.setDomainAttributeSpecified(true);
834 cookie.setPathAttributeSpecified(true);
835
836 CookieSpec cookiespec = new CookieSpecBase();
837 assertFalse(cookiespec.match("antisourceforge.net", 80, "/", false, cookie));
838 }
839
840 public void testMatchNullHost() throws Exception {
841 CookieSpec cookiespec = new CookieSpecBase();
842 Cookie cookie = new Cookie();
843 try {
844 cookiespec.match(null, 80, "/", false, cookie);
845 fail("IllegalArgumentException must have been thrown");
846 } catch (IllegalArgumentException expected) {
847 }
848 }
849
850 public void testMatchBlankHost() throws Exception {
851 CookieSpec cookiespec = new CookieSpecBase();
852 Cookie cookie = new Cookie();
853 try {
854 cookiespec.match(" ", 80, "/", false, cookie);
855 fail("IllegalArgumentException must have been thrown");
856 } catch (IllegalArgumentException expected) {
857 }
858 }
859
860 public void testMatchInvalidPort() throws Exception {
861 CookieSpec cookiespec = new CookieSpecBase();
862 Cookie cookie = new Cookie();
863 try {
864 cookiespec.match("host", -80, "/", false, cookie);
865 fail("IllegalArgumentException must have been thrown");
866 } catch (IllegalArgumentException expected) {
867 }
868 }
869
870 public void testMatchNullPath() throws Exception {
871 CookieSpec cookiespec = new CookieSpecBase();
872 Cookie cookie = new Cookie();
873 try {
874 cookiespec.match("host", 80, null, false, cookie);
875 fail("IllegalArgumentException must have been thrown");
876 } catch (IllegalArgumentException expected) {
877 }
878 }
879
880 public void testMatchBlankPath() throws Exception {
881 CookieSpec cookiespec = new CookieSpecBase();
882 Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
883 assertTrue(cookiespec.match("host", 80, " ", false, cookie));
884 }
885
886 public void testMatchNullCookie() throws Exception {
887 CookieSpec cookiespec = new CookieSpecBase();
888 try {
889 cookiespec.match("host", 80, "/", false, (Cookie)null);
890 fail("IllegalArgumentException must have been thrown");
891 } catch (IllegalArgumentException expected) {
892 }
893 }
894
895 public void testMatchNullCookieDomain() throws Exception {
896 CookieSpec cookiespec = new CookieSpecBase();
897 Cookie cookie = new Cookie(null, "name", "value", "/", null, false);
898 assertFalse(cookiespec.match("host", 80, "/", false, cookie));
899 }
900
901 public void testMatchNullCookiePath() throws Exception {
902 CookieSpec cookiespec = new CookieSpecBase();
903 Cookie cookie = new Cookie("host", "name", "value", null, null, false);
904 assertFalse(cookiespec.match("host", 80, "/", false, cookie));
905 }
906
907 public void testCookieMatch1() throws Exception {
908 CookieSpec cookiespec = new CookieSpecBase();
909 Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
910 assertTrue(cookiespec.match("host", 80, "/", false, cookie));
911 }
912
913 public void testCookieMatch2() throws Exception {
914 CookieSpec cookiespec = new CookieSpecBase();
915 Cookie cookie = new Cookie(".whatever.com", "name", "value", "/", null, false);
916 assertTrue(cookiespec.match(".whatever.com", 80, "/", false, cookie));
917 }
918
919 public void testCookieMatch3() throws Exception {
920 CookieSpec cookiespec = new CookieSpecBase();
921 Cookie cookie = new Cookie(".whatever.com", "name", "value", "/", null, false);
922 assertTrue(cookiespec.match(".really.whatever.com", 80, "/", false, cookie));
923 }
924
925 public void testCookieMatch4() throws Exception {
926 CookieSpec cookiespec = new CookieSpecBase();
927 Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
928 assertTrue(cookiespec.match("host", 80, "/foobar", false, cookie));
929 }
930
931 public void testCookieMismatch1() throws Exception {
932 CookieSpec cookiespec = new CookieSpecBase();
933 Cookie cookie = new Cookie("host1", "name", "value", "/", null, false);
934 assertFalse(cookiespec.match("host2", 80, "/", false, cookie));
935 }
936
937 public void testCookieMismatch2() throws Exception {
938 CookieSpec cookiespec = new CookieSpecBase();
939 Cookie cookie = new Cookie(".aaaaaaaaa.com", "name", "value", "/", null, false);
940 assertFalse(cookiespec.match(".bbbbbbbb.com", 80, "/", false, cookie));
941 }
942
943 public void testCookieMismatch3() throws Exception {
944 CookieSpec cookiespec = new CookieSpecBase();
945 Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, false);
946 assertFalse(cookiespec.match("host", 80, "/foo", false, cookie));
947 }
948
949 public void testCookieMismatch4() throws Exception {
950 CookieSpec cookiespec = new CookieSpecBase();
951 Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, true);
952 assertFalse(cookiespec.match("host", 80, "/foobar/", false, cookie));
953 }
954
955 public void testCookieMatch5() throws Exception {
956 CookieSpec cookiespec = new CookieSpecBase();
957 Cookie cookie = new Cookie("host", "name", "value", "/foobar/r", null, false);
958 assertFalse(cookiespec.match("host", 80, "/foobar/", false, cookie));
959 }
960
961 public void testCookieMismatch6() throws Exception {
962 CookieSpec cookiespec = new CookieSpecBase();
963 Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, true);
964 assertFalse(cookiespec.match("host", 80, "/foobar", false, cookie));
965 }
966
967 public void testMatchNullCookies() throws Exception {
968 CookieSpec cookiespec = new CookieSpecBase();
969 Cookie[] matched = cookiespec.match("host", 80, "/foobar", false, (Cookie[])null);
970 assertNull(matched);
971 }
972
973 public void testMatchedCookiesOrder() throws Exception {
974 CookieSpec cookiespec = new CookieSpecBase();
975 Cookie[] cookies = {
976 new Cookie("host", "nomatch", "value", "/noway", null, false),
977 new Cookie("host", "name2", "value", "/foobar/yada", null, false),
978 new Cookie("host", "name3", "value", "/foobar", null, false),
979 new Cookie("host", "name1", "value", "/foobar/yada/yada", null, false)};
980 Cookie[] matched = cookiespec.match("host", 80, "/foobar/yada/yada", false, cookies);
981 assertNotNull(matched);
982 assertEquals(3, matched.length);
983 assertEquals("name1", matched[0].getName());
984 assertEquals("name2", matched[1].getName());
985 assertEquals("name3", matched[2].getName());
986 }
987
988 public void testInvalidMatchDomain() throws Exception {
989 Cookie cookie = new Cookie("beta.gamma.com", "name", null, "/", null, false);
990 cookie.setDomainAttributeSpecified(true);
991 cookie.setPathAttributeSpecified(true);
992
993 CookieSpec cookiespec = new CookieSpecBase();
994 cookiespec.validate("alpha.beta.gamma.com", 80, "/", false, cookie);
995 assertTrue(cookiespec.match("alpha.beta.gamma.com", 80, "/", false, cookie));
996 }
997
998 public void testFormatInvalidCookie() throws Exception {
999 CookieSpec cookiespec = new CookieSpecBase();
1000 try {
1001 String s = cookiespec.formatCookie(null);
1002 fail("IllegalArgumentException nust have been thrown");
1003 } catch (IllegalArgumentException expected) {
1004 }
1005 }
1006
1007 /***
1008 * Tests generic cookie formatting.
1009 */
1010 public void testGenericCookieFormatting() throws Exception {
1011 Header header = new Header("Set-Cookie",
1012 "name=value; path=/; domain=.mydomain.com");
1013 CookieSpec cookiespec = new CookieSpecBase();
1014 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1015 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
1016 String s = cookiespec.formatCookie(cookies[0]);
1017 assertEquals("name=value", s);
1018 }
1019
1020 public void testGenericCookieFormattingAsHeader() throws Exception {
1021 Header header = new Header("Set-Cookie",
1022 "name=value; path=/; domain=.mydomain.com");
1023 CookieSpec cookiespec = new CookieSpecBase();
1024 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1025 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
1026 Header cookieheader = cookiespec.formatCookieHeader(cookies[0]);
1027 assertEquals("name=value", cookieheader.getValue());
1028 }
1029
1030 /***
1031 * Tests if null cookie values are handled correctly.
1032 */
1033 public void testNullCookieValueFormatting() {
1034 Cookie cookie = new Cookie(".whatever.com", "name", null, "/", null, false);
1035 cookie.setDomainAttributeSpecified(true);
1036 cookie.setPathAttributeSpecified(true);
1037
1038 CookieSpec cookiespec = new CookieSpecBase();
1039 String s = cookiespec.formatCookie(cookie);
1040 assertEquals("name=", s);
1041 }
1042
1043 public void testFormatInvalidCookies() throws Exception {
1044 CookieSpec cookiespec = new CookieSpecBase();
1045 try {
1046 String s = cookiespec.formatCookies(null);
1047 fail("IllegalArgumentException nust have been thrown");
1048 } catch (IllegalArgumentException expected) {
1049 }
1050 }
1051
1052 public void testFormatZeroCookies() throws Exception {
1053 CookieSpec cookiespec = new CookieSpecBase();
1054 try {
1055 String s = cookiespec.formatCookies(new Cookie[] {});
1056 fail("IllegalArgumentException nust have been thrown");
1057 } catch (IllegalArgumentException expected) {
1058 }
1059 }
1060
1061 /***
1062 * Tests generic cookie formatting.
1063 */
1064 public void testFormatSeveralCookies() throws Exception {
1065 Header header = new Header("Set-Cookie",
1066 "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
1067 CookieSpec cookiespec = new CookieSpecBase();
1068 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1069 String s = cookiespec.formatCookies(cookies);
1070 assertEquals("name1=value1; name2=value2", s);
1071 }
1072
1073 public void testFormatOneCookie() throws Exception {
1074 Header header = new Header("Set-Cookie",
1075 "name1=value1; path=/; domain=.mydomain.com;");
1076 CookieSpec cookiespec = new CookieSpecBase();
1077 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1078 String s = cookiespec.formatCookies(cookies);
1079 assertEquals("name1=value1", s);
1080 }
1081
1082 public void testFormatSeveralCookiesAsHeader() throws Exception {
1083 Header header = new Header("Set-Cookie",
1084 "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
1085 CookieSpec cookiespec = new CookieSpecBase();
1086 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1087 Header cookieheader = cookiespec.formatCookieHeader(cookies);
1088 assertEquals("name1=value1; name2=value2", cookieheader.getValue());
1089 }
1090
1091 public void testKeepCloverHappy() throws Exception {
1092 MalformedCookieException ex1 = new MalformedCookieException();
1093 MalformedCookieException ex2 = new MalformedCookieException("whatever");
1094 MalformedCookieException ex3 = new MalformedCookieException("whatever", null);
1095 }
1096
1097 }
1098