1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestWebappMethods.java,v 1.23 2004/06/13 20:22:19 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
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 junit.framework.*;
34  import org.apache.commons.httpclient.methods.*;
35  import org.apache.commons.httpclient.util.EncodingUtil;
36  
37  /***
38   * This suite of tests depends upon the httpclienttest webapp,
39   * which is available in the httpclient/src/test-webapp
40   * directory in the CVS tree.
41   * <p>
42   * The webapp should be deployed in the context "httpclienttest"
43   * on a servlet engine running on port 8080 on the localhost
44   * (IP 127.0.0.1).
45   * <p>
46   * You can change the assumed port by setting the
47   * "httpclient.test.localPort" property.
48   * You can change the assumed host by setting the
49   * "httpclient.test.localHost" property.
50   * You can change the assumed context by setting the
51   * "httpclient.test.webappContext" property.
52   *
53   * @author Rodney Waldhoff
54   * @author Ortwin Glück
55   * @version $Id: TestWebappMethods.java 155418 2005-02-26 13:01:52Z dirkv $
56   */
57  public class TestWebappMethods extends TestWebappBase {
58  
59      public TestWebappMethods(String testName) {
60          super(testName);
61      }
62  
63      public static Test suite() {
64          TestSuite suite = new TestSuite(TestWebappMethods.class);
65          return suite;
66      }
67  
68      public static void main(String args[]) {
69          String[] testCaseName = { TestWebappMethods.class.getName() };
70          junit.textui.TestRunner.main(testCaseName);
71      }
72  
73      // ------------------------------------------------------------------ Tests
74  
75      /***
76       * Simple test of {@link GetMethod} against /httpclienttest/params.
77       */
78      public void testGetMethod() throws Exception {
79          HttpClient client = createHttpClient();
80          GetMethod method = new GetMethod("/" + getWebappContext() + "/params");
81          
82          try {
83              client.executeMethod(method);
84          } catch (Throwable t) {
85              t.printStackTrace();
86              fail("Unable to execute method : " + t.toString());
87          }
88          assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
89          assertEquals(200,method.getStatusCode());
90  
91          method = new GetMethod("/" + getWebappContext() + "/params");
92          try {
93              client.executeMethod(method);
94          } catch (Throwable t) {
95              t.printStackTrace();
96              fail("Unable to execute method : " + t.toString());
97          }
98          assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0);
99          assertEquals(200,method.getStatusCode());
100     }
101 
102     /***
103      * Simple test of {@link PostMethod} against /httpclienttest/params.
104      */
105     public void testPostMethod() throws Exception {
106         HttpClient client = createHttpClient();
107         PostMethod method = new PostMethod("/" + getWebappContext() + "/params");
108         
109         try {
110             client.executeMethod(method);
111         } catch (Throwable t) {
112             t.printStackTrace();
113             fail("Unable to execute method : " + t.toString());
114         }
115         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0);
116         assertEquals(200,method.getStatusCode());
117 
118         method = new PostMethod("/" + getWebappContext() + "/params");
119         try {
120             client.executeMethod(method);
121         } catch (Throwable t) {
122             t.printStackTrace();
123             fail("Unable to execute method : " + t.toString());
124         }
125         assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0);
126         assertEquals(200,method.getStatusCode());
127     }
128 
129     /***
130      * Simple test of {@link HeadMethod} against /httpclienttest/params.
131      */
132     public void testHeadMethod() throws Exception {
133         HttpClient client = createHttpClient();
134         HeadMethod method = new HeadMethod("/" + getWebappContext() + "/params");
135         try {
136             client.executeMethod(method);
137         } catch (Throwable t) {
138             t.printStackTrace();
139             fail("Unable to execute method : " + t.toString());
140         }
141         assertEquals(200,method.getStatusCode());
142 
143         method = new HeadMethod("/" + getWebappContext() + "/params");
144         try {
145             client.executeMethod(method);
146         } catch (Throwable t) {
147             t.printStackTrace();
148             fail("Unable to execute method : " + t.toString());
149         }
150         assertEquals(200,method.getStatusCode());
151     }
152 
153     /***
154      * Simple test of {@link OptionsMethod} against /httpclienttest/params.
155      */
156     public void testOptionsMethod() throws Exception {
157         HttpClient client = createHttpClient();
158         OptionsMethod method = new OptionsMethod("/" + getWebappContext() + "/params");
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.getAllowedMethods().hasMoreElements());
167 
168         method = new OptionsMethod("/" + getWebappContext() + "/params");
169         try {
170             client.executeMethod(method);
171         } catch (Throwable t) {
172             t.printStackTrace();
173             fail("Unable to execute method : " + t.toString());
174         }
175         assertEquals(200,method.getStatusCode());
176         assertTrue(method.getAllowedMethods().hasMoreElements());
177     }
178 
179     /***
180      * Simple test of {@link OptionsMethod} against the path "*".
181      */
182     public void testOptionsStar() throws Exception {
183         HttpClient client = createHttpClient();
184         OptionsMethod method = new OptionsMethod("*");
185         try {
186             client.executeMethod(method);
187         } catch (Throwable t) {
188             t.printStackTrace();
189             fail("Unable to execute method : " + t.toString());
190         }
191         assertEquals(200,method.getStatusCode());
192         assertTrue(method.getAllowedMethods().hasMoreElements());
193     }
194 
195     /***
196      * Simple test of {@link DeleteMethod} against /httpclienttest/params.
197      */
198     public void testDeleteMethod() throws Exception {
199         HttpClient client = createHttpClient();
200         DeleteMethod method = new DeleteMethod("/" + getWebappContext() + "/params");
201         try {
202             client.executeMethod(method);
203         } catch (Throwable t) {
204             t.printStackTrace();
205             fail("Unable to execute method : " + t.toString());
206         }
207         assertEquals(200,method.getStatusCode());
208 
209         method = new DeleteMethod("/" + getWebappContext() + "/params");
210         try {
211             client.executeMethod(method);
212         } catch (Throwable t) {
213             t.printStackTrace();
214             fail("Unable to execute method : " + t.toString());
215         }
216         assertEquals(200,method.getStatusCode());
217     }
218 
219     /***
220      * Simple test of {@link PutMethod} against /httpclienttest/params.
221      */
222     public void testPutMethod() throws Exception {
223         HttpClient client = createHttpClient();
224         PutMethod method = new PutMethod("/" + getWebappContext() + "/params");
225         try {
226             client.executeMethod(method);
227         } catch (Throwable t) {
228             t.printStackTrace();
229             fail("Unable to execute method : " + t.toString());
230         }
231         assertEquals(200,method.getStatusCode());
232         assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<title>Param Servlet: PUT</title>") >= 0);
233 
234         method = new PutMethod("/" + getWebappContext() + "/params");
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(),method.getResponseBodyAsString().indexOf("<title>Param Servlet: PUT</title>") >= 0);
243     }
244 
245     public void testPostBodyNVP() throws Exception {
246         HttpClient client = createHttpClient();
247         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
248         
249         method.setRequestBody(new NameValuePair[] { 
250            new NameValuePair("quote","It was the best of times, it was the worst of times.") } );
251         try {
252             client.executeMethod(method);
253         } catch (Throwable t) {
254             t.printStackTrace();
255             fail("Unable to execute method : " + t.toString());
256         }
257         assertEquals(200,method.getStatusCode());
258         assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
259     }
260 
261     public void testPostBody() throws Exception {
262         HttpClient client = createHttpClient();
263         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
264         
265         method.setRequestEntity(new StringRequestEntity("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times."));
266         try {
267             client.executeMethod(method);
268         } catch (Throwable t) {
269             t.printStackTrace();
270             fail("Unable to execute method : " + t.toString());
271         }
272         assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
273         assertEquals(200,method.getStatusCode());
274     }
275 
276 
277     public void testPostBodyCustomLength() throws Exception {
278         HttpClient client = createHttpClient();
279         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
280         
281         String bodyStr = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.";
282 		byte[] body = EncodingUtil.getBytes(bodyStr, "ISO-8859-1");
283 
284         method.setRequestEntity(new ByteArrayRequestEntity(body));
285         client.executeMethod(method);
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 testPostBodyAutoLength() throws Exception {
292         HttpClient client = createHttpClient();
293         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
294         
295         String body = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.";
296         method.setRequestEntity(new StringRequestEntity(body));
297         client.executeMethod(method);
298         assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
299         assertEquals(200,method.getStatusCode());
300     }
301 
302 
303     public void testPostBodyChunked() throws Exception  {
304         HttpClient client = createHttpClient();
305         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
306         
307         String body = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.";
308         method.setRequestEntity(new StringRequestEntity(body));
309         method.setContentChunked(true);
310         client.executeMethod(method);
311         assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0);
312         assertEquals(200,method.getStatusLine().getStatusCode());
313     }
314 
315 
316     public void testPutBody() throws Exception {
317         HttpClient client = createHttpClient();
318         PutMethod method = new PutMethod("/" + getWebappContext() + "/body");
319         method.setRequestEntity(new StringRequestEntity("This is data to be sent in the body of an HTTP PUT."));
320         client.executeMethod(method);
321         assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<tt>This is data to be sent in the body of an HTTP PUT.</tt>") >= 0);
322         assertEquals(200,method.getStatusCode());
323     }
324 
325 
326     public void testPostMethodRecycle() throws Exception {
327         HttpClient client = createHttpClient();
328         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
329         
330         String bodyStr = "Like, hello, and stuff";
331         byte [] body = EncodingUtil.getBytes(bodyStr, "ISO-8859-1");
332         method.setRequestHeader("Content-Type", "text/plain");
333         method.setRequestEntity(new ByteArrayRequestEntity(body));
334         client.executeMethod(method);
335         assertEquals(200,method.getStatusLine().getStatusCode());
336         String response = method.getResponseBodyAsString();
337 
338         method = new PostMethod("/" + getWebappContext() + "/body");
339         method.setRequestHeader("Content-Type", "text/plain");
340         method.setRequestEntity(new ByteArrayRequestEntity(body));
341         client.executeMethod(method);
342         assertEquals(200,method.getStatusLine().getStatusCode());
343         response = method.getResponseBodyAsString();
344     }
345 
346     public void testEmptyPostMethod() throws Exception {
347         HttpClient client = createHttpClient();
348         PostMethod method = new PostMethod("/" + getWebappContext() + "/body");
349         
350         method.setRequestHeader("Content-Type", "text/plain");
351         client.executeMethod(method);
352         assertEquals(200,method.getStatusLine().getStatusCode());
353         String response = method.getResponseBodyAsString();
354         assertTrue(response.indexOf("No body submitted") >= 0);
355 
356         method = new PostMethod("/" + getWebappContext() + "/body");
357         method.setRequestHeader("Content-Type", "text/plain");
358         client.executeMethod(method);
359         assertEquals(200,method.getStatusLine().getStatusCode());
360         response = method.getResponseBodyAsString();
361         assertTrue(response.indexOf("No body submitted") >= 0);
362 
363         method = new PostMethod("/" + getWebappContext() + "/body");
364         method.setRequestHeader("Content-Type", "text/plain");
365         method.setRequestEntity(new StringRequestEntity(""));
366         client.executeMethod(method);
367         assertEquals(200,method.getStatusLine().getStatusCode());
368         response = method.getResponseBodyAsString();
369         assertTrue(response.indexOf("No body submitted") >= 0);
370 
371         method = new PostMethod("/" + getWebappContext() + "/body");
372         method.setRequestHeader("Content-Type", "text/plain");
373         method.setContentChunked(true);
374         client.executeMethod(method);
375         assertEquals(200,method.getStatusLine().getStatusCode());
376         response = method.getResponseBodyAsString();
377         assertTrue(response.indexOf("No body submitted") >= 0);
378 
379         method = new PostMethod("/" + getWebappContext() + "/body");
380         method.setRequestHeader("Content-Type", "text/plain");
381         method.setRequestEntity(new StringRequestEntity(""));
382         method.setContentChunked(true);
383         client.executeMethod(method);
384         assertEquals(200,method.getStatusLine().getStatusCode());
385         response = method.getResponseBodyAsString();
386 
387     }
388 
389 }