1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestCookie.java,v 1.21 2003/03/07 18:23:46 olegk Exp $ 3 * $Revision: 1.21 $ 4 * $Date: 2003/03/07 18:23:46 $ 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.21 $ 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 testParseNoValue() throws Exception { 302 Header setCookie = new Header("Set-Cookie","cookie-name="); 303 Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie); 304 assertEquals("Found 1 cookie.",1,parsed.length); 305 assertEquals("Name","cookie-name",parsed[0].getName()); 306 assertTrue("Value",null == 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 public void testParseWithWhiteSpace() throws Exception { 318 Header setCookie = new Header("Set-Cookie"," cookie-name = cookie-value "); 319 Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie); 320 assertEquals("Found 1 cookie.",1,parsed.length); 321 assertEquals("Name","cookie-name",parsed[0].getName()); 322 assertEquals("Value","cookie-value",parsed[0].getValue()); 323 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 324 assertEquals("Path","/",parsed[0].getPath()); 325 assertTrue("Secure",!parsed[0].getSecure()); 326 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 327 assertTrue("Comment",null == parsed[0].getComment()); 328 } 329 330 public void testParseWithQuotes() throws Exception { 331 Header setCookie = new Header("Set-Cookie"," cookie-name = \" cookie-value \" ;path=/"); 332 Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie); 333 assertEquals("Found 1 cookie.",1,parsed.length); 334 assertEquals("Name","cookie-name",parsed[0].getName()); 335 assertEquals("Value"," cookie-value ",parsed[0].getValue()); 336 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 337 assertEquals("Path","/",parsed[0].getPath()); 338 assertTrue("Secure",!parsed[0].getSecure()); 339 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 340 assertTrue("Comment",null == parsed[0].getComment()); 341 } 342 343 public void testParseWithPath() throws Exception { 344 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; Path=/path/"); 345 Cookie[] parsed = cookieParse("127.0.0.1","/path/path",setCookie); 346 assertEquals("Found 1 cookie.",1,parsed.length); 347 assertEquals("Name","cookie-name",parsed[0].getName()); 348 assertEquals("Value","cookie-value",parsed[0].getValue()); 349 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 350 assertEquals("Path","/path/",parsed[0].getPath()); 351 assertTrue("Secure",!parsed[0].getSecure()); 352 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 353 assertTrue("Comment",null == parsed[0].getComment()); 354 } 355 356 public void testParseWithDomain() throws Exception { 357 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; Domain=127.0.0.1"); 358 Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie); 359 assertEquals("Found 1 cookie.",1,parsed.length); 360 assertEquals("Name","cookie-name",parsed[0].getName()); 361 assertEquals("Value","cookie-value",parsed[0].getValue()); 362 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 363 assertEquals("Path","/",parsed[0].getPath()); 364 assertTrue("Secure",!parsed[0].getSecure()); 365 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 366 assertTrue("Comment",null == parsed[0].getComment()); 367 } 368 369 public void testParseWithSecure() throws Exception { 370 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; secure"); 371 Cookie[] parsed = cookieParse("127.0.0.1","/",true,setCookie); 372 assertEquals("Found 1 cookie.",1,parsed.length); 373 assertEquals("Name","cookie-name",parsed[0].getName()); 374 assertEquals("Value","cookie-value",parsed[0].getValue()); 375 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 376 assertEquals("Path","/",parsed[0].getPath()); 377 assertTrue("Secure",parsed[0].getSecure()); 378 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 379 assertTrue("Comment",null == parsed[0].getComment()); 380 } 381 382 public void testParseWithComment() throws Exception { 383 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; comment=\"This is a comment.\""); 384 Cookie[] parsed = cookieParse("127.0.0.1","/",true,setCookie); 385 assertEquals("Found 1 cookie.",1,parsed.length); 386 assertEquals("Name","cookie-name",parsed[0].getName()); 387 assertEquals("Value","cookie-value",parsed[0].getValue()); 388 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 389 assertEquals("Path","/",parsed[0].getPath()); 390 assertTrue("Secure",!parsed[0].getSecure()); 391 assertTrue("ExpiryDate",null == parsed[0].getExpiryDate()); 392 assertEquals("Comment","This is a comment.",parsed[0].getComment()); 393 } 394 395 public void testParseWithExpires() throws Exception { 396 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 397 Cookie[] parsed = cookieParse("127.0.0.1","/",true,setCookie); 398 assertEquals("Found 1 cookie.",1,parsed.length); 399 assertEquals("Name","cookie-name",parsed[0].getName()); 400 assertEquals("Value","cookie-value",parsed[0].getValue()); 401 assertEquals("Domain","127.0.0.1",parsed[0].getDomain()); 402 assertEquals("Path","/",parsed[0].getPath()); 403 assertTrue("Secure",!parsed[0].getSecure()); 404 assertEquals(new Date(10000L),parsed[0].getExpiryDate()); 405 assertTrue("Comment",null == parsed[0].getComment()); 406 } 407 408 public void testParseWithAll() throws Exception { 409 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"); 410 Cookie[] parsed = cookieParse(".apache.org","/commons/httpclient",true,setCookie); 411 assertEquals("Found 1 cookie.",1,parsed.length); 412 assertEquals("Name","cookie-name",parsed[0].getName()); 413 assertEquals("Value","cookie-value",parsed[0].getValue()); 414 assertEquals("Domain",".apache.org",parsed[0].getDomain()); 415 assertEquals("Path","/commons",parsed[0].getPath()); 416 assertTrue("Secure",parsed[0].getSecure()); 417 assertEquals(new Date(10000L),parsed[0].getExpiryDate()); 418 assertEquals("Comment","This is a comment.",parsed[0].getComment()); 419 assertEquals("Version",1,parsed[0].getVersion()); 420 } 421 422 public void testParseMultipleDifferentPaths() throws Exception { 423 Header setCookie = new Header("Set-Cookie","name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons/httpclient;Version=1"); 424 Cookie[] parsed = cookieParse(".apache.org","/commons/httpclient",true,setCookie); 425 HttpState state = new HttpState(); 426 state.addCookies(parsed); 427 Cookie[] cookies = state.getCookies(); 428 assertEquals("Wrong number of cookies.",2,cookies.length); 429 assertEquals("Name","name1",cookies[0].getName()); 430 assertEquals("Value","value1",cookies[0].getValue()); 431 assertEquals("Name","name1",cookies[1].getName()); 432 assertEquals("Value","value2",cookies[1].getValue()); 433 } 434 435 public void testParseMultipleSamePaths() throws Exception { 436 Header setCookie = new Header("Set-Cookie","name1=value1;Version=1;Path=/commons,name1=value2;Version=1;Path=/commons"); 437 Cookie[] parsed = cookieParse(".apache.org","/commons/httpclient",true,setCookie); 438 HttpState state = new HttpState(); 439 state.addCookies(parsed); 440 Cookie[] cookies = state.getCookies(); 441 assertEquals("Found 1 cookies.",1,cookies.length); 442 assertEquals("Name","name1",cookies[0].getName()); 443 assertEquals("Value","value2",cookies[0].getValue()); 444 } 445 446 public void testParseWithWrongDomain() throws Exception { 447 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1; version=1"); 448 try { 449 Cookie[] parsed = cookieParse("127.0.0.2","/",setCookie); 450 fail("HttpException exception should have been thrown"); 451 } catch (HttpException e) { 452 // expected 453 } 454 } 455 456 public void testParseWithWrongDomain2() throws Exception { 457 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.c.com; version=1"); 458 try { 459 Cookie[] parsed = cookieParse("a.b.c.com","/",setCookie); 460 fail("HttpException exception should have been thrown"); 461 } catch (HttpException e) { 462 // expected 463 } 464 } 465 466 /*** 467 * Domain has no embedded dots 468 */ 469 public void testParseWithIllegalDomain() throws Exception { 470 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com; version=1"); 471 try { 472 Cookie[] parsed = cookieParse("b.com","/",setCookie); 473 fail("HttpException exception should have been thrown"); 474 } catch (HttpException e) { 475 // expected 476 } 477 } 478 479 /*** 480 * Domain has no embedded dots again 481 */ 482 public void testParseWithIllegalDomain2() throws Exception { 483 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com.; version=1"); 484 try { 485 Cookie[] parsed = cookieParse("b.com","/",setCookie); 486 fail("HttpException exception should have been thrown"); 487 } catch (HttpException e) { 488 // expected 489 } 490 } 491 492 public void testParseWithIllegalNetscapeDomain1() throws Exception { 493 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.com"); 494 try { 495 Cookie[] parsed = netscapeCcookieParse("a.com","/",setCookie); 496 fail("HttpException exception should have been thrown"); 497 } catch (HttpException e) { 498 // expected 499 } 500 } 501 502 public void testParseWithWrongNetscapeDomain2() throws Exception { 503 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=.y.z"); 504 try { 505 Cookie[] parsed = netscapeCcookieParse("x.y.z","/",setCookie); 506 fail("HttpException exception should have been thrown"); 507 } catch (HttpException e) { 508 // expected 509 } 510 } 511 512 public void testParseWithWrongPath() throws Exception { 513 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1; path=/not/just/root"); 514 try { 515 Cookie[] parsed = cookieParse("127.0.0.1","/",setCookie); 516 fail("HttpException exception should have been thrown"); 517 } catch (HttpException e) { 518 // expected 519 } 520 } 521 522 public void testParseWithNullDomain() { 523 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1; path=/; secure"); 524 try { 525 Cookie[] parsed = cookieParse(null,"/",false,setCookie); 526 fail("IllegalArgumentException should have been thrown"); 527 } catch (IllegalArgumentException e) { 528 // expected 529 } catch (Exception e){ 530 fail("Should have thrown IllegalArgumentException."); 531 } 532 } 533 534 public void testParseWithNullPath() { 535 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1; path=/; secure"); 536 try { 537 Cookie[] parsed = cookieParse("127.0.0.1",null,false,setCookie); 538 fail("IllegalArgumentException should have been thrown"); 539 } catch (IllegalArgumentException e) { 540 // expected 541 } catch (Exception e){ 542 fail("Should have thrown IllegalArgumentException."); 543 } 544 } 545 546 public void testParseWithNullDomainAndPath() { 547 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; domain=127.0.0.1; path=/; secure"); 548 try { 549 Cookie[] parsed = cookieParse(null,null,false,setCookie); 550 fail("IllegalArgumentException should have been thrown"); 551 } catch (IllegalArgumentException e) { 552 // expected 553 } catch (Exception e){ 554 fail("Should have thrown IllegalArgumentException."); 555 } 556 } 557 558 public void testParseWithPathMismatch() { 559 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; path=/path/path/path"); 560 try { 561 Cookie[] parsed = cookieParse("127.0.0.1","/path",false,setCookie); 562 fail("HttpException should have been thrown."); 563 } catch (HttpException e) { 564 // expected 565 } catch (Exception e){ 566 fail("Should have thrown HttpException."); 567 } 568 } 569 570 public void testParseWithPathMismatch2() { 571 Header setCookie = new Header("Set-Cookie","cookie-name=cookie-value; path=/foobar"); 572 try { 573 Cookie[] parsed = cookieParse("127.0.0.1","/foo",false,setCookie); 574 fail("HttpException should have been thrown."); 575 } catch (HttpException e) { 576 // expected 577 } catch (Exception e){ 578 fail("Should have thrown HttpException."); 579 } 580 } 581 582 public void testComparator() throws Exception { 583 Header setCookie = null; 584 Cookie[] parsed = null; 585 Vector cookies = new Vector(); 586 // Cookie 0 587 setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.apache.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 588 parsed = cookieParse(".apache.org", "/commons/httpclient", true, 589 setCookie); 590 cookies.add(parsed[0]); 591 // Cookie 1 592 setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons/bif;Domain=.apache.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 593 parsed = cookieParse(".apache.org","/commons/bif/httpclient",true,setCookie); 594 cookies.add(parsed[0]); 595 // Cookie 2 596 setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.baz.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 597 parsed = cookieParse(".baz.org","/commons/httpclient",true,setCookie); 598 cookies.add(parsed[0]); 599 // Cookie 3 600 setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons/bif;Domain=.baz.org;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 601 parsed = cookieParse(".baz.org","/commons/bif/httpclient",true,setCookie); 602 cookies.add(parsed[0]); 603 // Cookie 4 604 setCookie = new Header("Set-Cookie","cookie-name=cookie-value;Path=/commons;Domain=.baz.com;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 605 parsed = cookieParse(".baz.com","/commons/httpclient",true,setCookie); 606 cookies.add(parsed[0]); 607 // The order should be: 608 // 1, 0, 3, 2, 4 609 parsed = (Cookie[])cookies.toArray(new Cookie[0]); 610 SortedSet set = new TreeSet(parsed[0]); 611 int pass = 0; 612 for (Iterator itr = set.iterator(); itr.hasNext();) { 613 Cookie cookie = (Cookie)itr.next(); 614 switch (pass) { 615 case 0: 616 assertTrue("0th cookie should be cookie[1]", cookie == parsed[1]); 617 break; 618 case 1: 619 assertTrue("1st cookie should be cookie[0]", cookie == parsed[0]); 620 break; 621 case 2: 622 assertTrue("2nd cookie should be cookie[3]", cookie == parsed[3]); 623 break; 624 case 3: 625 assertTrue("3rd cookie should be cookie[2]", cookie == parsed[2]); 626 break; 627 case 4: 628 assertTrue("4th cookie should be cookie[4]", cookie == parsed[4]); 629 break; 630 default: 631 fail("This should never happen."); 632 } 633 pass++; 634 } 635 try { 636 parsed[0].compare("foo", "bar"); 637 fail("Should have thrown an exception trying to compare non-cookies"); 638 } 639 catch (ClassCastException ex) { 640 // expected 641 } 642 } 643 644 /*** Call Cookie.createCookieHeader providing null for domain to match on 645 */ 646 public void testCreateCookieHeaderWithNullDomain() throws Exception { 647 Header setCookie = new Header("Set-Cookie", 648 TEST_COOKIE + SEP + OLD_EXPIRY); 649 Cookie[] parsed = cookieParse(DOMAIN_NAME, ROOT_PATH, true, setCookie); 650 651 try{ 652 Header header = cookieCreateHeader(null, DEFAULT_PORT, ROOT_PATH, false, parsed); 653 fail("IllegalArgumentException should have been thrown."); 654 }catch(IllegalArgumentException e){ 655 // Expected 656 }catch(Exception e){ 657 fail("Threw wrong type of exception. Expected IllegalArgumentException."); 658 } 659 } 660 661 /*** Call Cookie.createCookieHeader providing null for path to match on 662 */ 663 public void testCreateCookieHeaderWithNullPath() throws Exception{ 664 Header setCookie = new Header("Set-Cookie", 665 TEST_COOKIE + SEP + OLD_EXPIRY); 666 Cookie[] parsed = cookieParse(DOMAIN_NAME, ROOT_PATH, false, setCookie); 667 668 try{ 669 Header header = cookieCreateHeader(DOMAIN_NAME, DEFAULT_PORT, null, 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 /*** 679 * Verify that cookies with no domain or path don't get added to a cookie header. 680 */ 681 public void testCreateCookieHeaderWithUninitializedCookies() throws Exception { 682 Cookie cookies[] = new Cookie[2]; 683 cookies[0] = new Cookie(null, "name0", "value0"); 684 cookies[1] = new Cookie(null, "name1", "value1", null, null, false); 685 686 Header header = cookieCreateHeader(DOMAIN_NAME, DEFAULT_PORT, ROOT_PATH, false, cookies); 687 assertEquals("createCookieHeader added cookies with null domains or paths", null, header); 688 } 689 690 /*** Call Cookie.createCookieHeader providing null for domain and path to 691 * match on 692 */ 693 public void testCreateCookieHeaderWithNullDomainAndPath() throws Exception { 694 Header setCookie = new Header("Set-Cookie", 695 TEST_COOKIE + SEP + OLD_EXPIRY); 696 Cookie[] parsed = cookieParse(DOMAIN_NAME, ROOT_PATH, true, setCookie); 697 698 try{ 699 Header header = cookieCreateHeader(null, DEFAULT_PORT, null, false, parsed); 700 fail("IllegalArgumentException should have been thrown."); 701 }catch(IllegalArgumentException e){ 702 // Expected 703 }catch(Exception e){ 704 fail("Threw wrong type of exception. Expected IllegalArgumentException."); 705 } 706 } 707 708 /*** 709 * Tests several date formats. 710 */ 711 public void testDateFormats() throws Exception { 712 //comma, dashes 713 checkDate("Thu, 01-Jan-70 00:00:10 GMT"); 714 checkDate("Thu, 01-Jan-2070 00:00:10 GMT"); 715 //no comma, dashes 716 checkDate("Thu 01-Jan-70 00:00:10 GMT"); 717 checkDate("Thu 01-Jan-2070 00:00:10 GMT"); 718 //comma, spaces 719 checkDate("Thu, 01 Jan 70 00:00:10 GMT"); 720 checkDate("Thu, 01 Jan 2070 00:00:10 GMT"); 721 //no comma, spaces 722 checkDate("Thu 01 Jan 70 00:00:10 GMT"); 723 checkDate("Thu 01 Jan 2070 00:00:10 GMT"); 724 //weird stuff 725 checkDate("Wed, 20-Nov-2002 09-38-33 GMT"); 726 727 728 try { 729 checkDate("this aint a date"); 730 fail("Date check is bogous"); 731 } catch(Exception e) { 732 /* must fail */ 733 } 734 } 735 736 private void checkDate(String date) throws Exception { 737 Header setCookie = new Header("Set-Cookie", "custno=12345;Expires='"+date+"'"); 738 cookieParse("localhost","/",setCookie); 739 } 740 741 742 /*** 743 * Tests default constructor. 744 */ 745 public void testDefaultConsttuctor() { 746 Cookie dummy = new Cookie(); 747 assertEquals( "noname=", dummy.toExternalForm() ); 748 } 749 750 /*** 751 * Tests whether domain attribute check is case-insensitive. 752 */ 753 public void testDomainCaseInsensitivity() { 754 Header setCookie = new Header( 755 "Set-Cookie", "name=value; path=/; domain=.whatever.com"); 756 try { 757 Cookie[] parsed = cookieParse("www.WhatEver.com", "/", false, setCookie ); 758 } 759 catch(HttpException e) { 760 e.printStackTrace(); 761 fail("Unexpected exception: " + e.toString()); 762 } 763 } 764 765 766 /*** 767 * Tests if cookie constructor rejects cookie name containing blanks. 768 */ 769 public void testInvalidCookieName() { 770 try 771 { 772 Cookie dummy = new Cookie("localhost", "invalid name", "cooke name may not have blanks"); 773 fail("IllegalArgumentException must have been thrown"); 774 } 775 catch(IllegalArgumentException e) 776 { 777 // Expected 778 } 779 } 780 781 782 /*** 783 * Tests if cookie constructor rejects cookie name starting with $. 784 */ 785 public void testInvalidCookieName2() { 786 try 787 { 788 Cookie dummy = new Cookie("localhost", "$invalid_name", "cooke name may not start with $"); 789 fail("IllegalArgumentException must have been thrown"); 790 } 791 catch(IllegalArgumentException e) 792 { 793 // Expected 794 } 795 } 796 797 /*** 798 * Tests if default cookie validator rejects cookies originating from a host without domain 799 * where domain attribute does not match the host of origin 800 */ 801 802 public void testInvalidDomainWithSimpleHostName() { 803 CookieSpec parser = CookiePolicy.getDefaultSpec(); 804 Header setCookie = null; 805 Cookie[] cookies = null; 806 try { 807 setCookie = new Header( 808 "Set-Cookie", "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\""); 809 cookies = parser.parse("host", 80, "/", false, setCookie ); 810 try { 811 parser.validate("host", 80, "/", false, cookies[0]); 812 fail("MalformedCookieException must have thrown"); 813 } 814 catch(MalformedCookieException expected) { 815 } 816 } 817 catch(HttpException e) { 818 e.printStackTrace(); 819 fail("Unexpected exception: " + e.toString()); 820 } 821 try { 822 setCookie = new Header( 823 "Set-Cookie", "name=\"value\"; version=\"1\"; path=\"/\"; domain=\"host1\""); 824 cookies = parser.parse("host2", 80, "/", false, setCookie ); 825 try { 826 parser.validate("host2", 80, "/", false, cookies[0]); 827 fail("MalformedCookieException must have thrown"); 828 } 829 catch(MalformedCookieException expected) { 830 } 831 } 832 catch(HttpException e) { 833 e.printStackTrace(); 834 fail("Unexpected exception: " + e.toString()); 835 } 836 } 837 838 /*** 839 * Makes sure that a cookie matches with a path of the same value. 840 */ 841 public void testMatchWithEqualPaths() { 842 843 Cookie cookie = new Cookie(".test.com", "test", "1", "/test", null, false); 844 845 try { 846 boolean match = cookieMatch( 847 "test.test.com", 848 80, 849 "/test", 850 false, 851 cookie 852 ); 853 854 assertTrue("Cookie paths did not match", match); 855 } catch ( Exception e ) { 856 e.printStackTrace(); 857 fail("Unexpected exception: " + e); 858 } 859 860 } 861 862 863 /*** 864 * Tests generic cookie formatting. 865 */ 866 867 public void testGenericCookieFormatting() { 868 Header setCookie = new Header( 869 "Set-Cookie", "name=value; path=/; domain=.mydomain.com"); 870 try { 871 CookieSpec parser = CookiePolicy.getSpecByPolicy(CookiePolicy.COMPATIBILITY); 872 Cookie[] cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 873 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 874 String s = parser.formatCookie(cookies[0]); 875 assertEquals("name=value", s); 876 } 877 catch(HttpException e) { 878 e.printStackTrace(); 879 fail("Unexpected exception: " + e.toString()); 880 } 881 } 882 883 /*** 884 * Tests Netscape specific cookie formatting. 885 */ 886 887 public void testNetscapeCookieFormatting() { 888 Header setCookie = new Header( 889 "Set-Cookie", "name=value; path=/; domain=.mydomain.com"); 890 try { 891 CookieSpec parser = CookiePolicy.getSpecByPolicy(CookiePolicy.NETSCAPE_DRAFT); 892 Cookie[] cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 893 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 894 String s = parser.formatCookie(cookies[0]); 895 assertEquals("name=value", s); 896 } 897 catch(HttpException e) { 898 e.printStackTrace(); 899 fail("Unexpected exception: " + e.toString()); 900 } 901 } 902 903 904 /*** 905 * Tests RFC 2109 compiant cookie formatting. 906 */ 907 908 public void testRFC2109CookieFormatting() { 909 CookieSpec parser = CookiePolicy.getSpecByPolicy(CookiePolicy.RFC2109); 910 Header setCookie = null; 911 Cookie[] cookies = null; 912 try { 913 setCookie = new Header( 914 "Set-Cookie", "name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\""); 915 cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 916 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 917 String s1 = parser.formatCookie(cookies[0]); 918 assertEquals(s1, "$Version=\"1\"; name=\"value\"; $Domain=\".mydomain.com\"; $Path=\"/\""); 919 920 setCookie = new Header( 921 "Set-Cookie", "name=value; path=/; domain=.mydomain.com"); 922 cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 923 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 924 String s2 = parser.formatCookie(cookies[0]); 925 assertEquals(s2, "$Version=0; name=value; $Domain=.mydomain.com; $Path=/"); 926 } 927 catch(HttpException e) { 928 e.printStackTrace(); 929 fail("Unexpected exception: " + e.toString()); 930 } 931 } 932 933 934 /*** 935 * Tests Netscape specific expire attribute parsing. 936 */ 937 938 public void testNetscapeCookieExpireAttribute() { 939 CookieSpec parser = CookiePolicy.getSpecByPolicy(CookiePolicy.NETSCAPE_DRAFT); 940 Header setCookie = null; 941 Cookie[] cookies = null; 942 try { 943 setCookie = new Header( 944 "Set-Cookie", "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment"); 945 cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 946 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 947 } 948 catch(MalformedCookieException e) { 949 e.printStackTrace(); 950 fail("Unexpected exception: " + e.toString()); 951 } 952 try { 953 setCookie = new Header( 954 "Set-Cookie", "name=value; path=/; domain=.mydomain.com; expires=Thu 01-Jan-2070 00:00:10 GMT; comment=no_comment"); 955 cookies = parser.parse("myhost.mydomain.com", 80, "/", false, setCookie ); 956 parser.validate("myhost.mydomain.com", 80, "/", false, cookies[0]); 957 fail("MalformedCookieException must have been thrown"); 958 } 959 catch(MalformedCookieException e) { 960 //expected 961 } 962 } 963 964 965 /*** 966 * Tests if null cookie values are handled correctly. 967 */ 968 public void testNullCookieValueFormatting() { 969 Cookie cookie = new Cookie(".whatever.com", "name", null, "/", null, false); 970 cookie.setDomainAttributeSpecified(true); 971 cookie.setPathAttributeSpecified(true); 972 973 CookieSpec parser = null; 974 String s = null; 975 976 parser = CookiePolicy.getSpecByPolicy(CookiePolicy.COMPATIBILITY); 977 s = parser.formatCookie(cookie); 978 assertEquals("name=", s); 979 980 parser = CookiePolicy.getSpecByPolicy(CookiePolicy.RFC2109); 981 s = parser.formatCookie(cookie); 982 assertEquals("$Version=0; name=; $Domain=.whatever.com; $Path=/", s); 983 } 984 985 986 } 987

This page was automatically generated by Maven