1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappPostMethod.java,v 1.3.2.1 2003/09/12 07:33:20 olegk Exp $ 3 * $Revision: 1.3.2.1 $ 4 * $Date: 2003/09/12 07:33:20 $ 5 * 6 * ==================================================================== 7 * 8 * The Apache Software License, Version 1.1 9 * 10 * Copyright (c) 2003 The Apache Software Foundation. All rights 11 * reserved. 12 * 13 * Redistribution and use in source and binary forms, with or without 14 * modification, are permitted provided that the following conditions 15 * are met: 16 * 17 * 1. Redistributions of source code must retain the above copyright 18 * notice, this list of conditions and the following disclaimer. 19 * 20 * 2. Redistributions in binary form must reproduce the above copyright 21 * notice, this list of conditions and the following disclaimer in 22 * the documentation and/or other materials provided with the 23 * distribution. 24 * 25 * 3. The end-user documentation included with the redistribution, if 26 * any, must include the following acknowlegement: 27 * "This product includes software developed by the 28 * Apache Software Foundation (http://www.apache.org/)." 29 * Alternately, this acknowlegement may appear in the software itself, 30 * if and wherever such third-party acknowlegements normally appear. 31 * 32 * 4. The names "The Jakarta Project", "Commons", and "Apache Software 33 * Foundation" must not be used to endorse or promote products derived 34 * from this software without prior written permission. For written 35 * permission, please contact apache@apache.org. 36 * 37 * 5. Products derived from this software may not be called "Apache" 38 * nor may "Apache" appear in their names without prior written 39 * permission of the Apache Group. 40 * 41 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 42 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 43 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 44 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 45 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 46 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 47 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 48 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 49 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 50 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 51 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 52 * SUCH DAMAGE. 53 * ==================================================================== 54 * 55 * This software consists of voluntary contributions made by many 56 * individuals on behalf of the Apache Software Foundation. For more 57 * information on the Apache Software Foundation, please see 58 * <http://www.apache.org/>. 59 * 60 * [Additional notices, if required by prior licensing conditions] 61 * 62 */ 63 64 package org.apache.commons.httpclient; 65 66 import junit.framework.*; 67 import org.apache.commons.httpclient.methods.*; 68 import java.io.*; 69 70 /*** 71 * Webapp tests specific to the PostMethod. 72 * 73 * @author <a href="jsdever@apache.org">Jeff Dever</a> 74 * @version $Id: TestWebappPostMethod.java,v 1.3.2.1 2003/09/12 07:33:20 olegk Exp $ 75 */ 76 public class TestWebappPostMethod extends TestWebappBase { 77 78 HttpClient httpClient; 79 final String paramsPath = "/" + getWebappContext() + "/params"; 80 final String bodyPath = "/" + getWebappContext() + "/body"; 81 82 public TestWebappPostMethod(String testName) { 83 super(testName); 84 } 85 86 public static Test suite() { 87 TestSuite suite = new TestSuite(TestWebappPostMethod.class); 88 return suite; 89 } 90 91 public static void main(String args[]) { 92 String[] testCaseName = { TestWebappPostMethod.class.getName() }; 93 junit.textui.TestRunner.main(testCaseName); 94 } 95 96 public void setUp() { 97 httpClient = createHttpClient(); 98 } 99 100 /*** 101 * Helper method for performing a routine test. 102 */ 103 private void verifyBody(PostMethod method) throws Exception { 104 httpClient.executeMethod(method); 105 106 assertEquals(200,method.getStatusCode()); 107 String body = method.getResponseBodyAsString(); 108 //System.out.println(body); 109 assertTrue(body.indexOf("Body Servlet: POST") >= 0); 110 assertTrue(body.indexOf("pname1=pvalue1&pname2=pvalue2") >= 0); 111 } 112 113 114 /*** 115 * Helper method for performing a routine test. 116 */ 117 private void verifyParams(PostMethod method) throws Exception { 118 httpClient.executeMethod(method); 119 120 assertEquals(200,method.getStatusCode()); 121 String body = method.getResponseBodyAsString(); 122 //System.out.println(body); 123 assertTrue(body.indexOf("Param Servlet: POST") >= 0); 124 assertTrue(body.indexOf("QueryString=null") >= 0); 125 assertTrue(body.indexOf("name=\"pname1\";value=\"pvalue1\"") >= 0); 126 assertTrue(body.indexOf("name=\"pname2\";value=\"pvalue2\"") >= 0); 127 } 128 129 130 // ------------------------------------------------------------------ Tests 131 132 /*** 133 * Test that the body can be set as a array or parameters the param servlet. 134 */ 135 public void testParametersBodyToParamServlet() throws Exception { 136 PostMethod method = new PostMethod(paramsPath); 137 NameValuePair[] parametersBody = new NameValuePair[] { 138 new NameValuePair("pname1","pvalue1"), 139 new NameValuePair("pname2","pvalue2") 140 }; 141 142 method.setRequestBody(parametersBody); 143 144 verifyParams(method); 145 } 146 147 /*** 148 * Test that the body can be set as a String to the param servlet. 149 */ 150 public void testStringBodyToParamServlet() throws Exception { 151 PostMethod method = new PostMethod(paramsPath); 152 String stringBody = "pname1=pvalue1&pname2=pvalue2"; 153 154 method.setRequestBody(stringBody); 155 method.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE); 156 157 verifyParams(method); 158 } 159 160 /*** 161 * Test that the body can be set as a String to the body servlet. 162 */ 163 public void testStringBodyToBodyServlet() throws Exception { 164 PostMethod method = new PostMethod(bodyPath); 165 String stringBody = "pname1=pvalue1&pname2=pvalue2"; 166 167 method.setRequestBody(stringBody); 168 169 verifyBody(method); 170 } 171 172 /*** 173 * Test that the body can be set as a stream to the param servlet. 174 */ 175 public void testStreamBodyToParamServlet() throws Exception { 176 PostMethod method = new PostMethod(paramsPath); 177 InputStream streamBody = 178 new ByteArrayInputStream("pname1=pvalue1&pname2=pvalue2".getBytes()); 179 180 method.setRequestBody(streamBody); 181 method.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE); 182 183 verifyParams(method); 184 } 185 186 /*** 187 * Test that the body can be set as a stream to the body servlet. 188 */ 189 public void testStreamBodyToBodyServlet() throws Exception { 190 PostMethod method = new PostMethod(bodyPath); 191 192 InputStream streamBody = 193 new ByteArrayInputStream("pname1=pvalue1&pname2=pvalue2".getBytes()); 194 method.setRequestBody(streamBody); 195 196 verifyBody(method); 197 } 198 199 /*** 200 * Test that parameters can be added. 201 */ 202 public void testAddParametersToParamServlet() throws Exception { 203 PostMethod method = new PostMethod(paramsPath); 204 205 method.addParameter(new NameValuePair("pname1","pvalue1")); 206 method.addParameter(new NameValuePair("pname2","pvalue2")); 207 208 verifyParams(method); 209 } 210 211 /*** 212 * Test that parameters can be added and removed. 213 */ 214 public void testAddRemoveParametersToParamServlet() throws Exception { 215 PostMethod method = new PostMethod(paramsPath); 216 217 method.addParameter(new NameValuePair("pname0","pvalue0")); 218 method.addParameter(new NameValuePair("pname1","pvalue1")); 219 method.addParameter(new NameValuePair("pname2","pvalue2")); 220 method.addParameter(new NameValuePair("pname3","pvalue3")); 221 method.removeParameter("pname0"); 222 method.removeParameter("pname4"); 223 224 verifyParams(method); 225 } 226 227 /*** 228 * Test the return value of the PostMethod#removeParameter. 229 */ 230 public void testRemoveParameterReturnValue() throws Exception { 231 PostMethod method = new PostMethod(paramsPath); 232 233 method.addParameter("param", "whatever"); 234 assertTrue("Return value of the method is expected to be true", method.removeParameter("param")); 235 assertFalse("Return value of the method is expected to be false", method.removeParameter("param")); 236 } 237 238 /*** 239 * Test if setParameter overwrites existing parameter values. 240 */ 241 public void testAddParameterFollowedBySetParameter() throws Exception { 242 PostMethod method = new PostMethod(paramsPath); 243 244 method.addParameter("param", "a"); 245 method.addParameter("param", "b"); 246 method.addParameter("param", "c"); 247 assertEquals("param=a¶m=b¶m=c", method.getRequestBodyAsString()); 248 method.setParameter("param", "a"); 249 assertEquals("param=a", method.getRequestBodyAsString()); 250 } 251 252 } 253

This page was automatically generated by Maven