KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java,v 1.3.2.2 2004/02/22 18:21:16 olegk Exp $
3  * $Revision: 1.3.2.2 $
4  * $Date: 2004/02/22 18:21:16 $
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,v 1.3.2.2 2004/02/22 18:21:16 olegk Exp $
43  */

44 public class TestWebappPostMethod extends TestWebappBase {
45
46     HttpClient httpClient;
47     final String JavaDoc paramsPath = "/" + getWebappContext() + "/params";
48     final String JavaDoc bodyPath = "/" + getWebappContext() + "/body";
49
50     public TestWebappPostMethod(String JavaDoc 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 JavaDoc args[]) {
60         String JavaDoc[] 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 JavaDoc {
72         httpClient.executeMethod(method);
73
74         assertEquals(200,method.getStatusCode());
75         String JavaDoc 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 JavaDoc {
86         httpClient.executeMethod(method);
87
88         assertEquals(200,method.getStatusCode());
89         String JavaDoc 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 JavaDoc {
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 JavaDoc {
119         PostMethod method = new PostMethod(paramsPath);
120         String JavaDoc stringBody = "pname1=pvalue1&pname2=pvalue2";
121
122         method.setRequestBody(stringBody);
123         method.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE);
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 JavaDoc {
132         PostMethod method = new PostMethod(bodyPath);
133         String JavaDoc stringBody = "pname1=pvalue1&pname2=pvalue2";
134
135         method.setRequestBody(stringBody);
136         
137         verifyBody(method);
138     }
139
140     /**
141      * Test that the body can be set as a stream to the param servlet.
142      */

143     public void testStreamBodyToParamServlet() throws Exception JavaDoc {
144         PostMethod method = new PostMethod(paramsPath);
145         InputStream streamBody =
146             new ByteArrayInputStream("pname1=pvalue1&pname2=pvalue2".getBytes());
147
148         method.setRequestBody(streamBody);
149         method.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE);
150         
151         verifyParams(method);
152     }
153
154     /**
155      * Test that the body can be set as a stream to the body servlet.
156      */

157     public void testStreamBodyToBodyServlet() throws Exception JavaDoc {
158         PostMethod method = new PostMethod(bodyPath);
159         
160         InputStream streamBody =
161             new ByteArrayInputStream("pname1=pvalue1&pname2=pvalue2".getBytes());
162         method.setRequestBody(streamBody);
163         
164         verifyBody(method);
165     }
166
167     /**
168      * Test that parameters can be added.
169      */

170     public void testAddParametersToParamServlet() throws Exception JavaDoc {
171         PostMethod method = new PostMethod(paramsPath);
172
173         method.addParameter(new NameValuePair("pname1","pvalue1"));
174         method.addParameter(new NameValuePair("pname2","pvalue2"));
175
176         verifyParams(method);
177     }
178
179     /**
180      * Test that parameters can be added and removed.
181      */

182     public void testAddRemoveParametersToParamServlet() throws Exception JavaDoc {
183         PostMethod method = new PostMethod(paramsPath);
184
185         method.addParameter(new NameValuePair("pname0","pvalue0"));
186         method.addParameter(new NameValuePair("pname1","pvalue1"));
187         method.addParameter(new NameValuePair("pname2","pvalue2"));
188         method.addParameter(new NameValuePair("pname3","pvalue3"));
189         method.removeParameter("pname0");
190         method.removeParameter("pname4");
191
192         verifyParams(method);
193     }
194
195     /**
196      * Test the return value of the PostMethod#removeParameter.
197      */

198     public void testRemoveParameterReturnValue() throws Exception JavaDoc {
199         PostMethod method = new PostMethod(paramsPath);
200
201         method.addParameter("param", "whatever");
202         assertTrue("Return value of the method is expected to be true", method.removeParameter("param"));
203         assertFalse("Return value of the method is expected to be false", method.removeParameter("param"));
204     }
205
206     /**
207      * Test if setParameter overwrites existing parameter values.
208      */

209     public void testAddParameterFollowedBySetParameter() throws Exception JavaDoc {
210         PostMethod method = new PostMethod(paramsPath);
211
212         method.addParameter("param", "a");
213         method.addParameter("param", "b");
214         method.addParameter("param", "c");
215         assertEquals("param=a&param=b&param=c", method.getRequestBodyAsString());
216         method.setParameter("param", "a");
217         assertEquals("param=a", method.getRequestBodyAsString());
218     }
219
220 }
221
222
Popular Tags