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: 161334 $
4    * $Date: 2005-04-14 14:23:30 -0400 (Thu, 14 Apr 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   */
29  
30  package org.apache.commons.httpclient;
31  
32  import junit.framework.*;
33  import org.apache.commons.httpclient.methods.*;
34  import java.io.*;
35  
36  /***
37   * Webapp tests specific to the PostMethod.
38   *
39   * @author <a href="jsdever@apache.org">Jeff Dever</a>
40   * @version $Id: TestPostMethod.java 161334 2005-04-14 18:23:30Z olegk $
41   */
42  public class TestPostMethod extends HttpClientTestBase {
43  
44      public TestPostMethod(String testName) throws IOException {
45          super(testName);
46      }
47  
48      public static Test suite() {
49          TestSuite suite = new TestSuite(TestPostMethod.class);
50          return suite;
51      }
52  
53      public static void main(String args[]) {
54          String[] testCaseName = { TestPostMethod.class.getName() };
55          junit.textui.TestRunner.main(testCaseName);
56      }
57  
58      // ------------------------------------------------------------------ Tests
59      
60      /***
61       * Test that the body can be set as a array of parameters
62       */
63      public void testParametersBodyToParamServlet() throws Exception {
64          PostMethod method = new PostMethod("/");
65          NameValuePair[] parametersBody =  new NameValuePair[] { 
66              new NameValuePair("pname1","pvalue1"),
67              new NameValuePair("pname2","pvalue2") 
68          };
69          method.setRequestBody(parametersBody);
70          this.server.setHttpService(new EchoService());
71          try {
72              this.client.executeMethod(method);
73              assertEquals(200, method.getStatusCode());
74              String body = method.getResponseBodyAsString();
75              assertEquals("pname1=pvalue1&pname2=pvalue2", body);
76          } finally {
77              method.releaseConnection();
78          }
79      }
80  
81      /***
82       * Test that the body can be set as a String
83       */
84      public void testStringBodyToParamServlet() throws Exception {
85          PostMethod method = new PostMethod("/");
86          String stringBody = "pname1=pvalue1&pname2=pvalue2";
87          method.setRequestEntity(
88              new StringRequestEntity(stringBody, PostMethod.FORM_URL_ENCODED_CONTENT_TYPE, null));
89          this.server.setHttpService(new EchoService());
90          try {
91              this.client.executeMethod(method);
92              assertEquals(200, method.getStatusCode());
93              String body = method.getResponseBodyAsString();
94              assertEquals("pname1=pvalue1&pname2=pvalue2", body);
95          } finally {
96              method.releaseConnection();
97          }
98      }
99  
100     /***
101      * Test that the body can be set as a String without an explict 
102      * content type
103      */
104     public void testStringBodyToBodyServlet() throws Exception {
105         PostMethod method = new PostMethod("/");
106         String stringBody = "pname1=pvalue1&pname2=pvalue2";
107 
108         method.setRequestEntity(new StringRequestEntity(stringBody));
109         this.server.setHttpService(new EchoService());
110         try {
111             this.client.executeMethod(method);
112             assertEquals(200, method.getStatusCode());
113             String body = method.getResponseBodyAsString();
114             assertEquals("pname1=pvalue1&pname2=pvalue2", body);
115         } finally {
116             method.releaseConnection();
117         }
118     }
119 
120     /***
121      * Test that parameters can be added.
122      */
123     public void testAddParametersToParamServlet() throws Exception {
124         PostMethod method = new PostMethod("/");
125 
126         method.addParameter(new NameValuePair("pname1","pvalue1"));
127         method.addParameter(new NameValuePair("pname2","pvalue2"));
128 
129         this.server.setHttpService(new EchoService());
130         try {
131             this.client.executeMethod(method);
132             assertEquals(200, method.getStatusCode());
133             String body = method.getResponseBodyAsString();
134             assertEquals("pname1=pvalue1&pname2=pvalue2", body);
135         } finally {
136             method.releaseConnection();
137         }
138     }
139 
140     /***
141      * Test that parameters can be added and removed.
142      */
143     public void testAddRemoveParametersToParamServlet() throws Exception {
144         PostMethod method = new PostMethod("/");
145 
146         method.addParameter(new NameValuePair("pname0","pvalue0"));
147         method.addParameter(new NameValuePair("pname1","pvalue1"));
148         method.addParameter(new NameValuePair("pname2","pvalue2"));
149         method.addParameter(new NameValuePair("pname3","pvalue3"));
150         method.removeParameter("pname0");
151         method.removeParameter("pname3");
152 
153         this.server.setHttpService(new EchoService());
154         try {
155             this.client.executeMethod(method);
156             assertEquals(200, method.getStatusCode());
157             String body = method.getResponseBodyAsString();
158             assertEquals("pname1=pvalue1&pname2=pvalue2", body);
159         } finally {
160             method.releaseConnection();
161         }
162     }
163 
164     /***
165      * Test the return value of the PostMethod#removeParameter.
166      */
167     public void testRemoveParameterReturnValue() throws Exception {
168         PostMethod method = new PostMethod("/");
169 
170         method.addParameter("param", "whatever");
171         assertTrue("Return value of the method is expected to be true", method.removeParameter("param"));
172         assertFalse("Return value of the method is expected to be false", method.removeParameter("param"));
173     }
174 
175     private String getRequestAsString(RequestEntity entity) throws Exception {
176         ByteArrayOutputStream bos = new ByteArrayOutputStream();
177         entity.writeRequest(bos);
178         return new String(bos.toByteArray(), "UTF-8");
179     }
180     
181     /***
182      * Test if setParameter overwrites existing parameter values.
183      */
184     public void testAddParameterFollowedBySetParameter() throws Exception {
185         PostMethod method = new PostMethod("/");
186 
187         method.addParameter("param", "a");
188         method.addParameter("param", "b");
189         method.addParameter("param", "c");
190         assertEquals("param=a&param=b&param=c", getRequestAsString(method.getRequestEntity()));
191         method.setParameter("param", "a");
192         assertEquals("param=a", getRequestAsString(method.getRequestEntity()));
193     }
194 
195 }
196