1   /*
2    * $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//httpclient/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java,v 1.7 2004/05/12 20:43:54 olegk Exp $
3    * $Revision: 155418 $
4    * $Date: 2005-02-26 08:01:52 -0500 (Sat, 26 Feb 2005) $
5    *
6    * ====================================================================
7    *
8    *  Copyright 2003-2004 The Apache Software Foundation
9    *
10   *  Licensed under the Apache License, Version 2.0 (the "License");
11   *  you may not use this file except in compliance with the License.
12   *  You may obtain a copy of the License at
13   *
14   *      http://www.apache.org/licenses/LICENSE-2.0
15   *
16   *  Unless required by applicable law or agreed to in writing, software
17   *  distributed under the License is distributed on an "AS IS" BASIS,
18   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19   *  See the License for the specific language governing permissions and
20   *  limitations under the License.
21   * ====================================================================
22   *
23   * This software consists of voluntary contributions made by many
24   * individuals on behalf of the Apache Software Foundation.  For more
25   * information on the Apache Software Foundation, please see
26   * <http://www.apache.org/>.
27   *
28   * [Additional notices, if required by prior licensing conditions]
29   *
30   */
31  
32  package org.apache.commons.httpclient;
33  
34  import junit.framework.*;
35  import org.apache.commons.httpclient.methods.*;
36  import java.io.*;
37  
38  /***
39   * Webapp tests specific to the PostMethod.
40   *
41   * @author <a href="jsdever@apache.org">Jeff Dever</a>
42   * @version $Id: TestWebappPostMethod.java 155418 2005-02-26 13:01:52Z dirkv $
43   */
44  public class TestWebappPostMethod extends TestWebappBase {
45  
46      HttpClient httpClient; 
47      final String paramsPath = "/" + getWebappContext() + "/params";
48      final String bodyPath = "/" + getWebappContext() + "/body";
49  
50      public TestWebappPostMethod(String testName) {
51          super(testName);
52      }
53  
54      public static Test suite() {
55          TestSuite suite = new TestSuite(TestWebappPostMethod.class);
56          return suite;
57      }
58  
59      public static void main(String args[]) {
60          String[] testCaseName = { TestWebappPostMethod.class.getName() };
61          junit.textui.TestRunner.main(testCaseName);
62      }
63  
64      public void setUp() {
65          httpClient = createHttpClient();
66      }
67  
68      /***
69       * Helper method for performing a routine test.
70       */
71      private void verifyBody(PostMethod method) throws Exception {
72          httpClient.executeMethod(method);
73  
74          assertEquals(200,method.getStatusCode());
75          String body = method.getResponseBodyAsString();
76          //System.out.println(body);
77          assertTrue(body.indexOf("Body Servlet: POST") >= 0);
78          assertTrue(body.indexOf("pname1=pvalue1&pname2=pvalue2") >= 0);
79      }
80  
81  
82      /***
83       * Helper method for performing a routine test.
84       */
85      private void verifyParams(PostMethod method) throws Exception {
86          httpClient.executeMethod(method);
87  
88          assertEquals(200,method.getStatusCode());
89          String body = method.getResponseBodyAsString();
90          //System.out.println(body);
91          assertTrue(body.indexOf("Param Servlet: POST") >= 0);
92          assertTrue(body.indexOf("QueryString=null") >= 0);
93          assertTrue(body.indexOf("name=\"pname1\";value=\"pvalue1\"") >= 0);
94          assertTrue(body.indexOf("name=\"pname2\";value=\"pvalue2\"") >= 0);
95      }
96  
97  
98      // ------------------------------------------------------------------ Tests
99      
100     /***
101      * Test that the body can be set as a array or parameters the param servlet.
102      */
103     public void testParametersBodyToParamServlet() throws Exception {
104         PostMethod method = new PostMethod(paramsPath);
105         NameValuePair[] parametersBody =  new NameValuePair[] { 
106             new NameValuePair("pname1","pvalue1"),
107             new NameValuePair("pname2","pvalue2") 
108         };
109 
110         method.setRequestBody(parametersBody);
111 
112         verifyParams(method);
113     }
114 
115     /***
116      * Test that the body can be set as a String to the param servlet.
117      */
118     public void testStringBodyToParamServlet() throws Exception {
119         PostMethod method = new PostMethod(paramsPath);
120         String stringBody = "pname1=pvalue1&pname2=pvalue2";
121 
122         method.setRequestEntity(
123             new StringRequestEntity(stringBody, PostMethod.FORM_URL_ENCODED_CONTENT_TYPE, null));
124         
125         verifyParams(method);
126     }
127 
128     /***
129      * Test that the body can be set as a String to the body servlet.
130      */
131     public void testStringBodyToBodyServlet() throws Exception {
132         PostMethod method = new PostMethod(bodyPath);
133         String stringBody = "pname1=pvalue1&pname2=pvalue2";
134 
135         method.setRequestEntity(new StringRequestEntity(stringBody));
136         
137         verifyBody(method);
138     }
139 
140     /***
141      * Test that parameters can be added.
142      */
143     public void testAddParametersToParamServlet() throws Exception {
144         PostMethod method = new PostMethod(paramsPath);
145 
146         method.addParameter(new NameValuePair("pname1","pvalue1"));
147         method.addParameter(new NameValuePair("pname2","pvalue2"));
148 
149         verifyParams(method);
150     }
151 
152     /***
153      * Test that parameters can be added and removed.
154      */
155     public void testAddRemoveParametersToParamServlet() throws Exception {
156         PostMethod method = new PostMethod(paramsPath);
157 
158         method.addParameter(new NameValuePair("pname0","pvalue0"));
159         method.addParameter(new NameValuePair("pname1","pvalue1"));
160         method.addParameter(new NameValuePair("pname2","pvalue2"));
161         method.addParameter(new NameValuePair("pname3","pvalue3"));
162         method.removeParameter("pname0");
163         method.removeParameter("pname4");
164 
165         verifyParams(method);
166     }
167 
168     /***
169      * Test the return value of the PostMethod#removeParameter.
170      */
171     public void testRemoveParameterReturnValue() throws Exception {
172         PostMethod method = new PostMethod(paramsPath);
173 
174         method.addParameter("param", "whatever");
175         assertTrue("Return value of the method is expected to be true", method.removeParameter("param"));
176         assertFalse("Return value of the method is expected to be false", method.removeParameter("param"));
177     }
178 
179     private String getRequestAsString(RequestEntity entity) throws Exception {
180         ByteArrayOutputStream bos = new ByteArrayOutputStream();
181         entity.writeRequest(bos);
182         return new String(bos.toByteArray(), "UTF-8");
183     }
184     
185     /***
186      * Test if setParameter overwrites existing parameter values.
187      */
188     public void testAddParameterFollowedBySetParameter() throws Exception {
189         PostMethod method = new PostMethod(paramsPath);
190 
191         method.addParameter("param", "a");
192         method.addParameter("param", "b");
193         method.addParameter("param", "c");
194         assertEquals("param=a&param=b&param=c", getRequestAsString(method.getRequestEntity()));
195         method.setParameter("param", "a");
196         assertEquals("param=a", getRequestAsString(method.getRequestEntity()));
197     }
198 
199 }
200