1 /* 2 * $Header: /home/cvs/jakarta-commons/httpclient/src/test/org/apache/commons/httpclient/TestWebappParameters.java,v 1.10 2003/03/05 04:02:56 mbecke Exp $ 3 * $Revision: 1.10 $ 4 * $Date: 2003/03/05 04:02:56 $ 5 * ==================================================================== 6 * 7 * The Apache Software License, Version 1.1 8 * 9 * Copyright (c) 1999-2003 The Apache Software Foundation. All rights 10 * reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 16 * 1. Redistributions of source code must retain the above copyright 17 * notice, this list of conditions and the following disclaimer. 18 * 19 * 2. Redistributions in binary form must reproduce the above copyright 20 * notice, this list of conditions and the following disclaimer in 21 * the documentation and/or other materials provided with the 22 * distribution. 23 * 24 * 3. The end-user documentation included with the redistribution, if 25 * any, must include the following acknowlegement: 26 * "This product includes software developed by the 27 * Apache Software Foundation (http://www.apache.org/)." 28 * Alternately, this acknowlegement may appear in the software itself, 29 * if and wherever such third-party acknowlegements normally appear. 30 * 31 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software 32 * Foundation" must not be used to endorse or promote products derived 33 * from this software without prior written permission. For written 34 * permission, please contact apache@apache.org. 35 * 36 * 5. Products derived from this software may not be called "Apache" 37 * nor may "Apache" appear in their names without prior written 38 * permission of the Apache Group. 39 * 40 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 41 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 42 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 43 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR 44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 46 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF 47 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 48 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 49 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 50 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 51 * SUCH DAMAGE. 52 * ==================================================================== 53 * 54 * This software consists of voluntary contributions made by many 55 * individuals on behalf of the Apache Software Foundation. For more 56 * information on the Apache Software Foundation, please see 57 * <http://www.apache.org/>. 58 * 59 * [Additional notices, if required by prior licensing conditions] 60 * 61 */ 62 63 package org.apache.commons.httpclient; 64 65 import junit.framework.*; 66 import org.apache.commons.httpclient.methods.*; 67 68 /*** 69 * This suite of tests depends upon the httpclienttest webapp, 70 * which is available in the httpclient/src/test-webapp 71 * directory in the CVS tree. 72 * <p> 73 * The webapp should be deployed in the context "httpclienttest" 74 * on a servlet engine running on port 8080 on the localhost 75 * (IP 127.0.0.1). 76 * <p> 77 * You can change the assumed port by setting the 78 * "httpclient.test.localPort" property. 79 * You can change the assumed host by setting the 80 * "httpclient.test.localHost" property. 81 * You can change the assumed context by setting the 82 * "httpclient.test.webappContext" property. 83 * 84 * @author Rodney Waldhoff 85 * @version $Id: TestWebappParameters.java,v 1.10 2003/03/05 04:02:56 mbecke Exp $ 86 */ 87 public class TestWebappParameters extends TestWebappBase { 88 89 public TestWebappParameters(String testName) { 90 super(testName); 91 } 92 93 public static Test suite() { 94 TestSuite suite = new TestSuite(TestWebappParameters.class); 95 return suite; 96 } 97 98 public static void main(String args[]) { 99 String[] testCaseName = { TestWebappParameters.class.getName() }; 100 junit.textui.TestRunner.main(testCaseName); 101 } 102 103 // ------------------------------------------------------------------ Tests 104 105 /*** 106 * Test that {@link GetMethod#setQueryString(java.lang.String)} 107 * can include a leading question mark. 108 */ 109 public void testGetMethodQueryString() throws Exception { 110 HttpClient client = createHttpClient(); 111 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 112 method.setQueryString("?hadQuestionMark=true"); 113 114 try { 115 client.executeMethod(method); 116 } catch (Throwable t) { 117 t.printStackTrace(); 118 fail("Unable to execute method : " + t.toString()); 119 } 120 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 121 assertEquals(200,method.getStatusCode()); 122 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"hadQuestionMark=true\"</p>") >= 0); 123 } 124 125 /*** 126 * Test that {@link GetMethod#setQueryString(java.lang.String)} 127 * doesn't have to include a leading question mark. 128 */ 129 public void testGetMethodQueryString2() throws Exception { 130 HttpClient client = createHttpClient(); 131 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 132 method.setQueryString("hadQuestionMark=false"); 133 134 try { 135 client.executeMethod(method); 136 } catch (Throwable t) { 137 t.printStackTrace(); 138 fail("Unable to execute method : " + t.toString()); 139 } 140 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 141 assertEquals(200,method.getStatusCode()); 142 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"hadQuestionMark=false\"</p>") >= 0); 143 } 144 145 /*** 146 * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)} 147 * values get added to the query string. 148 */ 149 public void testGetMethodParameters() throws Exception { 150 HttpClient client = createHttpClient(); 151 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 152 method.setQueryString(new NameValuePair[] { new NameValuePair("param-one","param-value") }); 153 154 try { 155 client.executeMethod(method); 156 } catch (Throwable t) { 157 t.printStackTrace(); 158 fail("Unable to execute method : " + t.toString()); 159 } 160 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 161 assertEquals(200,method.getStatusCode()); 162 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"param-one=param-value\"</p>") >= 0); 163 } 164 165 /*** 166 * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)} 167 * works with multiple parameters. 168 */ 169 public void testGetMethodMultiParameters() throws Exception { 170 HttpClient client = createHttpClient(); 171 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 172 method.setQueryString(new NameValuePair[] { 173 new NameValuePair("param-one","param-value"), 174 new NameValuePair("param-two","param-value2"), 175 new NameValuePair("special-chars",":/?~.") 176 }); 177 178 try { 179 client.executeMethod(method); 180 } catch (Throwable t) { 181 t.printStackTrace(); 182 fail("Unable to execute method : " + t.toString()); 183 } 184 assertEquals(200,method.getStatusCode()); 185 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 186 assertTrue(method.getResponseBodyAsString().indexOf("name=\"special-chars\";value=\":/?~.\"") >= 0); 187 assertTrue(method.getResponseBodyAsString().indexOf("name=\"param-one\";value=\"param-value\"") >= 0); 188 assertTrue(method.getResponseBodyAsString().indexOf("name=\"param-two\";value=\"param-value2\"") >= 0); 189 } 190 191 /*** 192 * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)} 193 * works with a parameter name but no value. 194 */ 195 public void testGetMethodParameterWithoutValue() throws Exception { 196 HttpClient client = createHttpClient(); 197 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 198 method.setQueryString(new NameValuePair[] { new NameValuePair("param-without-value",null) }); 199 200 try { 201 client.executeMethod(method); 202 } catch (Throwable t) { 203 t.printStackTrace(); 204 fail("Unable to execute method : " + t.toString()); 205 } 206 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 207 assertEquals(200,method.getStatusCode()); 208 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"param-without-value=\"</p>") >= 0); 209 } 210 211 /*** 212 * Test that {@link GetMethod#addParameter(java.lang.String,java.lang.String)} 213 * works with a parameter name that occurs more than once. 214 */ 215 public void testGetMethodParameterAppearsTwice() throws Exception { 216 HttpClient client = createHttpClient(); 217 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 218 method.setQueryString(new NameValuePair[] { 219 new NameValuePair("foo","one"), 220 new NameValuePair("foo","two") 221 }); 222 223 try { 224 client.executeMethod(method); 225 } catch (Throwable t) { 226 t.printStackTrace(); 227 fail("Unable to execute method : " + t.toString()); 228 } 229 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 230 assertEquals(200,method.getStatusCode()); 231 assertTrue(method.getResponseBodyAsString().indexOf("name=\"foo\";value=\"one\"") >= 0); 232 assertTrue(method.getResponseBodyAsString().indexOf("name=\"foo\";value=\"two\"") >= 0); 233 } 234 235 public void testGetMethodOverwriteQueryString() throws Exception { 236 HttpClient client = createHttpClient(); 237 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 238 method.setQueryString("query=string"); 239 method.setQueryString(new NameValuePair[] { 240 new NameValuePair("param","eter"), 241 new NameValuePair("para","meter") 242 }); 243 244 try { 245 client.executeMethod(method); 246 } catch (Throwable t) { 247 t.printStackTrace(); 248 fail("Unable to execute method : " + t.toString()); 249 } 250 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 251 assertEquals(200,method.getStatusCode()); 252 assertTrue(method.getResponseBodyAsString().indexOf("name=\"query\";value=\"string\"") == -1); 253 assertTrue(method.getResponseBodyAsString().indexOf("name=\"param\";value=\"eter\"") >= 0); 254 assertTrue(method.getResponseBodyAsString().indexOf("name=\"para\";value=\"meter\"") >= 0); 255 } 256 257 /*** 258 * Test that {@link PostMethod#addParameter(java.lang.String,java.lang.String)} 259 * and {@link PostMethod#setQueryString(java.lang.String)} combine 260 * properly. 261 */ 262 public void testPostMethodParameterAndQueryString() throws Exception { 263 HttpClient client = createHttpClient(); 264 PostMethod method = new PostMethod("/" + getWebappContext() + "/params"); 265 method.setQueryString("query=string"); 266 method.setRequestBody(new NameValuePair[] { 267 new NameValuePair("param","eter"), 268 new NameValuePair("para","meter") } ); 269 270 try { 271 client.executeMethod(method); 272 } catch (Throwable t) { 273 t.printStackTrace(); 274 fail("Unable to execute method : " + t.toString()); 275 } 276 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0); 277 assertEquals(200,method.getStatusCode()); 278 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"query=string\"</p>") >= 0); 279 assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("name=\"param\";value=\"eter\"") >= 0); 280 assertTrue(method.getResponseBodyAsString().indexOf("name=\"para\";value=\"meter\"") >= 0); 281 } 282 } 283

This page was automatically generated by Maven