1 /*
2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java,v 1.17 2003/06/23 23:41:39 mbecke Exp $
3 * $Revision: 1.17 $
4 * $Date: 2003/06/23 23:41:39 $
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 java.io.ByteArrayInputStream;
66 import java.io.InputStream;
67
68 import junit.framework.*;
69 import org.apache.commons.httpclient.methods.*;
70
71 /***
72 * This suite of tests depends upon the httpclienttest webapp,
73 * which is available in the httpclient/src/test-webapp
74 * directory in the CVS tree.
75 * <p>
76 * The webapp should be deployed in the context "httpclienttest"
77 * on a servlet engine running on port 8080 on the localhost
78 * (IP 127.0.0.1).
79 * <p>
80 * You can change the assumed port by setting the
81 * "httpclient.test.localPort" property.
82 * You can change the assumed host by setting the
83 * "httpclient.test.localHost" property.
84 * You can change the assumed context by setting the
85 * "httpclient.test.webappContext" property.
86 *
87 * @author Rodney Waldhoff
88 * @author Ortwin Gl�ck
89 * @version $Id: TestWebappMethods.java,v 1.17 2003/06/23 23:41:39 mbecke Exp $
90 */
91 public class TestWebappMethods extends TestWebappBase {
92
93 public TestWebappMethods(String testName) {
94 super(testName);
95 }
96
97 public static Test suite() {
98 TestSuite suite = new TestSuite(TestWebappMethods.class);
99 return suite;
100 }
101
102 public static void main(String args[]) {
103 String[] testCaseName = { TestWebappMethods.class.getName() };
104 junit.textui.TestRunner.main(testCaseName);
105 }
106
107 // ------------------------------------------------------------------ Tests
108
109 /***
110 * Simple test of {@link GetMethod} against /httpclienttest/params.
111 */
112 public void testGetMethod() throws Exception {
113 HttpClient client = createHttpClient();
114 GetMethod method = new GetMethod("/" + getWebappContext() + "/params");
115
116 try {
117 client.executeMethod(method);
118 } catch (Throwable t) {
119 t.printStackTrace();
120 fail("Unable to execute method : " + t.toString());
121 }
122 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
123 assertEquals(200,method.getStatusCode());
124
125 method.recycle();
126
127 method.setPath("/" + getWebappContext() + "/params");
128 try {
129 client.executeMethod(method);
130 } catch (Throwable t) {
131 t.printStackTrace();
132 fail("Unable to execute method : " + t.toString());
133 }
134 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
135 assertEquals(200,method.getStatusCode());
136 }
137
138 /***
139 * Simple test of {@link PostMethod} against /httpclienttest/params.
140 */
141 public void testPostMethod() throws Exception {
142 HttpClient client = createHttpClient();
143 PostMethod method = new PostMethod("/" + getWebappContext() + "/params");
144
145 try {
146 client.executeMethod(method);
147 } catch (Throwable t) {
148 t.printStackTrace();
149 fail("Unable to execute method : " + t.toString());
150 }
151 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0);
152 assertEquals(200,method.getStatusCode());
153
154 method.recycle();
155
156 method.setPath("/" + getWebappContext() + "/params");
157 try {
158 client.executeMethod(method);
159 } catch (Throwable t) {
160 t.printStackTrace();
161 fail("Unable to execute method : " + t.toString());
162 }
163 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0);
164 assertEquals(200,method.getStatusCode());
165 }
166
167 /***
168 * Simple test of {@link HeadMethod} against /httpclienttest/params.
169 */
170 public void testHeadMethod() throws Exception {
171 HttpClient client = createHttpClient();
172 HeadMethod method = new HeadMethod("/" + getWebappContext() + "/params");
173 try {
174 client.executeMethod(method);
175 } catch (Throwable t) {
176 t.printStackTrace();
177 fail("Unable to execute method : " + t.toString());
178 }
179 assertEquals(200,method.getStatusCode());
180
181 method.recycle();
182
183 method.setPath("/" + getWebappContext() + "/params");
184 try {
185 client.executeMethod(method);
186 } catch (Throwable t) {
187 t.printStackTrace();
188 fail("Unable to execute method : " + t.toString());
189 }
190 assertEquals(200,method.getStatusCode());
191 }
192
193 /***
194 * Simple test of {@link OptionsMethod} against /httpclienttest/params.
195 */
196 public void testOptionsMethod() throws Exception {
197 HttpClient client = createHttpClient();
198 OptionsMethod method = new OptionsMethod("/" + getWebappContext() + "/params");
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.getAllowedMethods().hasMoreElements());
207
208 method.recycle();
209
210 method.setPath("/" + getWebappContext() + "/params");
211 try {
212 client.executeMethod(method);
213 } catch (Throwable t) {
214 t.printStackTrace();
215 fail("Unable to execute method : " + t.toString());
216 }
217 assertEquals(200,method.getStatusCode());
218 assertTrue(method.getAllowedMethods().hasMoreElements());
219 }
220
221 /***
222 * Simple test of {@link OptionsMethod} against the path "*".
223 */
224 public void testOptionsStar() throws Exception {
225 HttpClient client = createHttpClient();
226 OptionsMethod method = new OptionsMethod("*");
227 try {
228 client.executeMethod(method);
229 } catch (Throwable t) {
230 t.printStackTrace();
231 fail("Unable to execute method : " + t.toString());
232 }
233 assertEquals(200,method.getStatusCode());
234 assertTrue(method.getAllowedMethods().hasMoreElements());
235 }
236
237 /***
238 * Simple test of {@link DeleteMethod} against /httpclienttest/params.
239 */
240 public void testDeleteMethod() throws Exception {
241 HttpClient client = createHttpClient();
242 DeleteMethod method = new DeleteMethod("/" + getWebappContext() + "/params");
243 try {
244 client.executeMethod(method);
245 } catch (Throwable t) {
246 t.printStackTrace();
247 fail("Unable to execute method : " + t.toString());
248 }
249 assertEquals(200,method.getStatusCode());
250
251 method.recycle();
252
253 method.setPath("/" + getWebappContext() + "/params");
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 }
262
263 /***
264 * Simple test of {@link PutMethod} against /httpclienttest/params.
265 */
266 public void testPutMethod() throws Exception {
267 HttpClient client = createHttpClient();
268 PutMethod method = new PutMethod("/" + getWebappContext() + "/params");
269 try {
270 client.executeMethod(method);
271 } catch (Throwable t) {
272 t.printStackTrace();
273 fail("Unable to execute method : " + t.toString());
274 }
275 assertEquals(200,method.getStatusCode());
276 assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<title>Param Servlet: PUT</title>") >= 0);
277
278 method.recycle();
279
280 method.setPath("/" + getWebappContext() + "/params");
281 try {
282 client.executeMethod(method);
283 } catch (Throwable t) {
284 t.printStackTrace();
285 fail("Unable to execute method : " + t.toString());
286 }
287 assertEquals(200,method.getStatusCode());
288 assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<title>Param Servlet: PUT</title>") >= 0);
289 }
290
291 public void testPostBodyNVP() throws Exception {
292 HttpClient client = createHttpClient();
293 PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
294
295 method.setRequestBody(new NameValuePair[] {
296 new NameValuePair("quote","It was the best of times, it was the worst of times.") } );
297 try {
298 client.executeMethod(method);
299 } catch (Throwable t) {
300 t.printStackTrace();
301 fail("Unable to execute method : " + t.toString());
302 }
303 assertEquals(200,method.getStatusCode());
304 assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
305 }
306
307 public void testPostBody() throws Exception {
308 HttpClient client = createHttpClient();
309 PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
310
311 method.setRequestBody("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.");
312 try {
313 client.executeMethod(method);
314 } catch (Throwable t) {
315 t.printStackTrace();
316 fail("Unable to execute method : " + t.toString());
317 }
318 assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
319 assertEquals(200,method.getStatusCode());
320 }
321
322
323 public void testPostBodyCustomLength() throws Exception {
324 HttpClient client = createHttpClient();
325 PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
326
327 String bodyStr = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.";
328 byte[] body = HttpConstants.getContentBytes(bodyStr);
329
330 method.setRequestBody(new ByteArrayInputStream(body));
331 method.setRequestContentLength(body.length);
332 try {
333 client.executeMethod(method);
334 } catch (Throwable t) {
335 t.printStackTrace();
336 fail("Unable to execute method : " + t.toString());
337 }
338 assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
339 assertEquals(200,method.getStatusCode());
340 }
341
342
343 public void testPostBodyAutoLength() throws Exception {
344 HttpClient client = createHttpClient();
345 PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
346
347 String body = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.";
348 method.setRequestBody(body);
349 method.setRequestContentLength(PostMethod.CONTENT_LENGTH_AUTO);
350 try {
351 client.executeMethod(method);
352 } catch (Throwable t) {
353 t.printStackTrace();
354 fail("Unable to execute method : " + t.toString());
355 }
356 assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
357 assertEquals(200,method.getStatusCode());
358 }
359
360
361 public void testPostBodyChunked() {
362 HttpClient client = createHttpClient();
363 PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
364
365 String body = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.";
366 method.setRequestBody(body);
367 method.setRequestContentLength(PostMethod.CONTENT_LENGTH_CHUNKED);
368 try {
369 client.executeMethod(method);
370 } catch (Throwable t) {
371 t.printStackTrace();
372 fail("Unable to execute method : " + t.toString());
373 }
374 assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
375 assertEquals(200,method.getStatusLine().getStatusCode());
376 }
377
378
379 public void testPutBody() throws Exception {
380 HttpClient client = createHttpClient();
381 PutMethod method = new PutMethod("/" + getWebappContext() + "/body");
382 method.setRequestBody("This is data to be sent in the body of an HTTP PUT.");
383 try {
384 client.executeMethod(method);
385 } catch (Throwable t) {
386 t.printStackTrace();
387 fail("Unable to execute method : " + t.toString());
388 }
389 assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<tt>This is data to be sent in the body of an HTTP PUT.</tt>") >= 0);
390 assertEquals(200,method.getStatusCode());
391 }
392
393
394 public void testPostMethodRecycle() {
395 HttpClient client = createHttpClient();
396 PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
397
398 String bodyStr = "Like, hello, and stuff";
399 byte [] body = HttpConstants.getContentBytes(bodyStr);
400 method.setRequestHeader("Content-Type", "text/plain");
401 method.setRequestBody(new ByteArrayInputStream(body));
402 method.setRequestContentLength(body.length);
403 try {
404 client.executeMethod(method);
405 } catch (Throwable t) {
406 t.printStackTrace();
407 fail("Unable to execute method : " + t.toString());
408 }
409 assertEquals(200,method.getStatusLine().getStatusCode());
410 String response = method.getResponseBodyAsString();
411
412 method.recycle();
413
414 method.setPath("/" + getWebappContext() + "/body");
415 method.setRequestHeader("Content-Type", "text/plain");
416 method.setRequestBody(new ByteArrayInputStream(body));
417 method.setRequestContentLength(body.length);
418 try {
419 client.executeMethod(method);
420 } catch (Throwable t) {
421 t.printStackTrace();
422 fail("Unable to execute method : " + t.toString());
423 }
424 assertEquals(200,method.getStatusLine().getStatusCode());
425 response = method.getResponseBodyAsString();
426 }
427
428 public void testEmptyPostMethod() throws Exception {
429 HttpClient client = createHttpClient();
430 PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
431
432 method.setRequestHeader("Content-Type", "text/plain");
433 client.executeMethod(method);
434 assertEquals(200,method.getStatusLine().getStatusCode());
435 String response = method.getResponseBodyAsString();
436 assertTrue(response.indexOf("No body submitted") >= 0);
437
438 method.recycle();
439
440 method.setPath("/" + getWebappContext() + "/body");
441 method.setRequestHeader("Content-Type", "text/plain");
442 method.setRequestBody((String)null);
443 client.executeMethod(method);
444 assertEquals(200,method.getStatusLine().getStatusCode());
445 response = method.getResponseBodyAsString();
446 assertTrue(response.indexOf("No body submitted") >= 0);
447
448 method.recycle();
449
450 method.setPath("/" + getWebappContext() + "/body");
451 method.setRequestHeader("Content-Type", "text/plain");
452 method.setRequestBody((InputStream)null);
453 client.executeMethod(method);
454 assertEquals(200,method.getStatusLine().getStatusCode());
455 response = method.getResponseBodyAsString();
456 assertTrue(response.indexOf("No body submitted") >= 0);
457
458 method.recycle();
459
460 method.setPath("/" + getWebappContext() + "/body");
461 method.setRequestHeader("Content-Type", "text/plain");
462 method.setRequestBody("");
463 client.executeMethod(method);
464 assertEquals(200,method.getStatusLine().getStatusCode());
465 response = method.getResponseBodyAsString();
466 assertTrue(response.indexOf("No body submitted") >= 0);
467
468 method.recycle();
469
470 method.setPath("/" + getWebappContext() + "/body");
471 method.setRequestHeader("Content-Type", "text/plain");
472 method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
473 client.executeMethod(method);
474 assertEquals(200,method.getStatusLine().getStatusCode());
475 response = method.getResponseBodyAsString();
476 assertTrue(response.indexOf("No body submitted") >= 0);
477
478 method.recycle();
479
480 method.setPath("/" + getWebappContext() + "/body");
481 method.setRequestHeader("Content-Type", "text/plain");
482 method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
483 method.setRequestBody((String)null);
484 client.executeMethod(method);
485 assertEquals(200,method.getStatusLine().getStatusCode());
486 response = method.getResponseBodyAsString();
487 assertTrue(response.indexOf("No body submitted") >= 0);
488
489 method.recycle();
490
491 method.setPath("/" + getWebappContext() + "/body");
492 method.setRequestHeader("Content-Type", "text/plain");
493 method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
494 method.setRequestBody((InputStream)null);
495 client.executeMethod(method);
496 assertEquals(200,method.getStatusLine().getStatusCode());
497 response = method.getResponseBodyAsString();
498 assertTrue(response.indexOf("No body submitted") >= 0);
499
500 method.recycle();
501
502 method.setPath("/" + getWebappContext() + "/body");
503 method.setRequestHeader("Content-Type", "text/plain");
504 method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
505 method.setRequestBody("");
506 client.executeMethod(method);
507 assertEquals(200,method.getStatusLine().getStatusCode());
508 response = method.getResponseBodyAsString();
509
510 }
511
512 }
This page was automatically generated by Maven