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: 345737 $
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 public void testParseNoName() throws Exception {
385 Header header = new Header("Set-Cookie","=stuff; path=/");
386
387 CookieSpec cookiespec = new CookieSpecBase();
388 try {
389 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
390 fail("MalformedCookieException should have been thrown");
391 } catch (MalformedCookieException ex) {
392
393 }
394 }
395
396 public void testParseNoValue() throws Exception {
397 Header header = new Header("Set-Cookie","cookie-name=");
398
399 CookieSpec cookiespec = new CookieSpecBase();
400 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
401 assertEquals("Found 1 cookie.",1,parsed.length);
402 assertEquals("Name","cookie-name",parsed[0].getName());
403 assertEquals("Value", "", parsed[0].getValue());
404 assertTrue("Comment",null == parsed[0].getComment());
405 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
406
407 assertTrue("isPersistent",!parsed[0].isPersistent());
408 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
409 assertEquals("Path","/",parsed[0].getPath());
410 assertTrue("Secure",!parsed[0].getSecure());
411 assertEquals("Version",0,parsed[0].getVersion());
412 }
413
414 public void testParseWithWhiteSpace() throws Exception {
415 Header header = new Header("Set-Cookie"," cookie-name = cookie-value ");
416
417 CookieSpec cookiespec = new CookieSpecBase();
418 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
419 assertEquals("Found 1 cookie.",1,parsed.length);
420 assertEquals("Name","cookie-name",parsed[0].getName());
421 assertEquals("Value","cookie-value",parsed[0].getValue());
422 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
423 assertEquals("Path","/",parsed[0].getPath());
424 assertTrue("Secure",!parsed[0].getSecure());
425 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
426 assertTrue("Comment",null == parsed[0].getComment());
427 }
428
429 public void testParseWithQuotes() throws Exception {
430 Header header = new Header("Set-Cookie"," cookie-name = \" cookie-value \" ;path=/");
431
432 CookieSpec cookiespec = new CookieSpecBase();
433 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1",80, "/", false, header);
434 assertEquals("Found 1 cookie.",1,parsed.length);
435 assertEquals("Name","cookie-name",parsed[0].getName());
436 assertEquals("Value"," cookie-value ",parsed[0].getValue());
437 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
438 assertEquals("Path","/",parsed[0].getPath());
439 assertTrue("Secure",!parsed[0].getSecure());
440 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
441 assertTrue("Comment",null == parsed[0].getComment());
442 }
443
444 public void testParseWithPath() throws Exception {
445 Header header = new Header("Set-Cookie","cookie-name=cookie-value; Path=/path/");
446
447 CookieSpec cookiespec = new CookieSpecBase();
448 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1",80, "/path/path", false, header);
449 assertEquals("Found 1 cookie.",1,parsed.length);
450 assertEquals("Name","cookie-name",parsed[0].getName());
451 assertEquals("Value","cookie-value",parsed[0].getValue());
452 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
453 assertEquals("Path","/path/",parsed[0].getPath());
454 assertTrue("Secure",!parsed[0].getSecure());
455 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
456 assertTrue("Comment",null == parsed[0].getComment());
457 }
458
459 public void testParseWithDomain() throws Exception {
460 Header header = new Header("Set-Cookie","cookie-name=cookie-value; Domain=127.0.0.1");
461
462 CookieSpec cookiespec = new CookieSpecBase();
463 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, header);
464 assertEquals("Found 1 cookie.",1,parsed.length);
465 assertEquals("Name","cookie-name",parsed[0].getName());
466 assertEquals("Value","cookie-value",parsed[0].getValue());
467 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
468 assertEquals("Path","/",parsed[0].getPath());
469 assertTrue("Secure",!parsed[0].getSecure());
470 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
471 assertTrue("Comment",null == parsed[0].getComment());
472 }
473
474 public void testParseWithSecure() throws Exception {
475 Header header = new Header("Set-Cookie","cookie-name=cookie-value; secure");
476
477 CookieSpec cookiespec = new CookieSpecBase();
478 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
479 assertEquals("Found 1 cookie.",1,parsed.length);
480 assertEquals("Name","cookie-name",parsed[0].getName());
481 assertEquals("Value","cookie-value",parsed[0].getValue());
482 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
483 assertEquals("Path","/",parsed[0].getPath());
484 assertTrue("Secure",parsed[0].getSecure());
485 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
486 assertTrue("Comment",null == parsed[0].getComment());
487 }
488
489 public void testParseWithComment() throws Exception {
490 Header header = new Header("Set-Cookie",
491 "cookie-name=cookie-value; comment=\"This is a comment.\"");
492
493 CookieSpec cookiespec = new CookieSpecBase();
494 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
495 assertEquals("Found 1 cookie.",1,parsed.length);
496 assertEquals("Name","cookie-name",parsed[0].getName());
497 assertEquals("Value","cookie-value",parsed[0].getValue());
498 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
499 assertEquals("Path","/",parsed[0].getPath());
500 assertTrue("Secure",!parsed[0].getSecure());
501 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate());
502 assertEquals("Comment","This is a comment.",parsed[0].getComment());
503 }
504
505 public void testParseWithExpires() throws Exception {
506 Header header = new Header("Set-Cookie",
507 "cookie-name=cookie-value;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
508
509 CookieSpec cookiespec = new CookieSpecBase();
510 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", true, header);
511 assertEquals("Found 1 cookie.",1,parsed.length);
512 assertEquals("Name","cookie-name",parsed[0].getName());
513 assertEquals("Value","cookie-value",parsed[0].getValue());
514 assertEquals("Domain","127.0.0.1",parsed[0].getDomain());
515 assertEquals("Path","/",parsed[0].getPath());
516 assertTrue("Secure",!parsed[0].getSecure());
517 assertEquals(new Date(10000L),parsed[0].getExpiryDate());
518 assertTrue("Comment",null == parsed[0].getComment());
519 }
520
521 public void testParseWithAll() throws Exception {
522 Header header = new Header("Set-Cookie",
523 "cookie-name=cookie-value;Version=1;Path=/commons;Domain=.apache.org;" +
524 "Comment=This is a comment.;secure;Expires=Thu, 01-Jan-1970 00:00:10 GMT");
525
526 CookieSpec cookiespec = new CookieSpecBase();
527 Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
528 assertEquals("Found 1 cookie.",1,parsed.length);
529 assertEquals("Name","cookie-name",parsed[0].getName());
530 assertEquals("Value","cookie-value",parsed[0].getValue());
531 assertEquals("Domain",".apache.org",parsed[0].getDomain());
532 assertEquals("Path","/commons",parsed[0].getPath());
533 assertTrue("Secure",parsed[0].getSecure());
534 assertEquals(new Date(10000L),parsed[0].getExpiryDate());
535 assertEquals("Comment","This is a comment.",parsed[0].getComment());
536 assertEquals("Version",0,parsed[0].getVersion());
537 }
538
539 public void testParseMultipleDifferentPaths() throws Exception {
540 Header header = new Header("Set-Cookie",
541 "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;" +
542 "Path=/commons/httpclient;Version=1");
543
544 CookieSpec cookiespec = new CookieSpecBase();
545 Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
546 HttpState state = new HttpState();
547 state.addCookies(parsed);
548 Cookie[] cookies = state.getCookies();
549 assertEquals("Wrong number of cookies.",2,cookies.length);
550 assertEquals("Name","name1",cookies[0].getName());
551 assertEquals("Value","value1",cookies[0].getValue());
552 assertEquals("Name","name1",cookies[1].getName());
553 assertEquals("Value","value2",cookies[1].getValue());
554 }
555
556 public void testParseMultipleSamePaths() throws Exception {
557 Header header = new Header("Set-Cookie",
558 "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons");
559
560 CookieSpec cookiespec = new CookieSpecBase();
561 Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "/commons/httpclient", true, header);
562 HttpState state = new HttpState();
563 state.addCookies(parsed);
564 Cookie[] cookies = state.getCookies();
565 assertEquals("Found 1 cookies.",1,cookies.length);
566 assertEquals("Name","name1",cookies[0].getName());
567 assertEquals("Value","value2",cookies[0].getValue());
568 }
569
570 public void testParseRelativePath() throws Exception {
571 Header header = new Header("Set-Cookie", "name1=value1;Path=whatever");
572
573 CookieSpec cookiespec = new CookieSpecBase();
574 Cookie[] parsed = cookieParse(cookiespec, ".apache.org", 80, "whatever", true, header);
575 assertEquals("Found 1 cookies.",1,parsed.length);
576 assertEquals("Name","name1",parsed[0].getName());
577 assertEquals("Value","value1",parsed[0].getValue());
578 assertEquals("Path","whatever",parsed[0].getPath());
579 }
580
581 public void testParseWithWrongDomain() throws Exception {
582 Header header = new Header("Set-Cookie",
583 "cookie-name=cookie-value; domain=127.0.0.1; version=1");
584
585 CookieSpec cookiespec = new CookieSpecBase();
586 try {
587 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.2", 80, "/", false, header);
588 fail("HttpException exception should have been thrown");
589 } catch (HttpException e) {
590
591 }
592 }
593
594 public void testParseWithNullHost() throws Exception {
595 Header header = new Header("Set-Cookie",
596 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
597
598 CookieSpec cookiespec = new CookieSpecBase();
599 try {
600 Cookie[] parsed = cookieParse(cookiespec, null, 80, "/", false, header);
601 fail("IllegalArgumentException should have been thrown");
602 } catch (IllegalArgumentException e) {
603
604 }
605 }
606
607 public void testParseWithBlankHost() throws Exception {
608 Header header = new Header("Set-Cookie",
609 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
610
611 CookieSpec cookiespec = new CookieSpecBase();
612 try {
613 Cookie[] parsed = cookieParse(cookiespec, " ", 80, "/", false, header);
614 fail("IllegalArgumentException should have been thrown");
615 } catch (IllegalArgumentException e) {
616
617 }
618 }
619
620 public void testParseWithNullPath() throws Exception {
621 Header header = new Header("Set-Cookie",
622 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
623
624 CookieSpec cookiespec = new CookieSpecBase();
625 try {
626 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, null, false, header);
627 fail("IllegalArgumentException should have been thrown");
628 } catch (IllegalArgumentException e) {
629
630 }
631 }
632
633 public void testParseWithBlankPath() 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 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, " ", false, header);
639 assertNotNull(parsed);
640 assertEquals(1, parsed.length);
641 assertEquals("/", parsed[0].getPath());
642 }
643
644 public void testParseWithNegativePort() throws Exception {
645 Header header = new Header("Set-Cookie",
646 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
647
648 CookieSpec cookiespec = new CookieSpecBase();
649 try {
650 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", -80, null, false, header);
651 fail("IllegalArgumentException should have been thrown");
652 } catch (IllegalArgumentException e) {
653
654 }
655 }
656
657 public void testParseWithNullHostAndPath() throws Exception {
658 Header header = new Header("Set-Cookie",
659 "cookie-name=cookie-value; domain=127.0.0.1; path=/; secure");
660
661 CookieSpec cookiespec = new CookieSpecBase();
662 try {
663 Cookie[] parsed = cookieParse(cookiespec, null, 80, null, false, header);
664 fail("IllegalArgumentException should have been thrown");
665 } catch (IllegalArgumentException e) {
666
667 }
668 }
669
670 public void testParseWithPathMismatch() throws Exception {
671 Header header = new Header("Set-Cookie",
672 "cookie-name=cookie-value; path=/path/path/path");
673
674 CookieSpec cookiespec = new CookieSpecBase();
675 try {
676 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/path", false, header);
677 fail("MalformedCookieException should have been thrown.");
678 } catch (MalformedCookieException e) {
679
680 }
681 }
682
683 public void testParseWithPathMismatch2() throws Exception {
684 Header header = new Header("Set-Cookie",
685 "cookie-name=cookie-value; path=/foobar");
686
687 CookieSpec cookiespec = new CookieSpecBase();
688 try {
689 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/foo", false, header);
690 fail("MalformedCookieException should have been thrown.");
691 } catch (MalformedCookieException e) {
692
693 }
694 }
695
696
697 public void testParseWithInvalidHeader1() throws Exception {
698 CookieSpec cookiespec = new CookieSpecBase();
699 try {
700 Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo", false, (Header)null);
701 fail("IllegalArgumentException should have been thrown.");
702 } catch (IllegalArgumentException e) {
703
704 }
705 }
706
707 public void testParseWithInvalidHeader2() throws Exception {
708 CookieSpec cookiespec = new CookieSpecBase();
709 try {
710 Cookie[] parsed = cookiespec.parse("127.0.0.1", 80, "/foo", false, (String)null);
711 fail("IllegalArgumentException should have been thrown.");
712 } catch (IllegalArgumentException e) {
713
714 }
715 }
716
717 /***
718 * Tests if cookie constructor rejects cookie name containing blanks.
719 */
720 public void testCookieNameWithBlanks() throws Exception {
721 Header setcookie = new Header("Set-Cookie", "invalid name=");
722 CookieSpec cookiespec = new CookieSpecBase();
723 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, setcookie);
724 assertNotNull(parsed);
725 assertEquals(1, parsed.length);
726 }
727
728
729 /***
730 * Tests if cookie constructor rejects cookie name starting with $.
731 */
732 public void testCookieNameStartingWithDollarSign() throws Exception {
733 Header setcookie = new Header("Set-Cookie", "$invalid_name=");
734 CookieSpec cookiespec = new CookieSpecBase();
735 Cookie[] parsed = cookieParse(cookiespec, "127.0.0.1", 80, "/", false, setcookie);
736 assertNotNull(parsed);
737 assertEquals(1, parsed.length);
738 }
739
740
741 /***
742 * Tests if malformatted expires attribute is parsed correctly.
743 */
744 public void testCookieWithComma() throws Exception {
745 Header header = new Header("Set-Cookie", "name=value; expires=\"Thu, 01-Jan-1970 00:00:00 GMT");
746
747 CookieSpec cookiespec = new CookieSpecBase();
748 try {
749 Cookie[] cookies = cookiespec.parse("localhost", 80, "/", false, header);
750 fail("MalformedCookieException should have been thrown");
751 } catch (MalformedCookieException expected) {
752 }
753 }
754
755
756 /***
757 * Tests several date formats.
758 */
759 public void testDateFormats() throws Exception {
760
761 checkDate("Thu, 01-Jan-70 00:00:10 GMT");
762 checkDate("Thu, 01-Jan-2070 00:00:10 GMT");
763
764 checkDate("Thu 01-Jan-70 00:00:10 GMT");
765 checkDate("Thu 01-Jan-2070 00:00:10 GMT");
766
767 checkDate("Thu, 01 Jan 70 00:00:10 GMT");
768 checkDate("Thu, 01 Jan 2070 00:00:10 GMT");
769
770 checkDate("Thu 01 Jan 70 00:00:10 GMT");
771 checkDate("Thu 01 Jan 2070 00:00:10 GMT");
772
773 checkDate("Wed, 20-Nov-2002 09-38-33 GMT");
774
775
776 try {
777 checkDate("this aint a date");
778 fail("Date check is bogous");
779 } catch(Exception e) {
780
781 }
782 }
783
784 private void checkDate(String date) throws Exception {
785 Header header = new Header("Set-Cookie", "custno=12345;Expires='"+date+"';");
786 HttpParams params = new DefaultHttpParamsFactory().getDefaultParams();
787 CookieSpec cookiespec = new CookieSpecBase();
788 cookiespec.setValidDateFormats(
789 (Collection)params.getParameter(HttpMethodParams.DATE_PATTERNS));
790 cookieParse(cookiespec, "localhost", 80, "/", false, header);
791 }
792
793 /***
794 * Tests if invalid second domain level cookie gets accepted in the
795 * browser compatibility mode.
796 */
797 public void testSecondDomainLevelCookie() throws Exception {
798 Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
799 cookie.setDomainAttributeSpecified(true);
800 cookie.setPathAttributeSpecified(true);
801
802 CookieSpec cookiespec = new CookieSpecBase();
803 cookiespec.validate("sourceforge.net", 80, "/", false, cookie);
804 }
805
806 public void testSecondDomainLevelCookieMatch1() throws Exception {
807 Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
808 cookie.setDomainAttributeSpecified(true);
809 cookie.setPathAttributeSpecified(true);
810
811 CookieSpec cookiespec = new CookieSpecBase();
812 assertTrue(cookiespec.match("sourceforge.net", 80, "/", false, cookie));
813 }
814
815 public void testSecondDomainLevelCookieMatch2() throws Exception {
816 Cookie cookie = new Cookie("sourceforge.net", "name", null, "/", null, false);
817 cookie.setDomainAttributeSpecified(true);
818 cookie.setPathAttributeSpecified(true);
819
820 CookieSpec cookiespec = new CookieSpecBase();
821 assertTrue(cookiespec.match("www.sourceforge.net", 80, "/", false, cookie));
822 }
823
824 public void testSecondDomainLevelCookieMatch3() throws Exception {
825 Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
826 cookie.setDomainAttributeSpecified(true);
827 cookie.setPathAttributeSpecified(true);
828
829 CookieSpec cookiespec = new CookieSpecBase();
830 assertTrue(cookiespec.match("www.sourceforge.net", 80, "/", false, cookie));
831 }
832
833 public void testInvalidSecondDomainLevelCookieMatch1() throws Exception {
834 Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false);
835 cookie.setDomainAttributeSpecified(true);
836 cookie.setPathAttributeSpecified(true);
837
838 CookieSpec cookiespec = new CookieSpecBase();
839 assertFalse(cookiespec.match("antisourceforge.net", 80, "/", false, cookie));
840 }
841
842 public void testInvalidSecondDomainLevelCookieMatch2() throws Exception {
843 Cookie cookie = new Cookie("sourceforge.net", "name", null, "/", null, false);
844 cookie.setDomainAttributeSpecified(true);
845 cookie.setPathAttributeSpecified(true);
846
847 CookieSpec cookiespec = new CookieSpecBase();
848 assertFalse(cookiespec.match("antisourceforge.net", 80, "/", false, cookie));
849 }
850
851 public void testMatchNullHost() throws Exception {
852 CookieSpec cookiespec = new CookieSpecBase();
853 Cookie cookie = new Cookie();
854 try {
855 cookiespec.match(null, 80, "/", false, cookie);
856 fail("IllegalArgumentException must have been thrown");
857 } catch (IllegalArgumentException expected) {
858 }
859 }
860
861 public void testMatchBlankHost() throws Exception {
862 CookieSpec cookiespec = new CookieSpecBase();
863 Cookie cookie = new Cookie();
864 try {
865 cookiespec.match(" ", 80, "/", false, cookie);
866 fail("IllegalArgumentException must have been thrown");
867 } catch (IllegalArgumentException expected) {
868 }
869 }
870
871 public void testMatchInvalidPort() throws Exception {
872 CookieSpec cookiespec = new CookieSpecBase();
873 Cookie cookie = new Cookie();
874 try {
875 cookiespec.match("host", -80, "/", false, cookie);
876 fail("IllegalArgumentException must have been thrown");
877 } catch (IllegalArgumentException expected) {
878 }
879 }
880
881 public void testMatchNullPath() throws Exception {
882 CookieSpec cookiespec = new CookieSpecBase();
883 Cookie cookie = new Cookie();
884 try {
885 cookiespec.match("host", 80, null, false, cookie);
886 fail("IllegalArgumentException must have been thrown");
887 } catch (IllegalArgumentException expected) {
888 }
889 }
890
891 public void testMatchBlankPath() throws Exception {
892 CookieSpec cookiespec = new CookieSpecBase();
893 Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
894 assertTrue(cookiespec.match("host", 80, " ", false, cookie));
895 }
896
897 public void testMatchNullCookie() throws Exception {
898 CookieSpec cookiespec = new CookieSpecBase();
899 try {
900 cookiespec.match("host", 80, "/", false, (Cookie)null);
901 fail("IllegalArgumentException must have been thrown");
902 } catch (IllegalArgumentException expected) {
903 }
904 }
905
906 public void testMatchNullCookieDomain() throws Exception {
907 CookieSpec cookiespec = new CookieSpecBase();
908 Cookie cookie = new Cookie(null, "name", "value", "/", null, false);
909 assertFalse(cookiespec.match("host", 80, "/", false, cookie));
910 }
911
912 public void testMatchNullCookiePath() throws Exception {
913 CookieSpec cookiespec = new CookieSpecBase();
914 Cookie cookie = new Cookie("host", "name", "value", null, null, false);
915 assertFalse(cookiespec.match("host", 80, "/", false, cookie));
916 }
917
918 public void testCookieMatch1() throws Exception {
919 CookieSpec cookiespec = new CookieSpecBase();
920 Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
921 assertTrue(cookiespec.match("host", 80, "/", false, cookie));
922 }
923
924 public void testCookieMatch2() throws Exception {
925 CookieSpec cookiespec = new CookieSpecBase();
926 Cookie cookie = new Cookie(".whatever.com", "name", "value", "/", null, false);
927 assertTrue(cookiespec.match(".whatever.com", 80, "/", false, cookie));
928 }
929
930 public void testCookieMatch3() throws Exception {
931 CookieSpec cookiespec = new CookieSpecBase();
932 Cookie cookie = new Cookie(".whatever.com", "name", "value", "/", null, false);
933 assertTrue(cookiespec.match(".really.whatever.com", 80, "/", false, cookie));
934 }
935
936 public void testCookieMatch4() throws Exception {
937 CookieSpec cookiespec = new CookieSpecBase();
938 Cookie cookie = new Cookie("host", "name", "value", "/", null, false);
939 assertTrue(cookiespec.match("host", 80, "/foobar", false, cookie));
940 }
941
942 public void testCookieMismatch1() throws Exception {
943 CookieSpec cookiespec = new CookieSpecBase();
944 Cookie cookie = new Cookie("host1", "name", "value", "/", null, false);
945 assertFalse(cookiespec.match("host2", 80, "/", false, cookie));
946 }
947
948 public void testCookieMismatch2() throws Exception {
949 CookieSpec cookiespec = new CookieSpecBase();
950 Cookie cookie = new Cookie(".aaaaaaaaa.com", "name", "value", "/", null, false);
951 assertFalse(cookiespec.match(".bbbbbbbb.com", 80, "/", false, cookie));
952 }
953
954 public void testCookieMismatch3() throws Exception {
955 CookieSpec cookiespec = new CookieSpecBase();
956 Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, false);
957 assertFalse(cookiespec.match("host", 80, "/foo", false, cookie));
958 }
959
960 public void testCookieMismatch4() throws Exception {
961 CookieSpec cookiespec = new CookieSpecBase();
962 Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, true);
963 assertFalse(cookiespec.match("host", 80, "/foobar/", false, cookie));
964 }
965
966 public void testCookieMatch5() throws Exception {
967 CookieSpec cookiespec = new CookieSpecBase();
968 Cookie cookie = new Cookie("host", "name", "value", "/foobar/r", null, false);
969 assertFalse(cookiespec.match("host", 80, "/foobar/", false, cookie));
970 }
971
972 public void testCookieMismatch6() throws Exception {
973 CookieSpec cookiespec = new CookieSpecBase();
974 Cookie cookie = new Cookie("host", "name", "value", "/foobar", null, true);
975 assertFalse(cookiespec.match("host", 80, "/foobar", false, cookie));
976 }
977
978 public void testMatchNullCookies() throws Exception {
979 CookieSpec cookiespec = new CookieSpecBase();
980 Cookie[] matched = cookiespec.match("host", 80, "/foobar", false, (Cookie[])null);
981 assertNull(matched);
982 }
983
984 public void testMatchedCookiesOrder() throws Exception {
985 CookieSpec cookiespec = new CookieSpecBase();
986 Cookie[] cookies = {
987 new Cookie("host", "nomatch", "value", "/noway", null, false),
988 new Cookie("host", "name2", "value", "/foobar/yada", null, false),
989 new Cookie("host", "name3", "value", "/foobar", null, false),
990 new Cookie("host", "name1", "value", "/foobar/yada/yada", null, false)};
991 Cookie[] matched = cookiespec.match("host", 80, "/foobar/yada/yada", false, cookies);
992 assertNotNull(matched);
993 assertEquals(3, matched.length);
994 assertEquals("name1", matched[0].getName());
995 assertEquals("name2", matched[1].getName());
996 assertEquals("name3", matched[2].getName());
997 }
998
999 public void testInvalidMatchDomain() throws Exception {
1000 Cookie cookie = new Cookie("beta.gamma.com", "name", null, "/", null, false);
1001 cookie.setDomainAttributeSpecified(true);
1002 cookie.setPathAttributeSpecified(true);
1003
1004 CookieSpec cookiespec = new CookieSpecBase();
1005 cookiespec.validate("alpha.beta.gamma.com", 80, "/", false, cookie);
1006 assertTrue(cookiespec.match("alpha.beta.gamma.com", 80, "/", false, cookie));
1007 }
1008
1009 public void testFormatInvalidCookie() throws Exception {
1010 CookieSpec cookiespec = new CookieSpecBase();
1011 try {
1012 String s = cookiespec.formatCookie(null);
1013 fail("IllegalArgumentException nust have been thrown");
1014 } catch (IllegalArgumentException expected) {
1015 }
1016 }
1017
1018 /***
1019 * Tests generic cookie formatting.
1020 */
1021 public void testGenericCookieFormatting() throws Exception {
1022 Header header = new Header("Set-Cookie",
1023 "name=value; path=/; domain=.mydomain.com");
1024 CookieSpec cookiespec = new CookieSpecBase();
1025 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1026 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
1027 String s = cookiespec.formatCookie(cookies[0]);
1028 assertEquals("name=value", s);
1029 }
1030
1031 public void testGenericCookieFormattingAsHeader() throws Exception {
1032 Header header = new Header("Set-Cookie",
1033 "name=value; path=/; domain=.mydomain.com");
1034 CookieSpec cookiespec = new CookieSpecBase();
1035 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1036 cookiespec.validate("myhost.mydomain.com", 80, "/", false, cookies[0]);
1037 Header cookieheader = cookiespec.formatCookieHeader(cookies[0]);
1038 assertEquals("name=value", cookieheader.getValue());
1039 }
1040
1041 /***
1042 * Tests if null cookie values are handled correctly.
1043 */
1044 public void testNullCookieValueFormatting() {
1045 Cookie cookie = new Cookie(".whatever.com", "name", null, "/", null, false);
1046 cookie.setDomainAttributeSpecified(true);
1047 cookie.setPathAttributeSpecified(true);
1048
1049 CookieSpec cookiespec = new CookieSpecBase();
1050 String s = cookiespec.formatCookie(cookie);
1051 assertEquals("name=", s);
1052 }
1053
1054 public void testFormatInvalidCookies() throws Exception {
1055 CookieSpec cookiespec = new CookieSpecBase();
1056 try {
1057 String s = cookiespec.formatCookies(null);
1058 fail("IllegalArgumentException nust have been thrown");
1059 } catch (IllegalArgumentException expected) {
1060 }
1061 }
1062
1063 public void testFormatZeroCookies() throws Exception {
1064 CookieSpec cookiespec = new CookieSpecBase();
1065 try {
1066 String s = cookiespec.formatCookies(new Cookie[] {});
1067 fail("IllegalArgumentException nust have been thrown");
1068 } catch (IllegalArgumentException expected) {
1069 }
1070 }
1071
1072 /***
1073 * Tests generic cookie formatting.
1074 */
1075 public void testFormatSeveralCookies() throws Exception {
1076 Header header = new Header("Set-Cookie",
1077 "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
1078 CookieSpec cookiespec = new CookieSpecBase();
1079 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1080 String s = cookiespec.formatCookies(cookies);
1081 assertEquals("name1=value1; name2=value2", s);
1082 }
1083
1084 public void testFormatOneCookie() throws Exception {
1085 Header header = new Header("Set-Cookie",
1086 "name1=value1; path=/; domain=.mydomain.com;");
1087 CookieSpec cookiespec = new CookieSpecBase();
1088 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1089 String s = cookiespec.formatCookies(cookies);
1090 assertEquals("name1=value1", s);
1091 }
1092
1093 public void testFormatSeveralCookiesAsHeader() throws Exception {
1094 Header header = new Header("Set-Cookie",
1095 "name1=value1; path=/; domain=.mydomain.com, name2 = value2 ; path=/; domain=.mydomain.com");
1096 CookieSpec cookiespec = new CookieSpecBase();
1097 Cookie[] cookies = cookiespec.parse("myhost.mydomain.com", 80, "/", false, header);
1098 Header cookieheader = cookiespec.formatCookieHeader(cookies);
1099 assertEquals("name1=value1; name2=value2", cookieheader.getValue());
1100 }
1101
1102 public void testKeepCloverHappy() throws Exception {
1103 MalformedCookieException ex1 = new MalformedCookieException();
1104 MalformedCookieException ex2 = new MalformedCookieException("whatever");
1105 MalformedCookieException ex3 = new MalformedCookieException("whatever", null);
1106 }
1107
1108 }
1109