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