001: /*
002: * $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 $
003: * $Revision: 480424 $
004: * $Date: 2006-11-29 06:56:49 +0100 (Wed, 29 Nov 2006) $
005: *
006: * ====================================================================
007: *
008: * Licensed to the Apache Software Foundation (ASF) under one or more
009: * contributor license agreements. See the NOTICE file distributed with
010: * this work for additional information regarding copyright ownership.
011: * The ASF licenses this file to You under the Apache License, Version 2.0
012: * (the "License"); you may not use this file except in compliance with
013: * the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing, software
018: * distributed under the License is distributed on an "AS IS" BASIS,
019: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
020: * See the License for the specific language governing permissions and
021: * limitations under the License.
022: * ====================================================================
023: *
024: * This software consists of voluntary contributions made by many
025: * individuals on behalf of the Apache Software Foundation. For more
026: * information on the Apache Software Foundation, please see
027: * <http://www.apache.org/>.
028: *
029: */
030:
031: package org.apache.commons.httpclient;
032:
033: import junit.framework.*;
034: import org.apache.commons.httpclient.methods.*;
035: import java.io.*;
036:
037: /**
038: * Webapp tests specific to the PostMethod.
039: *
040: * @author <a href="jsdever@apache.org">Jeff Dever</a>
041: * @version $Id: TestPostMethod.java 480424 2006-11-29 05:56:49Z bayard $
042: */
043: public class TestPostMethod extends HttpClientTestBase {
044:
045: public TestPostMethod(String testName) throws IOException {
046: super (testName);
047: }
048:
049: public static Test suite() {
050: TestSuite suite = new TestSuite(TestPostMethod.class);
051: return suite;
052: }
053:
054: public static void main(String args[]) {
055: String[] testCaseName = { TestPostMethod.class.getName() };
056: junit.textui.TestRunner.main(testCaseName);
057: }
058:
059: // ------------------------------------------------------------------ Tests
060:
061: /**
062: * Test that the body can be set as a array of parameters
063: */
064: public void testParametersBodyToParamServlet() throws Exception {
065: PostMethod method = new PostMethod("/");
066: NameValuePair[] parametersBody = new NameValuePair[] {
067: new NameValuePair("pname1", "pvalue1"),
068: new NameValuePair("pname2", "pvalue2") };
069: method.setRequestBody(parametersBody);
070: this .server.setHttpService(new EchoService());
071: try {
072: this .client.executeMethod(method);
073: assertEquals(200, method.getStatusCode());
074: String body = method.getResponseBodyAsString();
075: assertEquals("pname1=pvalue1&pname2=pvalue2", body);
076: } finally {
077: method.releaseConnection();
078: }
079: }
080:
081: /**
082: * Test that the body can be set as a String
083: */
084: public void testStringBodyToParamServlet() throws Exception {
085: PostMethod method = new PostMethod("/");
086: String stringBody = "pname1=pvalue1&pname2=pvalue2";
087: method.setRequestEntity(new StringRequestEntity(stringBody,
088: PostMethod.FORM_URL_ENCODED_CONTENT_TYPE, null));
089: this .server.setHttpService(new EchoService());
090: try {
091: this .client.executeMethod(method);
092: assertEquals(200, method.getStatusCode());
093: String body = method.getResponseBodyAsString();
094: assertEquals("pname1=pvalue1&pname2=pvalue2", body);
095: } finally {
096: method.releaseConnection();
097: }
098: }
099:
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: null, null));
110: this .server.setHttpService(new EchoService());
111: try {
112: this .client.executeMethod(method);
113: assertEquals(200, method.getStatusCode());
114: String 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 {
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 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()
145: throws Exception {
146: PostMethod method = new PostMethod("/");
147:
148: method.addParameter(new NameValuePair("pname0", "pvalue0"));
149: method.addParameter(new NameValuePair("pname1", "pvalue1"));
150: method.addParameter(new NameValuePair("pname2", "pvalue2"));
151: method.addParameter(new NameValuePair("pname3", "pvalue3"));
152: method.removeParameter("pname0");
153: method.removeParameter("pname3");
154:
155: this .server.setHttpService(new EchoService());
156: try {
157: this .client.executeMethod(method);
158: assertEquals(200, method.getStatusCode());
159: String body = method.getResponseBodyAsString();
160: assertEquals("pname1=pvalue1&pname2=pvalue2", body);
161: } finally {
162: method.releaseConnection();
163: }
164: }
165:
166: /**
167: * Test the return value of the PostMethod#removeParameter.
168: */
169: public void testRemoveParameterReturnValue() throws Exception {
170: PostMethod method = new PostMethod("/");
171:
172: method.addParameter("param", "whatever");
173: assertTrue("Return value of the method is expected to be true",
174: method.removeParameter("param"));
175: assertFalse(
176: "Return value of the method is expected to be false",
177: method.removeParameter("param"));
178: }
179:
180: private String getRequestAsString(RequestEntity entity)
181: throws Exception {
182: ByteArrayOutputStream bos = new ByteArrayOutputStream();
183: entity.writeRequest(bos);
184: return new String(bos.toByteArray(), "UTF-8");
185: }
186:
187: /**
188: * Test if setParameter overwrites existing parameter values.
189: */
190: public void testAddParameterFollowedBySetParameter()
191: throws Exception {
192: PostMethod method = new PostMethod("/");
193:
194: method.addParameter("param", "a");
195: method.addParameter("param", "b");
196: method.addParameter("param", "c");
197: assertEquals("param=a¶m=b¶m=c",
198: getRequestAsString(method.getRequestEntity()));
199: method.setParameter("param", "a");
200: assertEquals("param=a", getRequestAsString(method
201: .getRequestEntity()));
202: }
203:
204: }
|