1 30 31 package org.apache.commons.httpclient; 32 33 import junit.framework.*; 34 import org.apache.commons.httpclient.methods.*; 35 36 55 public class TestWebappParameters extends TestWebappBase { 56 57 public TestWebappParameters(String testName) { 58 super(testName); 59 } 60 61 public static Test suite() { 62 TestSuite suite = new TestSuite(TestWebappParameters.class); 63 return suite; 64 } 65 66 public static void main(String args[]) { 67 String [] testCaseName = { TestWebappParameters.class.getName() }; 68 junit.textui.TestRunner.main(testCaseName); 69 } 70 71 73 77 public void testGetMethodQueryString() throws Exception { 78 HttpClient client = createHttpClient(); 79 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 80 method.setQueryString("?hadQuestionMark=true"); 81 82 try { 83 client.executeMethod(method); 84 } catch (Throwable t) { 85 t.printStackTrace(); 86 fail("Unable to execute method : " + t.toString()); 87 } 88 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 89 assertEquals(200,method.getStatusCode()); 90 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"hadQuestionMark=true\"</p>") >= 0); 91 } 92 93 97 public void testGetMethodQueryString2() throws Exception { 98 HttpClient client = createHttpClient(); 99 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 100 method.setQueryString("hadQuestionMark=false"); 101 102 try { 103 client.executeMethod(method); 104 } catch (Throwable t) { 105 t.printStackTrace(); 106 fail("Unable to execute method : " + t.toString()); 107 } 108 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 109 assertEquals(200,method.getStatusCode()); 110 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"hadQuestionMark=false\"</p>") >= 0); 111 } 112 113 117 public void testGetMethodParameters() throws Exception { 118 HttpClient client = createHttpClient(); 119 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 120 method.setQueryString(new NameValuePair[] { new NameValuePair("param-one","param-value") }); 121 122 try { 123 client.executeMethod(method); 124 } catch (Throwable t) { 125 t.printStackTrace(); 126 fail("Unable to execute method : " + t.toString()); 127 } 128 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 129 assertEquals(200,method.getStatusCode()); 130 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"param-one=param-value\"</p>") >= 0); 131 } 132 133 137 public void testGetMethodMultiParameters() throws Exception { 138 HttpClient client = createHttpClient(); 139 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 140 method.setQueryString(new NameValuePair[] { 141 new NameValuePair("param-one","param-value"), 142 new NameValuePair("param-two","param-value2"), 143 new NameValuePair("special-chars",":/?~.") 144 }); 145 146 try { 147 client.executeMethod(method); 148 } catch (Throwable t) { 149 t.printStackTrace(); 150 fail("Unable to execute method : " + t.toString()); 151 } 152 assertEquals(200,method.getStatusCode()); 153 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 154 assertTrue(method.getResponseBodyAsString().indexOf("name=\"special-chars\";value=\":/?~.\"") >= 0); 155 assertTrue(method.getResponseBodyAsString().indexOf("name=\"param-one\";value=\"param-value\"") >= 0); 156 assertTrue(method.getResponseBodyAsString().indexOf("name=\"param-two\";value=\"param-value2\"") >= 0); 157 } 158 159 163 public void testGetMethodParameterWithoutValue() throws Exception { 164 HttpClient client = createHttpClient(); 165 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 166 method.setQueryString(new NameValuePair[] { new NameValuePair("param-without-value",null) }); 167 168 try { 169 client.executeMethod(method); 170 } catch (Throwable t) { 171 t.printStackTrace(); 172 fail("Unable to execute method : " + t.toString()); 173 } 174 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 175 assertEquals(200,method.getStatusCode()); 176 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"param-without-value=\"</p>") >= 0); 177 } 178 179 183 public void testGetMethodParameterAppearsTwice() throws Exception { 184 HttpClient client = createHttpClient(); 185 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 186 method.setQueryString(new NameValuePair[] { 187 new NameValuePair("foo","one"), 188 new NameValuePair("foo","two") 189 }); 190 191 try { 192 client.executeMethod(method); 193 } catch (Throwable t) { 194 t.printStackTrace(); 195 fail("Unable to execute method : " + t.toString()); 196 } 197 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 198 assertEquals(200,method.getStatusCode()); 199 assertTrue(method.getResponseBodyAsString().indexOf("name=\"foo\";value=\"one\"") >= 0); 200 assertTrue(method.getResponseBodyAsString().indexOf("name=\"foo\";value=\"two\"") >= 0); 201 } 202 203 public void testGetMethodOverwriteQueryString() throws Exception { 204 HttpClient client = createHttpClient(); 205 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 206 method.setQueryString("query=string"); 207 method.setQueryString(new NameValuePair[] { 208 new NameValuePair("param","eter"), 209 new NameValuePair("para","meter") 210 }); 211 212 try { 213 client.executeMethod(method); 214 } catch (Throwable t) { 215 t.printStackTrace(); 216 fail("Unable to execute method : " + t.toString()); 217 } 218 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 219 assertEquals(200,method.getStatusCode()); 220 assertTrue(method.getResponseBodyAsString().indexOf("name=\"query\";value=\"string\"") == -1); 221 assertTrue(method.getResponseBodyAsString().indexOf("name=\"param\";value=\"eter\"") >= 0); 222 assertTrue(method.getResponseBodyAsString().indexOf("name=\"para\";value=\"meter\"") >= 0); 223 } 224 225 230 public void testPostMethodParameterAndQueryString() throws Exception { 231 HttpClient client = createHttpClient(); 232 PostMethod method = new PostMethod("/" + getWebappContext() + "/params"); 233 method.setQueryString("query=string"); 234 method.setRequestBody(new NameValuePair[] { 235 new NameValuePair("param","eter"), 236 new NameValuePair("para","meter") } ); 237 238 try { 239 client.executeMethod(method); 240 } catch (Throwable t) { 241 t.printStackTrace(); 242 fail("Unable to execute method : " + t.toString()); 243 } 244 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0); 245 assertEquals(200,method.getStatusCode()); 246 assertTrue(method.getResponseBodyAsString().indexOf("<p>QueryString=\"query=string\"</p>") >= 0); 247 assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("name=\"param\";value=\"eter\"") >= 0); 248 assertTrue(method.getResponseBodyAsString().indexOf("name=\"para\";value=\"meter\"") >= 0); 249 } 250 } 251 252 | Popular Tags |