KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > httpclient > TestWebappMethods


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java,v 1.17.2.1 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.17.2.1 $
4  * $Date: 2004/02/22 18:21:16 $
5  * ====================================================================
6  *
7  * Copyright 1999-2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ====================================================================
21  *
22  * This software consists of voluntary contributions made by many
23  * individuals on behalf of the Apache Software Foundation. For more
24  * information on the Apache Software Foundation, please see
25  * <http://www.apache.org/>.
26  *
27  * [Additional notices, if required by prior licensing conditions]
28  *
29  */

30
31 package org.apache.commons.httpclient;
32
33 import java.io.ByteArrayInputStream JavaDoc;
34 import java.io.InputStream JavaDoc;
35
36 import junit.framework.*;
37 import org.apache.commons.httpclient.methods.*;
38
39 /**
40  * This suite of tests depends upon the httpclienttest webapp,
41  * which is available in the httpclient/src/test-webapp
42  * directory in the CVS tree.
43  * <p>
44  * The webapp should be deployed in the context "httpclienttest"
45  * on a servlet engine running on port 8080 on the localhost
46  * (IP 127.0.0.1).
47  * <p>
48  * You can change the assumed port by setting the
49  * "httpclient.test.localPort" property.
50  * You can change the assumed host by setting the
51  * "httpclient.test.localHost" property.
52  * You can change the assumed context by setting the
53  * "httpclient.test.webappContext" property.
54  *
55  * @author Rodney Waldhoff
56  * @author Ortwin Glück
57  * @version $Id: TestWebappMethods.java,v 1.17.2.1 2004/02/22 18:21:16 olegk Exp $
58  */

59 public class TestWebappMethods extends TestWebappBase {
60
61     public TestWebappMethods(String JavaDoc testName) {
62         super(testName);
63     }
64
65     public static Test suite() {
66         TestSuite suite = new TestSuite(TestWebappMethods.class);
67         return suite;
68     }
69
70     public static void main(String JavaDoc args[]) {
71         String JavaDoc[] testCaseName = { TestWebappMethods.class.getName() };
72         junit.textui.TestRunner.main(testCaseName);
73     }
74
75     // ------------------------------------------------------------------ Tests
76

77     /**
78      * Simple test of {@link GetMethod} against /httpclienttest/params.
79      */

80     public void testGetMethod() throws Exception JavaDoc {
81         HttpClient client = createHttpClient();
82         GetMethod method = new GetMethod("/" + getWebappContext() + "/params");
83         
84         try {
85             client.executeMethod(method);
86         } catch (Throwable JavaDoc t) {
87             t.printStackTrace();
88             fail("Unable to execute method : " + t.toString());
89         }
90         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
91         assertEquals(200,method.getStatusCode());
92
93         method.recycle();
94
95         method.setPath("/" + getWebappContext() + "/params");
96         try {
97             client.executeMethod(method);
98         } catch (Throwable JavaDoc t) {
99             t.printStackTrace();
100             fail("Unable to execute method : " + t.toString());
101         }
102         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
103         assertEquals(200,method.getStatusCode());
104     }
105
106     /**
107      * Simple test of {@link PostMethod} against /httpclienttest/params.
108      */

109     public void testPostMethod() throws Exception JavaDoc {
110         HttpClient client = createHttpClient();
111         PostMethod method = new PostMethod("/" + getWebappContext() + "/params");
112         
113         try {
114             client.executeMethod(method);
115         } catch (Throwable JavaDoc t) {
116             t.printStackTrace();
117             fail("Unable to execute method : " + t.toString());
118         }
119         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0);
120         assertEquals(200,method.getStatusCode());
121
122         method.recycle();
123
124         method.setPath("/" + getWebappContext() + "/params");
125         try {
126             client.executeMethod(method);
127         } catch (Throwable JavaDoc t) {
128             t.printStackTrace();
129             fail("Unable to execute method : " + t.toString());
130         }
131         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0);
132         assertEquals(200,method.getStatusCode());
133     }
134
135     /**
136      * Simple test of {@link HeadMethod} against /httpclienttest/params.
137      */

