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: 155418 $
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 assertTrue("Value",null == 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 testSecondDomainLevelCookieMatch() 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 testMatchNullHost() throws Exception {
805 CookieSpec cookiespec = new CookieSpecBase();
806 Cookie cookie = new Cookie();
807 try {
808 cookiespec.match(null, 80, "/", false, cookie);
809 fail("IllegalArgumentException must have been thrown");
810 } catch (IllegalArgumentException expected) {
811 }
812 }
813
814 public void testMatchBlankHost() throws Exception {
815 CookieSpec cookiespec = new CookieSpecBase();
816 Cookie cookie = new Cookie();
817 try {
818 cookiespec.match(" ", 80, "/", false, cookie);
819 fail("IllegalArgumentException must have been thrown");
820 } catch (IllegalArgumentException expected) {
821 }
822 }
823
824 public void testMatchInvalidPort() throws Exception {
825 CookieSpec cookiespec = new CookieSpecBase();
826 Cookie cookie = new Cookie();
827 try {
828 cookiespec.match("host", -80, "/", false, cookie);
829 fail("IllegalArgumentException must have been thrown");
830 } catch (IllegalArgumentException expected) {
831 }
832 }
833
834 public void testMatchNullPath() throws Exception {
835 CookieSpec cookiespec = new CookieSpecBase();
836 Cookie cookie = new Cookie();
837 try {
838 cookiespec.match("host", 80, null, false, cookie);
839 fail("IllegalArgumentException must have been thrown");
840 } catch (IllegalArgumentException expected) {
841 }
842 }
843
844 public void testMatchBlankPath() throws Exception {
845 CookieSpec cookiespec = new CookieSpecBase();
846 Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
847 assertTrue(cookiespec.match("host", 80, " ", false, cookie));
848 }
849
850 public void testMatchNullCookie() throws Exception {
851 CookieSpec cookiespec = new CookieSpecBase();
852 try {
853 cookiespec.match("host", 80, "/", false, (Cookie)null);
854 fail("IllegalArgumentException must have been thrown");
855 } catch (IllegalArgumentException expected) {
856 }
857 }
858
859 public void testMatchNullCookieDomain() throws Exception {
860 CookieSpec cookiespec = new CookieSpecBase();
861 Cookie cookie = new Cookie(null, "name", "value", "/", null, false);
862 assertFalse(cookiespec.match("host", 80, "/", false, cookie));
863 }
864
865 public void testMatchNullCookiePath() throws Exception {
866 CookieSpec cookiespec = new CookieSpecBase();
867 Cookie cookie = new Cookie("host", "name", "value", null, null, false);
868 assertFalse(cookiespec.match("host", 80, "/", false, cookie));
869 }
870
871 public void testCookieMatch1() throws Exception {
872 CookieSpec cookiespec = new CookieSpecBase();
873 Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
874 assertTrue(cookiespec.match("host", 80, "/", false, cookie));
875 }
876
877 public void testCookieMatch2() throws Exception {
878 CookieSpec cookiespec = new CookieSpecBase();
879 Cookie cookie = new Cookie(".whatever.com", "name", "value", "/", null, false);
880 assertTrue(cookiespec.match(".whatever.com", 80, "/", false, cookie));
881 }
882
883 public void testCookieMatch3() throws Exception {
884 CookieSpec cookiespec = new CookieSpecBase();
885 Cookie cookie = new Cookie(".whatever.com", "name", "value", "/", null, false);
886 assertTrue(cookiespec.match(".really.whatever.com", 80, "/", false, cookie));
887 }
888
889 public void testCookieMatch4() throws Exception {
890 CookieSpec cookiespec = new CookieSpecBase();
891 Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
892 assertTrue(cookiespec.match("host", 80, "/foobar", false, cookie));
893 }
894
895 public void testCookieMismatch1() throws Exception {
896 CookieSpec cookiespec = new CookieSpecBase();
897 Cookie cookie = new Cookie("host1", "name", "value", "/", null, false);
898 assertFalse(cookiespec.match("host2", 80, "/", false, cookie));
899 }
900
901 public void testCookieMismatch2() throws Exception {
902 CookieSpec cookiespec = new CookieSpecBase();
903 Cookie cookie = new Cookie(".aaaaaaaaa.com", "name", "value", "/", null, false);
904 assertFalse(cookiespec.match(".bbbbbbbb.com", 80, "/", false, cookie));
905 }
906
907 public void testCookieMismatch3() throws Exception {
908 CookieSpec cookiespec = new CookieSpecBase();
909 Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, false);
910 assertFalse(cookiespec.match("host", 80, "/foo", false, cookie));
911 }
912
913 public void testCookieMismatch4() throws Exception {
914 CookieSpec cookiespec = new CookieSpecBase();
915 Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, true);
916 assertFalse(cookiespec.match("host", 80, "/foobar/", false, cookie));
917 }
918
919 public void testCookieMatch5() throws Exception {
920 CookieSpec cookiespec = new CookieSpecBase();
921 Cookie cookie = new Cookie("host", "name", "value", "/foobar/r", null, false);
922 assertFalse(cookiespec.match("host", 80, "/foobar/", false, cookie));
923 }
924
925 public void testCookieMismatch6() throws Exception {
926 CookieSpec cookiespec = new CookieSpecBase();
927 Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, true);
928 assertFalse(cookiespec.match("host", 80, "/foobar", false, cookie));
929 }
930
931 public void testMatchNullCookies() throws Exception {
932 CookieSpec cookiespec = new CookieSpecBase();
933 Cookie[] matched = cookiespec.match("host", 80, "/foobar", false, (Cookie[])null);
934 assertNull(matched);
935 }
936
937 public void testMatchedCookiesOrder() throws Exception {
938 CookieSpec cookiespec = new CookieSpecBase();
939 Cookie[] cookies = {
940 new Cookie("host", "nomatch", "value", "/noway", null, false),
941 new Cookie("host", "name2", "value", "/foobar/yada", null, false),
942 new Cookie("host", "name3", "value", "/foobar", null, false),
943 new Cookie("host", "name1", "value", "/foobar/yada/yada", null, false)};
944 Cookie[] matched = cookiespec.match("host", 80, "/foobar/yada/yada", false, cookies);
945 assertNotNull(matched);
946 assertEquals(3, matched.length);
947 assertEquals("name1", matched[0].getName());
948 assertEquals("name2", matched[1].getName());
949 assertEquals("name3", matched[2].getName());
950 }
951
952 public void testInvalidMatchDomain() throws Exception {
953 Cookie cookie = new Cookie("beta.gamma.com", "name", null, "/", null, false);
954 cookie.setDomainAttributeSpecified(true);
955 cookie.setPathAttributeSpecified(true);
956
957 CookieSpec cookiespec = new CookieSpecBase();
958 cookiespec.validate("alpha.beta.gamma.com", 80, "/", false, cookie);
959 assertTrue(cookiespec.match("alpha.beta.gamma.com", 80, "/", false, cookie));
960 }
961
962 public void testFormatInvalidCookie() throws Exception {
963 CookieSpec cookiespec = new CookieSpecBase();
964 try {
965 String s = cookiespec.formatCookie(null);
966 fail("IllegalArgumentException nust have been thrown");
967 } catch (IllegalArgumentException expected) {
968 }
969 }
970
971 /***
972 * Tests generic cookie formatting.
973 */
974 public void testGenericCookieFormatting() throws Exception {
975 Header header = new Header("Set-Cookie",
976 "name=value; path=/; domain=.mydomain.com");
977 CookieSpec cookiespec = new CookieSpecBase();
978 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
979 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
980 String s = cookiespec.formatCookie(cookies[0]);
981 assertEquals("name=value", s);
982 }
983
984 public void testGenericCookieFormattingAsHeader() throws Exception {
985 Header header = new Header("Set-Cookie",
986 "name=value; path=/; domain=.mydomain.com");
987 CookieSpec cookiespec = new CookieSpecBase();
988 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
989 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
990 Header cookieheader = cookiespec.formatCookieHeader(cookies[0]);
991 assertEquals("name=value", cookieheader.getValue());
992 }
993
994 /***
995 * Tests if null cookie values are handled correctly.
996 */
997 public void testNullCookieValueFormatting() {
998 Cookie cookie = new Cookie(".whatever.com", "name", null, "/", null, false);
999 cookie.setDomainAttributeSpecified(true);
1000 cookie.setPathAttributeSpecified(true);
1001
1002 CookieSpec cookiespec = new CookieSpecBase();
1003 String s = cookiespec.formatCookie(cookie);
1004 assertEquals("name=", s);
1005 }
1006
1007 public void testFormatInvalidCookies() throws Exception {
1008 CookieSpec cookiespec = new CookieSpecBase();
1009 try {
1010 String s = cookiespec.formatCookies(null);
1011 fail("IllegalArgumentException nust have been thrown");
1012 } catch (IllegalArgumentException expected) {
1013 }
1014 }
1015
1016 public void testFormatZeroCookies() throws Exception {
1017 CookieSpec cookiespec = new CookieSpecBase();
1018 try {
1019 String s = cookiespec.formatCookies(new Cookie[] {});
1020 fail("IllegalArgumentException nust have been thrown");
1021 } catch (IllegalArgumentException expected) {
1022 }
1023 }
1024
1025 /***
1026 * Tests generic cookie formatting.
1027 */
1028 public void testFormatSeveralCookies() throws Exception {
1029 Header header = new Header("Set-Cookie",
1030 "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
1031 CookieSpec cookiespec = new CookieSpecBase();
1032 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1033 String s = cookiespec.formatCookies(cookies);
1034 assertEquals("name1=value1; name2=value2", s);
1035 }
1036
1037 public void testFormatOneCookie() throws Exception {
1038 Header header = new Header("Set-Cookie",
1039 "name1=value1; path=/; domain=.mydomain.com;");
1040 CookieSpec cookiespec = new CookieSpecBase();
1041 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1042 String s = cookiespec.formatCookies(cookies);
1043 assertEquals("name1=value1", s);
1044 }
1045
1046 public void testFormatSeveralCookiesAsHeader() throws Exception {
1047 Header header = new Header("Set-Cookie",
1048 "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
1049 CookieSpec cookiespec = new CookieSpecBase();
1050 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1051 Header cookieheader = cookiespec.formatCookieHeader(cookies);
1052 assertEquals("name1=value1; name2=value2", cookieheader.getValue());
1053 }
1054
1055 public void testKeepCloverHappy() throws Exception {
1056 MalformedCookieException ex1 = new MalformedCookieException();
1057 MalformedCookieException ex2 = new MalformedCookieException("whatever");
1058 MalformedCookieException ex3 = new MalformedCookieException("whatever", null);
1059 }
1060
1061 }
1062