1 30 31 package org.apache.commons.httpclient; 32 33 import junit.framework.*; 34 35 import org.apache.commons.httpclient.cookie.CookiePolicy; 36 import org.apache.commons.httpclient.methods.*; 37 38 59 public class TestWebappCookie extends TestWebappBase { 60 61 public TestWebappCookie(String testName) { 62 super(testName); 63 } 64 65 public static Test suite() { 66 TestSuite suite = new TestSuite(TestWebappCookie.class); 67 return suite; 68 } 69 70 public static void main(String args[]) { 71 String [] testCaseName = { TestWebappCookie.class.getName() }; 72 junit.textui.TestRunner.main(testCaseName); 73 } 74 75 76 78 public void testSetCookieGet() throws Exception { 79 HttpClient client = createHttpClient(); 80 client.setStrictMode(true); 81 82 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 83 method.setQueryString("simple=set"); 84 85 try { 86 client.executeMethod(method); 87 } catch (Throwable t) { 88 t.printStackTrace(); 89 fail("Unable to execute method : " + t.toString()); 90 } 91 assertEquals(200,method.getStatusCode()); 92 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 93 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 94 assertEquals(1,client.getState().getCookies().length); 95 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 96 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 97 } 98 99 public void testSetCookiePost() throws Exception { 100 HttpClient client = createHttpClient(); 101 client.setStrictMode(true); 102 103 PostMethod method = new PostMethod("/" + getWebappContext() + "/cookie/write"); 104 method.setRequestBody(new NameValuePair[] { new NameValuePair("simple","set") } ); 105 106 try { 107 client.executeMethod(method); 108 } catch (Throwable t) { 109 t.printStackTrace(); 110 fail("Unable to execute method : " + t.toString()); 111 } 112 assertEquals(200,method.getStatusCode()); 113 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: POST</title>") >= 0); 114 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 115 assertEquals(1,client.getState().getCookies().length); 116 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 117 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 118 } 119 120 public void testSetCookiePut() throws Exception { 121 HttpClient client = createHttpClient(); 122 client.setStrictMode(true); 123 124 PutMethod method = new PutMethod("/" + getWebappContext() + "/cookie/write"); 125 method.setQueryString("simple=set"); 126 method.setRequestBody("data to be sent via http post"); 127 try { 128 client.executeMethod(method); 129 } catch (Throwable t) { 130 t.printStackTrace(); 131 fail("Unable to execute method : " + t.toString()); 132 } 133 assertEquals(200,method.getStatusCode()); 134 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: PUT</title>") >= 0); 135 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 136 assertEquals(1,client.getState().getCookies().length); 137 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 138 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 139 } 140 141 public void testSetExpiredCookieGet() throws Exception { 142 HttpClient client = createHttpClient(); 143 client.setStrictMode(true); 144 145 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 146 method.setQueryString("simple=unset"); 147 148 try { 149 client.executeMethod(method); 150 } catch (Throwable t) { 151 t.printStackTrace(); 152 fail("Unable to execute method : " + t.toString()); 153 } 154 assertEquals(200,method.getStatusCode()); 155 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 156 assertTrue(method.getResponseBodyAsString().indexOf("Deleted simplecookie.<br>") >= 0); 157 assertEquals(0,client.getState().getCookies().length); 158 } 159 160 public void testSetExpiredCookiePut() throws Exception { 161 HttpClient client = createHttpClient(); 162 client.setStrictMode(true); 163 164 PutMethod method = new PutMethod("/" + getWebappContext() + "/cookie/write"); 165 method.setQueryString("simple=unset"); 166 method.setRequestBody("data to be sent via http post"); 167 try { 168 client.executeMethod(method); 169 } catch (Throwable t) { 170 t.printStackTrace(); 171 fail("Unable to execute method : " + t.toString()); 172 } 173 assertEquals(200,method.getStatusCode()); 174 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: PUT</title>") >= 0); 175 assertTrue(method.getResponseBodyAsString().indexOf("Deleted simplecookie.<br>") >= 0); 176 assertEquals(0,client.getState().getCookies().length); 177 } 178 179 public void testSetUnsetCookieGet() throws Exception { 180 HttpClient client = createHttpClient(); 181 client.setStrictMode(true); 182 183 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 184 method.setQueryString("simple=set"); 185 186 try { 187 client.executeMethod(method); 188 } catch (Throwable t) { 189 t.printStackTrace(); 190 fail("Unable to execute method : " + t.toString()); 191 } 192 assertEquals(200,method.getStatusCode()); 193 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 194 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 195 assertEquals(1,client.getState().getCookies().length); 196 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 197 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 198 199 method.recycle(); 200 201 method.setPath("/" + getWebappContext() + "/cookie/write"); 202 method.setQueryString("simple=unset"); 203 try { 204 client.executeMethod(method); 205 } catch (Throwable t) { 206 t.printStackTrace(); 207 fail("Unable to execute method : " + t.toString()); 208 } 209 assertEquals(200,method.getStatusCode()); 210 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 211 assertTrue(method.getResponseBodyAsString().indexOf("Deleted simplecookie.<br>") >= 0); 212 assertEquals(0,client.getState().getCookies().length); 213 } 214 215 public void testSetMultiCookieGetStrict() throws Exception { 216 HttpClient client = createHttpClient(); 217 client.setStrictMode(true); 218 219 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 220 method.setQueryString("simple=set&domain=set"); 221 222 try { 223 client.executeMethod(method); 224 } catch (Throwable t) { 225 t.printStackTrace(); 226 fail("Unable to execute method : " + t.toString()); 227 } 228 assertEquals(200,method.getStatusCode()); 229 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 230 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 231 assertTrue(method.getResponseBodyAsString().indexOf("Wrote domaincookie.<br>") >= 0); 232 assertEquals(2,client.getState().getCookies().length); 233 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 234 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 235 assertEquals("domaincookie", ((Cookie)(client.getState().getCookies()[1])).getName()); 236 assertEquals("value",((Cookie)(client.getState().getCookies()[1])).getValue()); 237 } 238 239 240 public void testMultiSendCookieGetNonstrict() throws Exception { 241 HttpClient client = createHttpClient(); 242 243 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 244 method.setQueryString("simple=set&domain=set"); 245 try { 246 client.executeMethod(method); 247 } catch (Throwable t) { 248 t.printStackTrace(); 249 fail("Unable to execute method : " + t.toString()); 250 } 251 assertEquals(200,method.getStatusCode()); 252 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 253 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 254 assertTrue(method.getResponseBodyAsString().indexOf("Wrote domaincookie.<br>") >= 0); 255 assertEquals(2,client.getState().getCookies().length); 256 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 257 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 258 assertEquals("domaincookie", ((Cookie)(client.getState().getCookies()[1])).getName()); 259 assertEquals("value",((Cookie)(client.getState().getCookies()[1])).getValue()); 260 261 GetMethod method2 = new GetMethod("/" + getWebappContext() + "/cookie/read"); 262 try { 263 client.executeMethod(method2); 264 } catch (Throwable t) { 265 t.printStackTrace(); 266 fail("Unable to execute method : " + t.toString()); 267 } 268 assertEquals(200,method2.getStatusCode()); 269 String s = method2.getResponseBodyAsString(); 270 assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >= 0); 271 assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; simplecookie=\"value\"</tt></p>") >= 0); 272 assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; domaincookie=\"value\"; $Domain=\"" + getHost() + "\"</tt></p>") >= 0); 273 assertTrue(s, s.indexOf("<tt>simplecookie=\"value\"</tt><br>") >= 0); 274 assertTrue(s, s.indexOf("<tt>domaincookie=\"value\"</tt><br>") >= 0); 275 } 276 277 278 public void testSetMultiCookiePut() throws Exception { 279 HttpClient client = createHttpClient(); 280 client.setStrictMode(true); 281 282 PutMethod method = new PutMethod("/" + getWebappContext() + "/cookie/write"); 283 method.setQueryString("simple=set&domain=set"); 284 method.setRequestBody("data to be sent via http post"); 285 try { 286 client.executeMethod(method); 287 } catch (Throwable t) { 288 t.printStackTrace(); 289 fail("Unable to execute method : " + t.toString()); 290 } 291 assertEquals(200,method.getStatusCode()); 292 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: PUT</title>") >= 0); 293 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 294 assertTrue(method.getResponseBodyAsString().indexOf("Wrote domaincookie.<br>") >= 0); 295 assertEquals(2,client.getState().getCookies().length); 296 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 297 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 298 assertEquals("domaincookie", ((Cookie)(client.getState().getCookies()[1])).getName()); 299 assertEquals("value",((Cookie)(client.getState().getCookies()[1])).getValue()); 300 } 301 302 public void testSendCookieGet() throws Exception { 303 HttpClient client = createHttpClient(); 304 client.setStrictMode(true); 305 306 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 307 method.setQueryString("simple=set"); 308 309 try { 310 client.executeMethod(method); 311 } catch (Throwable t) { 312 t.printStackTrace(); 313 fail("Unable to execute method : " + t.toString()); 314 } 315 assertEquals(200,method.getStatusCode()); 316 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 317 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 318 assertEquals(1,client.getState().getCookies().length); 319 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 320 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 321 322 GetMethod method2 = new GetMethod("/" + getWebappContext() + "/cookie/read"); 323 324 try { 325 client.executeMethod(method2); 326 } catch (Throwable t) { 327 t.printStackTrace(); 328 fail("Unable to execute method : " + t.toString()); 329 } 330 assertEquals(200,method2.getStatusCode()); 331 String s = method2.getResponseBodyAsString(); 332 assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >= 0); 333 assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; simplecookie=\"value\"</tt></p>") >= 0); 334 assertTrue(s, s.indexOf("<tt>simplecookie=\"value\"</tt><br>") >= 0); 335 } 336 337 public void testMultiSendCookieGet() throws Exception { 338 HttpClient client = createHttpClient(); 339 client.setStrictMode(true); 340 341 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 342 method.setQueryString("simple=set&domain=set"); 343 344 try { 345 client.executeMethod(method); 346 } catch (Throwable t) { 347 t.printStackTrace(); 348 fail("Unable to execute method : " + t.toString()); 349 } 350 assertEquals(200,method.getStatusCode()); 351 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 352 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 353 assertTrue(method.getResponseBodyAsString().indexOf("Wrote domaincookie.<br>") >= 0); 354 assertEquals(2,client.getState().getCookies().length); 355 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 356 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 357 assertEquals("domaincookie", ((Cookie)(client.getState().getCookies()[1])).getName()); 358 assertEquals("value",((Cookie)(client.getState().getCookies()[1])).getValue()); 359 360 GetMethod method2 = new GetMethod("/" + getWebappContext() + "/cookie/read"); 361 362 try { 363 client.executeMethod(method2); 364 } catch (Throwable t) { 365 t.printStackTrace(); 366 fail("Unable to execute method : " + t.toString()); 367 } 368 assertEquals(200,method2.getStatusCode()); 369 String s = method2.getResponseBodyAsString(); 370 assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >= 0); 371 assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; simplecookie=\"value\"; domaincookie=\"value\"; $Domain=\"" + getHost() + "\"</tt></p>") >= 0); 372 assertTrue(s, s.indexOf("<tt>simplecookie=\"value\"</tt><br>") >= 0); 373 assertTrue(s, s.indexOf("<tt>domaincookie=\"value\"</tt><br>") >= 0); 374 } 375 376 public void testDeleteCookieGet() throws Exception { 377 HttpClient client = createHttpClient(); 378 client.setStrictMode(true); 379 380 381 { 382 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 383 method.setQueryString("simple=set&domain=set"); 384 385 try { 386 client.executeMethod(method); 387 } catch (Throwable t) { 388 t.printStackTrace(); 389 fail("Unable to execute method : " + t.toString()); 390 } 391 assertEquals(200,method.getStatusCode()); 392 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 393 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 394 assertTrue(method.getResponseBodyAsString().indexOf("Wrote domaincookie.<br>") >= 0); 395 assertEquals(2,client.getState().getCookies().length); 396 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 397 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 398 assertEquals("domaincookie", ((Cookie)(client.getState().getCookies()[1])).getName()); 399 assertEquals("value",((Cookie)(client.getState().getCookies()[1])).getValue()); 400 } 401 402 { 403 GetMethod method2 = new GetMethod("/" + getWebappContext() + "/cookie/read"); 404 405 try { 406 client.executeMethod(method2); 407 } catch (Throwable t) { 408 t.printStackTrace(); 409 fail("Unable to execute method : " + t.toString()); 410 } 411 assertEquals(200,method2.getStatusCode()); 412 String s = method2.getResponseBodyAsString(); 413 assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >= 0); 414 assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; simplecookie=\"value\"; domaincookie=\"value\"; $Domain=\"" + getHost() + "\"</tt></p>") >= 0); 415 assertTrue(s, s.indexOf("<tt>simplecookie=\"value\"</tt><br>") >= 0); 416 assertTrue(s, s.indexOf("<tt>domaincookie=\"value\"</tt><br>") >= 0); 417 } 418 419 { 420 GetMethod method3 = new GetMethod("/" + getWebappContext() + "/cookie/write"); 421 422 method3.setQueryString("simple=unset"); 423 try { 424 client.executeMethod(method3); 425 } catch (Throwable t) { 426 t.printStackTrace(); 427 fail("Unable to execute method : " + t.toString()); 428 } 429 assertEquals(200,method3.getStatusCode()); 430 assertTrue(method3.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 431 assertTrue(method3.getResponseBodyAsString().indexOf("Deleted simplecookie.<br>") >= 0); 432 assertEquals(1,client.getState().getCookies().length); 433 assertEquals("domaincookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 434 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 435 } 436 437 { 438 GetMethod method4 = new GetMethod("/" + getWebappContext() + "/cookie/read"); 439 440 try { 441 client.executeMethod(method4); 442 } catch (Throwable t) { 443 t.printStackTrace(); 444 fail("Unable to execute method : " + t.toString()); 445 } 446 assertEquals(200,method4.getStatusCode()); 447 String s = method4.getResponseBodyAsString(); 448 assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >= 0); 449 assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; domaincookie=\"value\"; $Domain=\"" + getHost() + "\"</tt></p>") >= 0); 450 assertTrue(s, s.indexOf("<tt>domaincookie=\"value\"</tt><br>") >= 0); 451 } 452 } 453 454 public void testDeleteCookiePut() throws Exception { 455 HttpClient client = createHttpClient(); 456 client.setStrictMode(true); 457 458 459 { 460 PutMethod method = new PutMethod("/" + getWebappContext() + "/cookie/write"); 461 method.setQueryString("simple=set&domain=set"); 462 method.setRequestBody("data to be sent via http post"); 463 try { 464 client.executeMethod(method); 465 } catch (Throwable t) { 466 t.printStackTrace(); 467 fail("Unable to execute method : " + t.toString()); 468 } 469 assertEquals(200,method.getStatusCode()); 470 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: PUT</title>") >= 0); 471 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 472 assertTrue(method.getResponseBodyAsString().indexOf("Wrote domaincookie.<br>") >= 0); 473 assertEquals(2,client.getState().getCookies().length); 474 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 475 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 476 assertEquals("domaincookie", ((Cookie)(client.getState().getCookies()[1])).getName()); 477 assertEquals("value",((Cookie)(client.getState().getCookies()[1])).getValue()); 478 } 479 480 { 481 PutMethod method2 = new PutMethod("/" + getWebappContext() + "/cookie/read"); 482 method2.setRequestBody("data to be sent via http post"); 483 try { 484 client.executeMethod(method2); 485 } catch (Throwable t) { 486 t.printStackTrace(); 487 fail("Unable to execute method : " + t.toString()); 488 } 489 assertEquals(200,method2.getStatusCode()); 490 String s = method2.getResponseBodyAsString(); 491 assertTrue(s, s.indexOf("<title>ReadCookieServlet: PUT</title>") >= 0); 492 assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; simplecookie=\"value\"; domaincookie=\"value\"; $Domain=\"" + getHost() + "\"</tt></p>") >= 0); 493 assertTrue(s, s.indexOf("<tt>simplecookie=\"value\"</tt><br>") >= 0); 494 assertTrue(s, s.indexOf("<tt>domaincookie=\"value\"</tt><br>") >= 0); 495 } 496 497 { 498 PutMethod method3 = new PutMethod("/" + getWebappContext() + "/cookie/write"); 499 method3.setRequestBody("data to be sent via http post"); 500 method3.setQueryString("simple=unset"); 501 try { 502 client.executeMethod(method3); 503 } catch (Throwable t) { 504 t.printStackTrace(); 505 fail("Unable to execute method : " + t.toString()); 506 } 507 assertEquals(200,method3.getStatusCode()); 508 assertTrue(method3.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: PUT</title>") >= 0); 509 assertTrue(method3.getResponseBodyAsString().indexOf("Deleted simplecookie.<br>") >= 0); 510 assertEquals(1,client.getState().getCookies().length); 511 assertEquals("domaincookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 512 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 513 } 514 515 { 516 PutMethod method4 = new PutMethod("/" + getWebappContext() + "/cookie/read"); 517 method4.setRequestBody("data to be sent via http post"); 518 try { 519 client.executeMethod(method4); 520 } catch (Throwable t) { 521 t.printStackTrace(); 522 fail("Unable to execute method : " + t.toString()); 523 } 524 assertEquals(200,method4.getStatusCode()); 525 String s = method4.getResponseBodyAsString(); 526 assertTrue(s, s.indexOf("<title>ReadCookieServlet: PUT</title>") >= 0); 527 assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; domaincookie=\"value\"; $Domain=\"" + getHost() + "\"</tt></p>") >= 0); 528 assertTrue(s, s.indexOf("<tt>domaincookie=\"value\"</tt><br>") >= 0); 529 } 530 } 531 532 public void testPathCookie1() throws Exception { 533 HttpClient client = createHttpClient(); 534 535 536 { 537 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 538 method.setQueryString("path=/"); 539 540 try { 541 client.executeMethod(method); 542 } catch (Throwable t) { 543 t.printStackTrace(); 544 fail("Unable to execute method : " + t.toString()); 545 } 546 assertEquals(200,method.getStatusCode()); 547 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 548 assertTrue(method.getResponseBodyAsString().indexOf("Wrote pathcookie.<br>") >= 0); 549 assertEquals(1,client.getState().getCookies().length); 550 assertEquals("/",((Cookie)(client.getState().getCookies()[0])).getPath()); 551 } 552 553 { 554 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/read"); 555 556 try { 557 client.executeMethod(method); 558 } catch (Throwable t) { 559 t.printStackTrace(); 560 fail("Unable to execute method : " + t.toString()); 561 } 562 assertEquals(200,method.getStatusCode()); 563 String s = method.getResponseBodyAsString(); 564 assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >= 0); 565 assertTrue(s ,s.indexOf("<p><tt>Cookie: $Version=\"1\"; pathcookie=\"value\"; $Path=\"/\"</tt></p>") >= 0); 566 assertTrue(s, s.indexOf("<tt>pathcookie=\"value\"</tt><br>") >= 0); 567 } 568 } 569 570 public void testPathCookie2() throws Exception { 571 HttpClient client = createHttpClient(); 572 573 574 { 575 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 576 method.setQueryString("path=/" + getWebappContext()); 577 578 try { 579 client.executeMethod(method); 580 } catch (Throwable t) { 581 t.printStackTrace(); 582 fail("Unable to execute method : " + t.toString()); 583 } 584 assertEquals(200,method.getStatusCode()); 585 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 586 assertTrue(method.getResponseBodyAsString().indexOf("Wrote pathcookie.<br>") >= 0); 587 assertEquals(1,client.getState().getCookies().length); 588 assertEquals("/" + getWebappContext(),((Cookie)(client.getState().getCookies()[0])).getPath()); 589 } 590 591 { 592 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/read"); 593 594 try { 595 client.executeMethod(method); 596 } catch (Throwable t) { 597 t.printStackTrace(); 598 fail("Unable to execute method : " + t.toString()); 599 } 600 assertEquals(200,method.getStatusCode()); 601 String s = method.getResponseBodyAsString(); 602 assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >= 0); 603 assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; pathcookie=\"value\"; $Path=\"/" + getWebappContext() +"\"</tt></p>") >= 0); 604 assertTrue(s, s.indexOf("<tt>pathcookie=\"value\"</tt><br>") >= 0); 605 } 606 } 607 608 public void testPathCookie3() throws Exception { 609 HttpClient client = createHttpClient(); 610 611 { 612 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 613 method.setQueryString("path=/" + getWebappContext() + "/cookie"); 614 615 try { 616 client.executeMethod(method); 617 } catch (Throwable t) { 618 t.printStackTrace(); 619 fail("Unable to execute method : " + t.toString()); 620 } 621 assertEquals(200,method.getStatusCode()); 622 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 623 assertTrue(method.getResponseBodyAsString().indexOf("Wrote pathcookie.<br>") >= 0); 624 assertEquals(1,client.getState().getCookies().length); 625 assertEquals("/" + getWebappContext() + "/cookie",((Cookie)(client.getState().getCookies()[0])).getPath()); 626 } 627 628 { 629 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/read"); 630 631 try { 632 client.executeMethod(method); 633 } catch (Throwable t) { 634 t.printStackTrace(); 635 fail("Unable to execute method : " + t.toString()); 636 } 637 assertEquals(200,method.getStatusCode()); 638 String s = method.getResponseBodyAsString(); 639 assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >= 0); 640 assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; pathcookie=\"value\"; $Path=\"/" + getWebappContext() + "/cookie\"</tt></p>") >= 0); 641 assertTrue(s, s.indexOf("<tt>pathcookie=\"value\"</tt><br>") >= 0); 642 } 643 } 644 645 public void testPathCookie4() throws Exception { 646 HttpClient client = createHttpClient(); 647 648 649 { 650 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 651 method.setQueryString("path=/" + getWebappContext() + "/cookie/write"); 652 653 try { 654 client.executeMethod(method); 655 } catch (Throwable t) { 656 t.printStackTrace(); 657 fail("Unable to execute method : " + t.toString()); 658 } 659 assertEquals(200,method.getStatusCode()); 660 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 661 assertTrue(method.getResponseBodyAsString().indexOf("Wrote pathcookie.<br>") >= 0); 662 assertEquals(1,client.getState().getCookies().length); 663 assertEquals("/" + getWebappContext() + "/cookie/write",((Cookie)(client.getState().getCookies()[0])).getPath()); 664 } 665 666 { 667 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/read"); 668 669 try { 670 client.executeMethod(method); 671 } catch (Throwable t) { 672 t.printStackTrace(); 673 fail("Unable to execute method : " + t.toString()); 674 } 675 assertEquals(200,method.getStatusCode()); 676 assertTrue(method.getResponseBodyAsString().indexOf("<title>ReadCookieServlet: GET</title>") >= 0); 677 assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<p><tt>Cookie: ") == -1); 678 assertTrue(method.getResponseBodyAsString().indexOf("<tt>pathcookie=value</tt><br>") == -1); 679 } 680 } 681 682 683 public void testCookiePolicies() { 684 HttpClient client = createHttpClient(); 685 686 687 { 688 client.getState().setCookiePolicy(CookiePolicy.RFC2109); 689 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 690 method.setQueryString("simple=set"); 691 692 try { 693 client.executeMethod(method); 694 } catch (Throwable t) { 695 t.printStackTrace(); 696 fail("Unable to execute method : " + t.toString()); 697 } 698 assertEquals(200,method.getStatusCode()); 699 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 700 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 701 assertEquals(1,client.getState().getCookies().length); 702 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 703 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 704 705 GetMethod method2 = new GetMethod("/" + getWebappContext() + "/cookie/read"); 706 707 try { 708 client.executeMethod(method2); 709 } catch (Throwable t) { 710 t.printStackTrace(); 711 fail("Unable to execute method : " + t.toString()); 712 } 713 assertEquals(200,method2.getStatusCode()); 714 String s = method2.getResponseBodyAsString(); 715 assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >= 0); 716 assertTrue(s, s.indexOf("<p><tt>Cookie: $Version=\"1\"; simplecookie=\"value\"</tt></p>") >= 0); 717 assertTrue(s, s.indexOf("<tt>simplecookie=\"value\"</tt><br>") >= 0); 718 } 719 720 { 721 client.getState().setCookiePolicy(CookiePolicy.COMPATIBILITY); 722 GetMethod method = new GetMethod("/" + getWebappContext() + "/cookie/write"); 723 method.setQueryString("simple=set"); 724 725 try { 726 client.executeMethod(method); 727 } catch (Throwable t) { 728 t.printStackTrace(); 729 fail("Unable to execute method : " + t.toString()); 730 } 731 assertEquals(200,method.getStatusCode()); 732 assertTrue(method.getResponseBodyAsString().indexOf("<title>WriteCookieServlet: GET</title>") >= 0); 733 assertTrue(method.getResponseBodyAsString().indexOf("Wrote simplecookie.<br>") >= 0); 734 assertEquals(1,client.getState().getCookies().length); 735 assertEquals("simplecookie", ((Cookie)(client.getState().getCookies()[0])).getName()); 736 assertEquals("value",((Cookie)(client.getState().getCookies()[0])).getValue()); 737 738 GetMethod method2 = new GetMethod("/" + getWebappContext() + "/cookie/read"); 739 740 try { 741 client.executeMethod(method2); 742 } catch (Throwable t) { 743 t.printStackTrace(); 744 fail("Unable to execute method : " + t.toString()); 745 } 746 assertEquals(200,method2.getStatusCode()); 747 String s = method2.getResponseBodyAsString(); 748 assertTrue(s, s.indexOf("<title>ReadCookieServlet: GET</title>") >= 0); 749 assertTrue(s, s.indexOf("<p><tt>Cookie: simplecookie=value</tt></p>") >= 0); 750 assertTrue(s, s.indexOf("<tt>simplecookie=value</tt><br>") >= 0); 751 } 752 } 753 754 } 755 756 | Popular Tags |