138     public void testHeadMethod() throws Exception JavaDoc {
139         HttpClient client = createHttpClient();
140         HeadMethod method = new HeadMethod("/" + getWebappContext() + "/params");
141         try {
142             client.executeMethod(method);
143         } catch (Throwable JavaDoc t) {
144             t.printStackTrace();
145             fail("Unable to execute method : " + t.toString());
146         }
147         assertEquals(200,method.getStatusCode());
148
149         method.recycle();
150
151         method.setPath("/" + getWebappContext() + "/params");
152         try {
153             client.executeMethod(method);
154         } catch (Throwable JavaDoc t) {
155             t.printStackTrace();
156             fail("Unable to execute method : " + t.toString());
157         }
158         assertEquals(200,method.getStatusCode());
159     }
160
161     /**
162      * Simple test of {@link OptionsMethod} against /httpclienttest/params.
163      */

164     public void testOptionsMethod() throws Exception JavaDoc {
165         HttpClient client = createHttpClient();
166         OptionsMethod method = new OptionsMethod("/" + getWebappContext() + "/params");
167         try {
168             client.executeMethod(method);
169         } catch (Throwable JavaDoc t) {
170             t.printStackTrace();
171             fail("Unable to execute method : " + t.toString());
172         }
173         assertEquals(200,method.getStatusCode());
174         assertTrue(method.getAllowedMethods().hasMoreElements());
175
176         method.recycle();
177
178         method.setPath("/" + getWebappContext() + "/params");
179         try {
180             client.executeMethod(method);
181         } catch (Throwable JavaDoc t) {
182             t.printStackTrace();
183             fail("Unable to execute method : " + t.toString());
184         }
185         assertEquals(200,method.getStatusCode());
186         assertTrue(method.getAllowedMethods().hasMoreElements());
187     }
188
189     /**
190      * Simple test of {@link OptionsMethod} against the path "*".
191      */

192     public void testOptionsStar() throws Exception JavaDoc {
193         HttpClient client = createHttpClient();
194         OptionsMethod method = new OptionsMethod("*");
195         try {
196             client.executeMethod(method);
197         } catch (Throwable JavaDoc t) {
198             t.printStackTrace();
199             fail("Unable to execute method : " + t.toString());
200         }
201         assertEquals(200,method.getStatusCode());
202         assertTrue(method.getAllowedMethods().hasMoreElements());
203     }
204
205     /**
206      * Simple test of {@link DeleteMethod} against /httpclienttest/params.
207      */

208     public void testDeleteMethod() throws Exception JavaDoc {
209         HttpClient client = createHttpClient();
210         DeleteMethod method = new DeleteMethod("/" + getWebappContext() + "/params");
211         try {
212             client.executeMethod(method);
213         } catch (Throwable JavaDoc t) {
214             t.printStackTrace();
215             fail("Unable to execute method : " + t.toString());
216         }
217         assertEquals(200,method.getStatusCode());
218
219         method.recycle();
220
221         method.setPath("/" + getWebappContext() + "/params");
222         try {
223             client.executeMethod(method);
224         } catch (Throwable JavaDoc t) {
225             t.printStackTrace();
226             fail("Unable to execute method : " + t.toString());
227         }
228         assertEquals(200,method.getStatusCode());
229     }
230
231     /**
232      * Simple test of {@link PutMethod} against /httpclienttest/params.
233      */

