1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestCookie.java,v 1.22.2.2 2004/01/06 22:09:04 olegk Exp $ 3 * $Revision: 1.22.2.2 $ 4 * $Date: 2004/01/06 22:09:04 $ 5 * ==================================================================== 6 * 7 * The Apache Software License, Version 1.1 8 * 9 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights 10 * reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the 22 * distribution. 23 * 24 * 3. The end-user documentation included with the redistribution, if 25 * any, must include the following acknowlegement: 26 * "This product includes software developed by the 27 * Apache Software Foundation (http://www.apache.org/)." 28 * Alternately, this acknowlegement may appear in the software itself, 29 * if and wherever such third-party acknowlegements normally appear. 30 * 31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 32 * Foundation" must not be used to endorse or promote products derived 33 * from this software without prior written permission. For written 34 * permission, please contact apache@apache.org. 35 * 36 * 5. Products derived from this software may not be called "Apache" 37 * nor may "Apache" appear in their names without prior written 38 * permission of the Apache Group. 39 * 40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This software consists of voluntary contributions made by many 55 * individuals on behalf of the Apache Software Foundation. For more 56 * information on the Apache Software Foundation, please see 57 * <http://www.apache.org/>. 58 * 59 * [Additional notices, if required by prior licensing conditions] 60 * 61 */ 62 63 package org.apache.commons.httpclient; 64 65 import junit.framework.Test; 66 import junit.framework.TestCase; 67 import junit.framework.TestSuite; 68 import java.util.Date; 69 import java.util.Vector; 70 import java.util.SortedSet; 71 import java.util.TreeSet; 72 import java.util.Iterator; 73 import org.apache.commons.httpclient.cookie.*; 74 75 76 /*** 77 * Test cases for Cookie 78 * 79 * @author BC Holmes 80 * @author Rod Waldhoff 81 * @author dIon Gillard 82 * @author <a href="mailto:JEvans@Cyveillance.com">John Evans</a> 83 * @author Marc A. Saegesser 84 * @author <a href="mailto:oleg@ural.ru">Oleg Kalnichevski</a> 85 * @version $Revision: 1.22.2.2 $ 86 */ 87 public class TestCookie extends TestCase { 88 89 90 // -------------------------------------------------------------- Constants 91 92 private static final String DOMAIN_NAME = "www.apache.org"; 93 private static final String TEST_COOKIE = "cookie-name=cookie-value"; 94 private static final String OLD_EXPIRY = "Expires=Thu, 01-Jan-1970 00:00:10 GMT"; 95 private static final String SEP = ";"; 96 private static final String ROOT_PATH = "/"; 97 private static final int DEFAULT_PORT = 80; 98 99 private String[] testName = { "custno", "name", "name" }; 100 private String[] testValue = { "12345", "John", "Doe, John" }; 101 private String[] testDomain = { "www.apache.org", ".apache.org", 102 ".apache.org" }; 103 104 // ------------------------------------------------------------ Constructor 105 106 107 public TestCookie(String name) { 108 super(name); 109 } 110 111 112 // ------------------------------------------------------- TestCase Methods 113 114 115 public static Test suite() { 116 return new TestSuite(TestCookie.class); 117 } 118 119 120 // ------------------------------------------------------- Helper Methods 121 122 private static Cookie[] cookieParse(int policy, String host, String path, boolean isSecure, Header setHeader) 123 throws MalformedCookieException 124 { 125 CookieSpec parser = CookiePolicy.getSpecByPolicy(policy); 126 Cookie[] cookies = parser.parse(host, DEFAULT_PORT, path, isSecure, setHeader); 127 if (cookies != null) 128 { 129 for(int i = 0; i < cookies.length; i++) 130 { 131 parser.validate(host, DEFAULT_PORT, path, isSecure, cookies[i]); 132 } 133 } 134 return cookies; 135 } 136 137 138 private static Cookie[] cookieParse(String host, String path, boolean isSecure, Header setHeader) 139 throws MalformedCookieException 140 { 141 return cookieParse(CookiePolicy.RFC2109, host, path, isSecure, setHeader); 142 } 143 144 145 private static Cookie[] cookieParse(String host, String path, Header setHeader) 146 throws MalformedCookieException 147 { 148 return cookieParse(CookiePolicy.RFC2109, host, path, false, setHeader); 149 } 150 151 152 private static Cookie[] netscapeCcookieParse(String host, String path, Header setHeader) 153 throws MalformedCookieException 154 { 155 return cookieParse(CookiePolicy.NETSCAPE_DRAFT, host, path, false, setHeader); 156 } 157 158 159 public static Header cookieCreateHeader(int policy, String domain, int port, String path, boolean secure, Cookie[] cookies) 160 { 161 CookieSpec matcher = CookiePolicy.getSpecByPolicy(policy); 162 cookies = matcher.match(domain, port, path, secure, cookies); 163 if ((cookies != null) && (cookies.length > 0)) 164 { 165 return matcher.formatCookieHeader(cookies); 166 } 167 else 168 { 169 return null; 170 } 171 } 172 173 public static Header cookieCreateHeader(String domain, int port, String path, boolean secure, Cookie[] cookies) 174 { 175 return cookieCreateHeader(CookiePolicy.RFC2109, domain, port, path, secure, cookies); 176 } 177 178 179 public boolean cookieMatch(int policy, String domain, int port, String path, boolean secure, Cookie cookie) 180 { 181 CookieSpec matcher = CookiePolicy.getSpecByPolicy(policy); 182 return matcher.match(domain, port, path, secure, cookie); 183 } 184 185 186 public boolean cookieMatch(String domain, int port, String path, boolean secure, Cookie cookie) 187 { 188 return cookieMatch(CookiePolicy.RFC2109, domain, port, path, secure, cookie); 189 } 190 191 // ------------------------------------------------------------ Parse1 Test 192 193 194 /*** 195 * Test basic parse (with various spacings 196 */ 197 public void testParse1() throws Exception { 198 String headerValue = "custno = 12345; comment=test; version=1," + 199 " name=John; version=1; max-age=600; secure; domain=.apache.org"; 200 Cookie[] cookies = cookieParse(DOMAIN_NAME,"/", true, new Header( 201 "set-cookie", headerValue)); 202 checkResultsOfParse(cookies, 2, 0); 203 } 204 205 206 protected void checkResultsOfParse( 207 Cookie[] cookies, int length, int offset) throws Exception { 208 209 assertTrue("number of cookies should be " + length + ", but is " + 210 cookies.length + " instead.", cookies.length == length); 211 212 for (int i = 0; i < cookies.length; i++) { 213 214 assertTrue("Name of cookie " + i + " should be \"" + 215 testName[i+offset] + "\", but is " + cookies[i].getName() + 216 " instead.", 217 testName[i+offset].equals(cookies[i].getName())); 218 assertTrue("Value of cookie " + i + " should be \"" + 219 testValue[i+offset] + "\", but is " + 220 cookies[i].getValue() + " instead.", 221 testValue[i+offset].equals(cookies[i].getValue())); 222 assertTrue("Domain of cookie " + i + " should be \"" + 223 testDomain[i+offset] + "\", but is " + 224 cookies[i].getDomain() + " instead.", 225 testDomain[i+offset].equalsIgnoreCase( 226 cookies[i].getDomain())); 227 } 228 } 229 230 231 // ------------------------------------------------------------ Parse2 Test 232 233 234 /*** 235 * Test no spaces 236 */ 237 public void testParse2() throws Exception { 238 String headerValue = "custno=12345;comment=test; version=1," + 239 "name=John;version=1;max-age=600;secure;domain=.apache.org"; 240 Cookie[] cookies = cookieParse(DOMAIN_NAME, "/", true, new Header( 241 "set-cookie", headerValue)); 242 checkResultsOfParse(cookies, 2, 0); 243 } 244 245 246 // ------------------------------------------------------------ Parse3 Test 247 248 249 /*** 250 * Test parse with quoted text 251 */ 252 public void testParse3() throws Exception { 253 String headerValue = 254 "name=\"Doe, John\";version=1;max-age=600;secure;domain=.apache.org"; 255 Cookie[] cookies = cookieParse(DOMAIN_NAME,"/", true, new Header( 256 "set-cookie", headerValue)); 257 checkResultsOfParse(cookies, 1, 2); 258 } 259 260 // ------------------------------------------------------------- More Tests 261 262 // see http://nagoya.apache.org/bugzilla/show_bug.cgi?id=5279 263 public void testQuotedExpiresAttribute() throws Exception { 264 String headerValue = "custno=12345;Expires='Thu, 01-Jan-2070 00:00:10 GMT'"; 265 Cookie[] cookies = cookieParse(DOMAIN_NAME,"/",true,new Header( 266 "set-cookie", headerValue)); 267 assertNotNull("Expected some cookies",cookies); 268 assertEquals("Expected 1 cookie",1,cookies.length); 269 assertNotNull("Expected cookie to have getExpiryDate",cookies[0].getExpiryDate()); 270 } 271 272 public void testSecurityError() throws Exception { 273 String headerValue = "custno=12345;comment=test; version=1," + 274 "name=John;version=1;max-age=600;secure;domain=jakarta.apache.org"; 275 try { 276 Cookie[] cookies = cookieParse(DOMAIN_NAME, "/", new Header( 277 "set-cookie", headerValue)); 278 fail("HttpException exception should have been thrown"); 279 } catch (HttpException e) { 280 // expected 281 } 282 } 283 284 public void testParseSimple() throws Exception { 285 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value"); 286 Cookie[] parsed = cookieParse("127.0.0.1","/path/path",setCookie); 287 assertEquals("Found 1 cookie.",1,parsed.length); 288 assertEquals("Name","cookie-name",parsed[0].getName()); 289 assertEquals("Value","cookie-value",parsed[0].getValue()); 290 assertTrue("Comment",null == parsed[0].getComment()); 291 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 292 //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded()); 293 assertTrue("isPersistent",!parsed[0].isPersistent()); 294 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 295 assertEquals("Path","/path",parsed[0].getPath()); 296 assertTrue("Secure",!parsed[0].getSecure()); 297 assertEquals("Version",0,parsed[0].getVersion()); 298 } 299 300 301 public void testParseSimple2() throws Exception { 302 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value"); 303 Cookie[] parsed = cookieParse("127.0.0.1","/path",setCookie); 304 assertEquals("Found 1 cookie.",1,parsed.length); 305 assertEquals("Name","cookie-name",parsed[0].getName()); 306 assertEquals("Value","cookie-value",parsed[0].getValue()); 307 assertTrue("Comment",null == parsed[0].getComment()); 308 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 309 //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded()); 310 assertTrue("isPersistent",!parsed[0].isPersistent()); 311 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 312 assertEquals("Path","/",parsed[0].getPath()); 313 assertTrue("Secure",!parsed[0].getSecure()); 314 assertEquals("Version",0,parsed[0].getVersion()); 315 } 316 317 318 public void testParseNoValue() throws Exception { 319 Header setCookie = new Header("Set-Cookie","cookie-name="); 320 Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie); 321 assertEquals("Found 1 cookie.",1,parsed.length); 322 assertEquals("Name","cookie-name",parsed[0].getName()); 323 assertTrue("Value",null == parsed[0].getValue()); 324 assertTrue("Comment",null == parsed[0].getComment()); 325 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 326 //assertTrue("isToBeDiscarded",parsed[0].isToBeDiscarded()); 327 assertTrue("isPersistent",!parsed[0].isPersistent()); 328 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 329 assertEquals("Path","/",parsed[0].getPath()); 330 assertTrue("Secure",!parsed[0].getSecure()); 331 assertEquals("Version",0,parsed[0].getVersion()); 332 } 333 334 public void testParseWithWhiteSpace() throws Exception { 335 Header setCookie = new Header("Set-Cookie"," cookie-name = cookie-value "); 336 Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie); 337 assertEquals("Found 1 cookie.",1,parsed.length); 338 assertEquals("Name","cookie-name",parsed[0].getName()); 339 assertEquals("Value","cookie-value",parsed[0].getValue()); 340 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 341 assertEquals("Path","/",parsed[0].getPath()); 342 assertTrue("Secure",!parsed[0].getSecure()); 343 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 344 assertTrue("Comment",null == parsed[0].getComment()); 345 } 346 347 public void testParseWithQuotes() throws Exception { 348 Header setCookie = new Header("Set-Cookie"," cookie-name = \" cookie-value \" ;path=/"); 349 Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie); 350 assertEquals("Found 1 cookie.",1,parsed.length); 351 assertEquals("Name","cookie-name",parsed[0].getName()); 352 assertEquals("Value"," cookie-value ",parsed[0].getValue()); 353 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 354 assertEquals("Path","/",parsed[0].getPath()); 355 assertTrue("Secure",!parsed[0].getSecure()); 356 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 357 assertTrue("Comment",null == parsed[0].getComment()); 358 } 359 360 public void testParseWithPath() throws Exception { 361 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; Path=/path/"); 362 Cookie[] parsed = cookieParse("127.0.0.1","/path/path",setCookie); 363 assertEquals("Found 1 cookie.",1,parsed.length); 364 assertEquals("Name","cookie-name",parsed[0].getName()); 365 assertEquals("Value","cookie-value",parsed[0].getValue()); 366 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 367 assertEquals("Path","/path/",parsed[0].getPath()); 368 assertTrue("Secure",!parsed[0].getSecure()); 369 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 370 assertTrue("Comment",null == parsed[0].getComment()); 371 } 372 373 public void testParseWithDomain() throws Exception { 374 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; Domain=127.0.0.1"); 375 Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie); 376 assertEquals("Found 1 cookie.",1,parsed.length); 377 assertEquals("Name","cookie-name",parsed[0].getName()); 378 assertEquals("Value","cookie-value",parsed[0].getValue()); 379 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 380 assertEquals("Path","/",parsed[0].getPath()); 381 assertTrue("Secure",!parsed[0].getSecure()); 382 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 383 assertTrue("Comment",null == parsed[0].getComment()); 384 } 385 386 public void testParseWithSecure() throws Exception { 387 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; secure"); 388 Cookie[] parsed = cookieParse("127.0.0.1","/",true,setCookie); 389 assertEquals("Found 1 cookie.",1,parsed.length); 390 assertEquals("Name","cookie-name",parsed[0].getName()); 391 assertEquals("Value","cookie-value",parsed[0].getValue()); 392 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 393 assertEquals("Path","/",parsed[0].getPath()); 394 assertTrue("Secure",parsed[0].getSecure()); 395 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 396 assertTrue("Comment",null == parsed[0].getComment()); 397 } 398 399 public void testParseWithComment() throws Exception { 400 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; comment=\"This is a comment.\""); 401 Cookie[] parsed = cookieParse("127.0.0.1","/",true,setCookie); 402 assertEquals("Found 1 cookie.",1,parsed.length); 403 assertEquals("Name","cookie-name",parsed[0].getName()); 404 assertEquals("Value","cookie-value",parsed[0].getValue()); 405 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 406 assertEquals("Path","/",parsed[0].getPath()); 407 assertTrue("Secure",!parsed[0].getSecure()); 408 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 409 assertEquals("Comment","This is a comment.",parsed[0].getComment()); 410 } 411 412 public void testParseWithExpires() throws Exception { 413 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 414 Cookie[] parsed = cookieParse("127.0.0.1","/",true,setCookie); 415 assertEquals("Found 1 cookie.",1,parsed.length); 416 assertEquals("Name","cookie-name",parsed[0].getName()); 417 assertEquals("Value","cookie-value",parsed[0].getValue()); 418 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 419 assertEquals("Path","/",parsed[0].getPath()); 420 assertTrue("Secure",!parsed[0].getSecure()); 421 assertEquals(new Date(10000L),parsed[0].getExpiryDate()); 422 assertTrue("Comment",null == parsed[0].getComment()); 423 } 424 425 public void testParseWithAll() throws Exception { 426 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Version=1;Path=/commons;Domain=.apache.org;Comment=This is a comment.;secure;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 427 Cookie[] parsed = cookieParse(".apache.org","/commons/httpclient",true,setCookie); 428 assertEquals("Found 1 cookie.",1,parsed.length); 429 assertEquals("Name","cookie-name",parsed[0].getName()); 430 assertEquals("Value","cookie-value",parsed[0].getValue()); 431 assertEquals("Domain",".apache.org",parsed[0].getDomain()); 432 assertEquals("Path","/commons",parsed[0].getPath()); 433 assertTrue("Secure",parsed[0].getSecure()); 434 assertEquals(new Date(10000L),parsed[0].getExpiryDate()); 435 assertEquals("Comment","This is a comment.",parsed[0].getComment()); 436 assertEquals("Version",1,parsed[0].getVersion()); 437 } 438 439 public void testParseMultipleDifferentPaths() throws Exception { 440 Header setCookie = new Header("Set-Cookie","name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons/httpclient;Version=1"); 441 Cookie[] parsed = cookieParse(".apache.org","/commons/httpclient",true,setCookie); 442 HttpState state = new HttpState(); 443 state.addCookies(parsed); 444 Cookie[] cookies = state.getCookies(); 445 assertEquals("Wrong number of cookies.",2,cookies.length); 446 assertEquals("Name","name1",cookies[0].getName()); 447 assertEquals("Value","value1",cookies[0].getValue()); 448 assertEquals("Name","name1",cookies[1].getName()); 449 assertEquals("Value","value2",cookies[1].getValue()); 450 } 451 452 public void testParseMultipleSamePaths() throws Exception { 453 Header setCookie = new Header("Set-Cookie","name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons"); 454 Cookie[] parsed = cookieParse(".apache.org","/commons/httpclient",true,setCookie); 455 HttpState state = new HttpState(); 456 state.addCookies(parsed); 457 Cookie[] cookies = state.getCookies(); 458 assertEquals("Found 1 cookies.",1,cookies.length); 459 assertEquals("Name","name1",cookies[0].getName()); 460 assertEquals("Value","value2",cookies[0].getValue()); 461 } 462 463 public void testParseWithWrongDomain() throws Exception { 464 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1; version=1"); 465 try { 466 Cookie[] parsed = cookieParse("127.0.0.2","/",setCookie); 467 fail("HttpException exception should have been thrown"); 468 } catch (HttpException e) { 469 // expected 470 } 471 } 472 473 public void testParseWithWrongDomain2() throws Exception { 474 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.c.com; version=1"); 475 try { 476 Cookie[] parsed = cookieParse("a.b.c.com","/",setCookie); 477 fail("HttpException exception should have been thrown"); 478 } catch (HttpException e) { 479 // expected 480 } 481 } 482 483 /*** 484 * Domain has no embedded dots 485 */ 486 public void testParseWithIllegalDomain() throws Exception { 487 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com; version=1"); 488 try { 489 Cookie[] parsed = cookieParse("b.com","/",setCookie); 490 fail("HttpException exception should have been thrown"); 491 } catch (HttpException e) { 492 // expected 493 } 494 } 495 496 /*** 497 * Domain has no embedded dots again 498 */ 499 public void testParseWithIllegalDomain2() throws Exception { 500 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com.; version=1"); 501 try { 502 Cookie[] parsed = cookieParse("b.com","/",setCookie); 503 fail("HttpException exception should have been thrown"); 504 } catch (HttpException e) { 505 // expected 506 } 507 } 508 509 public void testParseWithIllegalNetscapeDomain1() throws Exception { 510 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com"); 511 try { 512 Cookie[] parsed = netscapeCcookieParse("a.com","/",setCookie); 513 fail("HttpException exception should have been thrown"); 514 } catch (HttpException e) { 515 // expected 516 } 517 } 518 519 public void testParseWithWrongNetscapeDomain2() throws Exception { 520 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.y.z"); 521 try { 522 Cookie[] parsed = netscapeCcookieParse("x.y.z","/",setCookie); 523 fail("HttpException exception should have been thrown"); 524 } catch (HttpException e) { 525 // expected 526 } 527 } 528 529 public void testParseWithWrongPath() throws Exception { 530 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1; path=/not/just/root"); 531 try { 532 Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie); 533 fail("HttpException exception should have been thrown"); 534 } catch (HttpException e) { 535 // expected 536 } 537 } 538 539 public void testParseWithNullDomain() { 540 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1; path=/; secure"); 541 try { 542 Cookie[] parsed = cookieParse(null,"/",false,setCookie); 543 fail("IllegalArgumentException should have been thrown"); 544 } catch (IllegalArgumentException e) { 545 // expected 546 } catch (Exception e){ 547 fail("Should have thrown IllegalArgumentException."); 548 } 549 } 550 551 public void testParseWithNullPath() { 552 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1; path=/; secure"); 553 try { 554 Cookie[] parsed = cookieParse("127.0.0.1",null,false,setCookie); 555 fail("IllegalArgumentException should have been thrown"); 556 } catch (IllegalArgumentException e) { 557 // expected 558 } catch (Exception e){ 559 fail("Should have thrown IllegalArgumentException."); 560 } 561 } 562 563 public void testParseWithNullDomainAndPath() { 564 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1; path=/; secure"); 565 try { 566 Cookie[] parsed = cookieParse(null,null,false,setCookie); 567 fail("IllegalArgumentException should have been thrown"); 568 } catch (IllegalArgumentException e) { 569 // expected 570 } catch (Exception e){ 571 fail("Should have thrown IllegalArgumentException."); 572 } 573 } 574 575 public void testParseWithPathMismatch() { 576 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; path=/path/path/path"); 577 try { 578 Cookie[] parsed = cookieParse("127.0.0.1","/path",false,setCookie); 579 fail("HttpException should have been thrown."); 580 } catch (HttpException e) { 581 // expected 582 } catch (Exception e){ 583 fail("Should have thrown HttpException."); 584 } 585 } 586 587 public void testParseWithPathMismatch2() { 588 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; path=/foobar"); 589 try { 590 Cookie[] parsed = cookieParse("127.0.0.1","/foo",false,setCookie); 591 fail("HttpException should have been thrown."); 592 } catch (HttpException e) { 593 // expected 594 } catch (Exception e){ 595 fail("Should have thrown HttpException."); 596 } 597 } 598 599 public void testComparator() throws Exception { 600 Header setCookie = null; 601 Cookie[] parsed = null; 602 Vector cookies = new Vector(); 603 // Cookie 0 604 setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.apache.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 605 parsed = cookieParse(".apache.org", "/commons/httpclient", true, 606 setCookie); 607 cookies.add(parsed[0]); 608 // Cookie 1 609 setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons/bif;Domain=.apache.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 610 parsed = cookieParse(".apache.org","/commons/bif/httpclient",true,setCookie); 611 cookies.add(parsed[0]); 612 // Cookie 2 613 setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.baz.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 614 parsed = cookieParse(".baz.org","/commons/httpclient",true,setCookie); 615 cookies.add(parsed[0]); 616 // Cookie 3 617 setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons/bif;Domain=.baz.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 618 parsed = cookieParse(".baz.org","/commons/bif/httpclient",true,setCookie); 619 cookies.add(parsed[0]); 620 // Cookie 4 621 setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.baz.com;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 622 parsed = cookieParse(".baz.com","/commons/httpclient",true,setCookie); 623 cookies.add(parsed[0]); 624 // The order should be: 625 // 1, 0, 3, 2, 4 626 parsed = (Cookie[])cookies.toArray(new Cookie[0]); 627 SortedSet set = new TreeSet(parsed[0]); 628 int pass = 0; 629 for (Iterator itr = set.iterator(); itr.hasNext();) { 630 Cookie cookie = (Cookie)itr.next(); 631 switch (pass) { 632 case 0: 633 assertTrue("0th cookie should be cookie[1]", cookie == parsed[1]); 634 break; 635 case 1: 636 assertTrue("1st cookie should be cookie[0]", cookie == parsed[0]); 637 break; 638 case 2: 639 assertTrue("2nd cookie should be cookie[3]", cookie == parsed[3]); 640 break; 641 case 3: 642 assertTrue("3rd cookie should be cookie[2]", cookie == parsed[2]); 643 break; 644 case 4: 645 assertTrue("4th cookie should be cookie[4]", cookie == parsed[4]); 646 break; 647 default: 648 fail("This should never happen."); 649 } 650 pass++; 651 } 652 try { 653 parsed[0].compare("foo", "bar"); 654 fail("Should have thrown an exception trying to compare non-cookies"); 655 } 656 catch (ClassCastException ex) { 657 // expected 658 } 659 } 660 661 /*** Call Cookie.createCookieHeader providing null for domain to match on 662 */ 663 public void testCreateCookieHeaderWithNullDomain() throws Exception { 664 Header setCookie = new Header("Set-Cookie", 665 TEST_COOKIE + SEP + OLD_EXPIRY); 666 Cookie[] parsed = cookieParse(DOMAIN_NAME, ROOT_PATH, true, setCookie); 667 668 try{ 669 Header header = cookieCreateHeader(null, DEFAULT_PORT, ROOT_PATH, false, parsed); 670 fail("IllegalArgumentException should have been thrown."); 671 }catch(IllegalArgumentException e){ 672 // Expected 673 }catch(Exception e){ 674 fail("Threw wrong type of exception. Expected IllegalArgumentException."); 675 } 676 } 677 678 /*** Call Cookie.createCookieHeader providing null for path to match on 679 */ 680 public void testCreateCookieHeaderWithNullPath() throws Exception{ 681 Header setCookie = new Header("Set-Cookie", 682 TEST_COOKIE + SEP + OLD_EXPIRY); 683 Cookie[] parsed = cookieParse(DOMAIN_NAME, ROOT_PATH, false, setCookie); 684 685 try{ 686 Header header = cookieCreateHeader(DOMAIN_NAME, DEFAULT_PORT, null, false, parsed); 687 fail("IllegalArgumentException should have been thrown."); 688 }catch(IllegalArgumentException e){ 689 // Expected 690 }catch(Exception e){ 691 fail("Threw wrong type of exception. Expected IllegalArgumentException."); 692 } 693 } 694 695 /*** 696 * Verify that cookies with no domain or path don't get added to a cookie header. 697 */ 698 public void testCreateCookieHeaderWithUninitializedCookies() throws Exception { 699 Cookie cookies[] = new Cookie[2]; 700 cookies[0] = new Cookie(null, "name0", "value0"); 701 cookies[1] = new Cookie(null, "name1", "value1", null, null, false); 702 703 Header header = cookieCreateHeader(DOMAIN_NAME, DEFAULT_PORT, ROOT_PATH, false, cookies); 704 assertEquals("createCookieHeader added cookies with null domains or paths", null, header); 705 } 706 707 /*** Call Cookie.createCookieHeader providing null for domain and path to 708 * match on 709 */ 710 public void testCreateCookieHeaderWithNullDomainAndPath() throws Exception { 711 Header setCookie = new Header("Set-Cookie", 712 TEST_COOKIE + SEP + OLD_EXPIRY); 713 Cookie[] parsed = cookieParse(DOMAIN_NAME, ROOT_PATH, true, setCookie); 714 715 try{ 716 Header header = cookieCreateHeader(null, DEFAULT_PORT, null, false, parsed); 717 fail("IllegalArgumentException should have been thrown."); 718 }catch(IllegalArgumentException e){ 719 // Expected 720 }catch(Exception e){ 721 fail("Threw wrong type of exception. Expected IllegalArgumentException."); 722 } 723 } 724 725 /*** 726 * Tests several date formats. 727 */ 728 public void testDateFormats() throws Exception { 729 //comma, dashes 730 checkDate("Thu, 01-Jan-70 00:00:10 GMT"); 731 checkDate("Thu, 01-Jan-2070 00:00:10 GMT"); 732 //no comma, dashes 733 checkDate("Thu 01-Jan-70 00:00:10 GMT"); 734 checkDate("Thu 01-Jan-2070 00:00:10 GMT"); 735 //comma, spaces 736 checkDate("Thu, 01 Jan 70 00:00:10 GMT"); 737 checkDate("Thu, 01 Jan 2070 00:00:10 GMT"); 738 //no comma, spaces 739 checkDate("Thu 01 Jan 70 00:00:10 GMT"); 740 checkDate("Thu 01 Jan 2070 00:00:10 GMT"); 741 //weird stuff 742 checkDate("Wed, 20-Nov-2002 09-38-33 GMT"); 743 744 745 try { 746 checkDate("this aint a date"); 747 fail("Date check is bogous"); 748 } catch(Exception e) { 749 /* must fail */ 750 } 751 } 752 753 private void checkDate(String date) throws Exception { 754 Header setCookie = new Header("Set-Cookie", "custno=12345;Expires='"+date+"'"); 755 cookieParse("localhost","/",setCookie); 756 } 757 758 759 /*** 760 * Tests default constructor. 761 */ 762 public void testDefaultConsttuctor() { 763 Cookie dummy = new Cookie(); 764 assertEquals( "noname=", dummy.toExternalForm() ); 765 } 766 767 /*** 768 * Tests whether domain attribute check is case-insensitive. 769 */ 770 public void testDomainCaseInsensitivity() { 771 Header setCookie = new Header( 772 "Set-Cookie", "name=value; path=/; domain=.whatever.com"); 773 try { 774 Cookie[] parsed = cookieParse("www.WhatEver.com", "/", false, setCookie ); 775 } 776 catch(HttpException e) { 777 e.printStackTrace(); 778 fail("Unexpected exception: " + e.toString()); 779 } 780 } 781 782 783 /*** 784 * Tests if cookie constructor rejects cookie name containing blanks. 785 */ 786 public void testInvalidCookieName() { 787 try 788 { 789 CookieSpec parser = new CookieSpecBase(); 790 parser.parse("localhost", 80, "/", false, "invalid name="); 791 fail("MalformedCookieException must have been thrown"); 792 } 793 catch(MalformedCookieException e) 794 { 795 // Expected 796 } 797 } 798 799 800 /*** 801 * Tests if cookie constructor rejects cookie name starting with $. 802 */ 803 public void testInvalidCookieName2() { 804 try 805 { 806 CookieSpec parser = new CookieSpecBase(); 807 parser.parse("localhost", 80, "/", false, "$invalid_name="); 808 fail("MalformedCookieException must have been thrown"); 809 } 810 catch(MalformedCookieException e) 811 { 812 // Expected 813 } 814 } 815 816 /*** 817 * Tests if default cookie validator rejects cookies originating from a host without domain 818 * where domain attribute does not match the host of origin 819 */ 820 821 public void testInvalidDomainWithSimpleHostName() { 822 CookieSpec parser = CookiePolicy.getDefaultSpec(); 823 Header setCookie = null; 824 Cookie[] cookies = null; 825 try { 826 setCookie = new Header( 827 "Set-Cookie", "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\""); 828 cookies = parser.parse("host", 80, "/", false, setCookie ); 829 try { 830 parser.validate("host", 80, "/", false, cookies[0]); 831 fail("MalformedCookieException must have thrown"); 832 } 833 catch(MalformedCookieException expected) { 834 } 835 } 836 catch(HttpException e) { 837 e.printStackTrace(); 838 fail("Unexpected exception: " + e.toString()); 839 } 840 try { 841 setCookie = new Header( 842 "Set-Cookie", "name=\"value\"; version=\"1\"; path=\"/\"; domain=\"host1\""); 843 cookies = parser.parse("host2", 80, "/", false, setCookie ); 844 try { 845 parser.validate("host2", 80, "/", false, cookies[0]); 846 fail("MalformedCookieException must have thrown"); 847 } 848 catch(MalformedCookieException expected) { 849 } 850 } 851 catch(HttpException e) { 852 e.printStackTrace(); 853 fail("Unexpected exception: " + e.toString()); 854 } 855 } 856 857 /*** 858 * Makes sure that a cookie matches with a path of the same value. 859 */ 860 public void testMatchWithEqualPaths() { 861 862 Cookie cookie = new Cookie(".test.com", "test", "1", "/test", null, false); 863 864 try { 865 boolean match = cookieMatch( 866 "test.test.com", 867 80, 868 "/test", 869 false, 870 cookie 871 ); 872 873 assertTrue("Cookie paths did not match", match); 874 } catch ( Exception e ) { 875 e.printStackTrace(); 876 fail("Unexpected exception: " + e); 877 } 878 879 } 880 881 882 /*** 883 * Tests generic cookie formatting. 884 */ 885 886 public void testGenericCookieFormatting() { 887 Header setCookie = new Header( 888 "Set-Cookie", "name=value; path=/; domain=.mydomain.com"); 889 try { 890 CookieSpec parser = CookiePolicy.getSpecByPolicy(CookiePolicy.COMPATIBILITY); 891 Cookie[] cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 892 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 893 String s = parser.formatCookie(cookies[0]); 894 assertEquals("name=value", s); 895 } 896 catch(HttpException e) { 897 e.printStackTrace(); 898 fail("Unexpected exception: " + e.toString()); 899 } 900 } 901 902 /*** 903 * Tests Netscape specific cookie formatting. 904 */ 905 906 public void testNetscapeCookieFormatting() { 907 Header setCookie = new Header( 908 "Set-Cookie", "name=value; path=/; domain=.mydomain.com"); 909 try { 910 CookieSpec parser = CookiePolicy.getSpecByPolicy(CookiePolicy.NETSCAPE_DRAFT); 911 Cookie[] cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 912 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 913 String s = parser.formatCookie(cookies[0]); 914 assertEquals("name=value", s); 915 } 916 catch(HttpException e) { 917 e.printStackTrace(); 918 fail("Unexpected exception: " + e.toString()); 919 } 920 } 921 922 923 /*** 924 * Tests RFC 2109 compiant cookie formatting. 925 */ 926 927 public void testRFC2109CookieFormatting() { 928 CookieSpec parser = CookiePolicy.getSpecByPolicy(CookiePolicy.RFC2109); 929 Header setCookie = null; 930 Cookie[] cookies = null; 931 try { 932 setCookie = new Header( 933 "Set-Cookie", "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\""); 934 cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 935 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 936 String s1 = parser.formatCookie(cookies[0]); 937 assertEquals(s1, "$Version=\"1\"; name=\"value\"; $Domain=\".mydomain.com\"; $Path=\"/\""); 938 939 setCookie = new Header( 940 "Set-Cookie", "name=value; path=/; domain=.mydomain.com"); 941 cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 942 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 943 String s2 = parser.formatCookie(cookies[0]); 944 assertEquals(s2, "$Version=0; name=value; $Domain=.mydomain.com; $Path=/"); 945 } 946 catch(HttpException e) { 947 e.printStackTrace(); 948 fail("Unexpected exception: " + e.toString()); 949 } 950 } 951 952 953 /*** 954 * Tests Netscape specific expire attribute parsing. 955 */ 956 957 public void testNetscapeCookieExpireAttribute() { 958 CookieSpec parser = CookiePolicy.getSpecByPolicy(CookiePolicy.NETSCAPE_DRAFT); 959 Header setCookie = null; 960 Cookie[] cookies = null; 961 try { 962 setCookie = new Header( 963 "Set-Cookie", "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment"); 964 cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 965 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 966 } 967 catch(MalformedCookieException e) { 968 e.printStackTrace(); 969 fail("Unexpected exception: " + e.toString()); 970 } 971 try { 972 setCookie = new Header( 973 "Set-Cookie", "name=value; path=/; domain=.mydomain.com; expires=Thu 01-Jan-2070 00:00:10 GMT; comment=no_comment"); 974 cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 975 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 976 fail("MalformedCookieException must have been thrown"); 977 } 978 catch(MalformedCookieException e) { 979 //expected 980 } 981 } 982 983 984 /*** 985 * Tests if null cookie values are handled correctly. 986 */ 987 public void testNullCookieValueFormatting() { 988 Cookie cookie = new Cookie(".whatever.com", "name", null, "/", null, false); 989 cookie.setDomainAttributeSpecified(true); 990 cookie.setPathAttributeSpecified(true); 991 992 CookieSpec parser = null; 993 String s = null; 994 995 parser = CookiePolicy.getSpecByPolicy(CookiePolicy.COMPATIBILITY); 996 s = parser.formatCookie(cookie); 997 assertEquals("name=", s); 998 999 parser = CookiePolicy.getSpecByPolicy(CookiePolicy.RFC2109); 1000 s = parser.formatCookie(cookie); 1001 assertEquals("$Version=0; name=; $Domain=.whatever.com; $Path=/", s); 1002 } 1003 1004 /*** 1005 * Tests if that invalid second domain level cookie gets 1006 * rejected in the strict mode, but gets accepted in the 1007 * browser compatibility mode. 1008 */ 1009 public void testSecondDomainLevelCookie() throws Exception { 1010 Cookie cookie = new Cookie(".sourceforge.net", "name", null, "/", null, false); 1011 cookie.setDomainAttributeSpecified(true); 1012 cookie.setPathAttributeSpecified(true); 1013 1014 CookieSpec parser = null; 1015 1016 parser = CookiePolicy.getSpecByPolicy(CookiePolicy.COMPATIBILITY); 1017 parser.validate("sourceforge.net", 80, "/", false, cookie); 1018 1019 parser = CookiePolicy.getSpecByPolicy(CookiePolicy.RFC2109); 1020 try { 1021 parser.validate("sourceforge.net", 80, "/", false, cookie); 1022 fail("MalformedCookieException should have been thrown"); 1023 } catch (MalformedCookieException e) { 1024 // Expected 1025 } 1026 } 1027 } 1028

This page was automatically generated by Maven