KickJava   Java API By Example, From Geeks To Geeks.

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


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: 480424 $
4  * $Date: 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) $
5  *
6  * ====================================================================
7  *
8  * Licensed to the Apache Software Foundation (ASF) under one or more
9  * contributor license agreements. See the NOTICE file distributed with
10  * this work for additional information regarding copyright ownership.
11  * The ASF licenses this file to You under the Apache License, Version 2.0
12  * (the "License"); you may not use this file except in compliance with
13  * the License. You may obtain a copy of the License at
14  *
15  * http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  * ====================================================================
23  *
24  * This software consists of voluntary contributions made by many
25  * individuals on behalf of the Apache Software Foundation. For more
26  * information on the Apache Software Foundation, please see
27  * <http://www.apache.org/>.
28  *
29  */

30
31 package org.apache.commons.httpclient;
32
33 import junit.framework.*;
34 import org.apache.commons.httpclient.methods.*;
35 import java.io.*;
36
37 /**
38  * Webapp tests specific to the PostMethod.
39  *
40  * @author <a HREF="jsdever@apache.org">Jeff Dever</a>
41  * @version $Id: TestPostMethod.java 480424 2006-11-29 05:56:49 +0000 (Wed, 29 Nov 2006) bayard $
42  */

43 public class TestPostMethod extends HttpClientTestBase {
44
45     public TestPostMethod(String JavaDoc testName) throws IOException {
46         super(testName);
47     }
48
49     public static Test suite() {
50         TestSuite suite = new TestSuite(TestPostMethod.class);
51         return suite;
52     }
53
54     public static void main(String JavaDoc args[]) {
55         String JavaDoc[] testCaseName = { TestPostMethod.class.getName() };
56         junit.textui.TestRunner.main(testCaseName);
57     }
58
59     // ------------------------------------------------------------------ Tests
60

61     /**
62      * Test that the body can be set as a array of parameters
63      */

64     public void testParametersBodyToParamServlet() throws Exception JavaDoc {
65         PostMethod method = new PostMethod("/");
66         NameValuePair[] parametersBody = new NameValuePair[] {
67             new NameValuePair("pname1","pvalue1"),
68             new NameValuePair("pname2","pvalue2")
69         };
70         method.setRequestBody(parametersBody);
71         this.server.setHttpService(new EchoService());
72         try {
73             this.client.executeMethod(method);
74             assertEquals(200, method.getStatusCode());
75             String JavaDoc body = method.getResponseBodyAsString();
76             assertEquals("pname1=pvalue1&pname2=pvalue2", body);
77         } finally {
78             method.releaseConnection();
79         }
80     }
81
82     /**
83      * Test that the body can be set as a String
84      */

85     public void testStringBodyToParamServlet() throws Exception JavaDoc {
86         PostMethod method = new PostMethod("/");
87         String JavaDoc stringBody = "pname1=pvalue1&pname2=pvalue2";
88         method.setRequestEntity(
89             new StringRequestEntity(stringBody, PostMethod.FORM_URL_ENCODED_CONTENT_TYPE, null));
90         this.server.setHttpService(new EchoService());
91         try {
92             this.client.executeMethod(method);
93             assertEquals(200, method.getStatusCode());
94             String JavaDoc body = method.getResponseBodyAsString();
95             assertEquals("pname1=pvalue1&pname2=pvalue2", body);
96         } finally {
97             method.releaseConnection();
98         }
99     }
100
101     /**
102      * Test that the body can be set as a String without an explict
103      * content type
104      */

105     public void testStringBodyToBodyServlet() throws Exception JavaDoc {
106         PostMethod method = new PostMethod("/");
107         String JavaDoc stringBody = "pname1=pvalue1&pname2=pvalue2";
108
109         method.setRequestEntity(new StringRequestEntity(stringBody, null, null));
110         this.server.setHttpService(new EchoService());
111         try {
112             this.client.executeMethod(method);
113             assertEquals(200, method.getStatusCode());
114             String JavaDoc body = method.getResponseBodyAsString();
115             assertEquals("pname1=pvalue1&pname2=pvalue2", body);
116         } finally {
117             method.releaseConnection();
118         }
119     }
120
121     /**
122      * Test that parameters can be added.
123      */

124     public void testAddParametersToParamServlet() throws Exception JavaDoc {
125         PostMethod method = new PostMethod("/");
126
127         method.addParameter(new NameValuePair("pname1","pvalue1"));
128         method.addParameter(new NameValuePair("pname2","pvalue2"));
129
130         this.server.setHttpService(new EchoService());
131         try {
132             this.client.executeMethod(method);
133             assertEquals(200, method.getStatusCode());
134             String JavaDoc body = method.getResponseBodyAsString();
135             assertEquals("pname1=pvalue1&pname2=pvalue2", body);
136         } finally {
137             method.releaseConnection();
138         }
139     }
140
141     /**
142      * Test that parameters can be added and removed.
143      */

144     public void testAddRemoveParametersToParamServlet() throws Exception JavaDoc {
145         PostMethod method = new PostMethod("/");
146
147         method.addParameter(new NameValuePair("pname0","pvalue0"));
148         method.addParameter(new NameValuePair("pname1","pvalue1"));
149         method.addParameter(new NameValuePair("pname2","pvalue2"));
150         method.addParameter(new NameValuePair("pname3","pvalue3"));
151         method.removeParameter("pname0");
152         method.removeParameter("pname3");
153
154         this.server.setHttpService(new EchoService());
155         try {
156             this.client.executeMethod(method);
157             assertEquals(200, method.getStatusCode());
158             String JavaDoc body = method.getResponseBodyAsString();
159             assertEquals("pname1=pvalue1&pname2=pvalue2", body);
160         } finally {
161             method.releaseConnection();
162         }
163     }
164
165     /**
166      * Test the return value of the PostMethod#removeParameter.
167      */

168     public void testRemoveParameterReturnValue() throws Exception JavaDoc {
169         PostMethod method = new PostMethod("/");
170
171         method.addParameter("param", "whatever");
172         assertTrue("Return value of the method is expected to be true", method.removeParameter("param"));
173         assertFalse("Return value of the method is expected to be false", method.removeParameter("param"));
174     }
175
176     private String JavaDoc getRequestAsString(RequestEntity entity) throws Exception JavaDoc {
177         ByteArrayOutputStream bos = new ByteArrayOutputStream();
178         entity.writeRequest(bos);
179         return new String JavaDoc(bos.toByteArray(), "UTF-8");
180     }
181     
182     /**
183      * Test if setParameter overwrites existing parameter values.
184      */

185     public void testAddParameterFollowedBySetParameter() throws Exception JavaDoc {
186         PostMethod method = new PostMethod("/");
187
188         method.addParameter("param", "a");
189         method.addParameter("param", "b");
190         method.addParameter("param", "c");
191         assertEquals("param=a&param=b&param=c", getRequestAsString(method.getRequestEntity()));
192         method.setParameter("param", "a");
193         assertEquals("param=a", getRequestAsString(method.getRequestEntity()));
194     }
195
196 }
197
198
Popular Tags