234     public void testPutMethod() throws Exception JavaDoc {
235         HttpClient client = createHttpClient();
236         PutMethod method = new PutMethod("/" + getWebappContext() + "/params");
237         try {
238             client.executeMethod(method);
239         } catch (Throwable JavaDoc t) {
240             t.printStackTrace();
241             fail("Unable to execute method : " + t.toString());
242         }
243         assertEquals(200,method.getStatusCode());
244         assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<title>Param Servlet: PUT</title>") >= 0);
245
246         method.recycle();
247
248         method.setPath("/" + getWebappContext() + "/params");
249         try {
250             client.executeMethod(method);
251         } catch (Throwable JavaDoc t) {
252             t.printStackTrace();
253             fail("Unable to execute method : " + t.toString());
254         }
255         assertEquals(200,method.getStatusCode());
256         assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<title>Param Servlet: PUT</title>") >= 0);
257     }
258
259     public void testPostBodyNVP() throws Exception JavaDoc {
260         HttpClient client = createHttpClient();
261         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
262         
263         method.setRequestBody(new NameValuePair[] {
264            new NameValuePair("quote","It was the best of times, it was the worst of times.") } );
265         try {
266             client.executeMethod(method);
267         } catch (Throwable JavaDoc t) {
268             t.printStackTrace();
269             fail("Unable to execute method : " + t.toString());
270         }
271         assertEquals(200,method.getStatusCode());
272         assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
273     }
274
275     public void testPostBody() throws Exception JavaDoc {
276         HttpClient client = createHttpClient();
277         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
278         
279         method.setRequestBody("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.");
280         try {
281             client.executeMethod(method);
282         } catch (Throwable JavaDoc t) {
283             t.printStackTrace();
284             fail("Unable to execute method : " + t.toString());
285         }
286         assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
287         assertEquals(200,method.getStatusCode());
288     }
289
290
291     public void testPostBodyCustomLength() throws Exception JavaDoc {
292         HttpClient client = createHttpClient();
293         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
294         
295         String JavaDoc bodyStr = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.";
296         byte[] body = HttpConstants.getContentBytes(bodyStr);
297
298         method.setRequestBody(new ByteArrayInputStream JavaDoc(body));
299         method.setRequestContentLength(body.length);
300         try {
301             client.executeMethod(method);
302         } catch (Throwable JavaDoc t) {
303             t.printStackTrace();
304             fail("Unable to execute method : " + t.toString());
305         }
306         assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
307         assertEquals(200,method.getStatusCode());
308     }
309
310
311     public void testPostBodyAutoLength() throws Exception JavaDoc {
312         HttpClient client = createHttpClient();
313         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
314         
315         String JavaDoc body = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.";
316         method.setRequestBody(body);
317         method.setRequestContentLength(PostMethod.CONTENT_LENGTH_AUTO);
318         try {
319             client.executeMethod(method);
320         } catch (Throwable JavaDoc t) {
321             t.printStackTrace();
322             fail("Unable to execute method : " + t.toString());
323         }
324         assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
325         assertEquals(200,method.getStatusCode());
326     }
327
328
329     public void testPostBodyChunked() {
330         HttpClient client = createHttpClient();
331         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
332         
333         String JavaDoc body = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.";
334         method.setRequestBody(body);
335         method.setRequestContentLength(PostMethod.CONTENT_LENGTH_CHUNKED);
336         try {
337             client.executeMethod(method);
338         } catch (Throwable JavaDoc t) {
339             t.printStackTrace();
340             fail("Unable to execute method : " + t.toString());
341         }
342         assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
343         assertEquals(200,method.getStatusLine().getStatusCode());
344     }
345
346
347     public void testPutBody() throws Exception JavaDoc {
348         HttpClient client = createHttpClient();
349         PutMethod method = new PutMethod("/" + getWebappContext() + "/body");
350         method.setRequestBody("This is data to be sent in the body of an HTTP PUT.");
351         try {
352             client.executeMethod(method);
353         } catch (Throwable JavaDoc t) {
354             t.printStackTrace();
355             fail("Unable to execute method : " + t.toString());
356         }
357         assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<tt>This is data to be sent in the body of an HTTP PUT.</tt>") >= 0);
358         assertEquals(200,method.getStatusCode());
359     }
360
361
362     public void testPostMethodRecycle() {
363         HttpClient client = createHttpClient();
364         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
365         
366         String JavaDoc bodyStr = "Like, hello, and stuff";
367         byte [] body = HttpConstants.getContentBytes(bodyStr);
368         method.setRequestHeader("Content-Type", "text/plain");
369         method.setRequestBody(new ByteArrayInputStream JavaDoc(body));
370         method.setRequestContentLength(body.length);
371         try {
372             client.executeMethod(method);
373         } catch (Throwable JavaDoc t) {
374             t.printStackTrace();
375             fail("Unable to execute method : " + t.toString());
376         }
377         assertEquals(200,method.getStatusLine().getStatusCode());
378         String JavaDoc response = method.getResponseBodyAsString();
379
380         method.recycle();
381
382         method.setPath("/" + getWebappContext() + "/body");
383         method.setRequestHeader("Content-Type", "text/plain");
384         method.setRequestBody(new ByteArrayInputStream JavaDoc(body));
385         method.setRequestContentLength(body.length);
386         try {
387             client.executeMethod(method);
388         } catch (Throwable JavaDoc t) {
389             t.printStackTrace();
390             fail("Unable to execute method : " + t.toString());
391         }
392         assertEquals(200,method.getStatusLine().getStatusCode());
393         response = method.getResponseBodyAsString();
394     }
395
396     public void testEmptyPostMethod() throws Exception JavaDoc {
397         HttpClient client = createHttpClient();
398         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
399         
400         method.setRequestHeader("Content-Type", "text/plain");
401         client.executeMethod(method);
402         assertEquals(200,method.getStatusLine().getStatusCode());
403         String JavaDoc response = method.getResponseBodyAsString();
404         assertTrue(response.indexOf("No body submitted") >= 0);
405
406         method.recycle();
407
408         method.setPath("/" + getWebappContext() + "/body");
409         method.setRequestHeader("Content-Type", "text/plain");
410         method.setRequestBody((String JavaDoc)null);
411         client.executeMethod(method);
412         assertEquals(200,method.getStatusLine().getStatusCode());
413         response = method.getResponseBodyAsString();
414         assertTrue(response.indexOf("No body submitted") >= 0);
415
416         method.recycle();
417
418         method.setPath("/" + getWebappContext() + "/body");
419         method.setRequestHeader("Content-Type", "text/plain");
420         method.setRequestBody((InputStream JavaDoc)null);
421         client.executeMethod(method);
422         assertEquals(200,method.getStatusLine().getStatusCode());
423         response = method.getResponseBodyAsString();
424         assertTrue(response.indexOf("No body submitted") >= 0);
425
426         method.recycle();
427
428         method.setPath("/" + getWebappContext() + "/body");
429         method.setRequestHeader("Content-Type", "text/plain");
430         method.setRequestBody("");
431         client.executeMethod(method);
432         assertEquals(200,method.getStatusLine().getStatusCode());
433         response = method.getResponseBodyAsString();
434         assertTrue(response.indexOf("No body submitted") >= 0);
435
436         method.recycle();
437
438         method.setPath("/" + getWebappContext() + "/body");
439         method.setRequestHeader("Content-Type", "text/plain");
440         method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
441         client.executeMethod(method);
442         assertEquals(200,method.getStatusLine().getStatusCode());
443         response = method.getResponseBodyAsString();
444         assertTrue(response.indexOf("No body submitted") >= 0);
445
446         method.recycle();
447
448         method.setPath("/" + getWebappContext() + "/body");
449         method.setRequestHeader("Content-Type", "text/plain");
450         method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
451         method.setRequestBody((String JavaDoc)null);
452         client.executeMethod(method);
453         assertEquals(200,method.getStatusLine().getStatusCode());
454         response = method.getResponseBodyAsString();
455         assertTrue(response.indexOf("No body submitted") >= 0);
456
457         method.recycle();
458
459         method.setPath("/" + getWebappContext() + "/body");
460         method.setRequestHeader("Content-Type", "text/plain");
461         method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED);
462         method.setRequestBody((InputStream JavaDoc)null);
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         method.setRequestBody("");
474         client.executeMethod(method);
475         assertEquals(200,method.getStatusLine().getStatusCode());
476         response = method.getResponseBodyAsString();
477
478     }
479
480 }
481
Popular Tags