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