1 31 32 package org.apache.commons.httpclient; 33 34 import junit.framework.*; 35 import org.apache.commons.httpclient.methods.*; 36 import java.io.*; 37 38 44 public class TestWebappPostMethod extends TestWebappBase { 45 46 HttpClient httpClient; 47 final String paramsPath = "/" + getWebappContext() + "/params"; 48 final String bodyPath = "/" + getWebappContext() + "/body"; 49 50 public TestWebappPostMethod(String testName) { 51 super(testName); 52 } 53 54 public static Test suite() { 55 TestSuite suite = new TestSuite(TestWebappPostMethod.class); 56 return suite; 57 } 58 59 public static void main(String args[]) { 60 String [] testCaseName = { TestWebappPostMethod.class.getName() }; 61 junit.textui.TestRunner.main(testCaseName); 62 } 63 64 public void setUp() { 65 httpClient = createHttpClient(); 66 } 67 68 71 private void verifyBody(PostMethod method) throws Exception { 72 httpClient.executeMethod(method); 73 74 assertEquals(200,method.getStatusCode()); 75 String body = method.getResponseBodyAsString(); 76 assertTrue(body.indexOf("Body Servlet: POST") >= 0); 78 assertTrue(body.indexOf("pname1=pvalue1&pname2=pvalue2") >= 0); 79 } 80 81 82 85 private void verifyParams(PostMethod method) throws Exception { 86 httpClient.executeMethod(method); 87 88 assertEquals(200,method.getStatusCode()); 89 String body = method.getResponseBodyAsString(); 90 assertTrue(body.indexOf("Param Servlet: POST") >= 0); 92 assertTrue(body.indexOf("QueryString=null") >= 0); 93 assertTrue(body.indexOf("name=\"pname1\";value=\"pvalue1\"") >= 0); 94 assertTrue(body.indexOf("name=\"pname2\";value=\"pvalue2\"") >= 0); 95 } 96 97 98 100 103 public void testParametersBodyToParamServlet() throws Exception { 104 PostMethod method = new PostMethod(paramsPath); 105 NameValuePair[] parametersBody = new NameValuePair[] { 106 new NameValuePair("pname1","pvalue1"), 107 new NameValuePair("pname2","pvalue2") 108 }; 109 110 method.setRequestBody(parametersBody); 111 112 verifyParams(method); 113 } 114 115 118 public void testStringBodyToParamServlet() throws Exception { 119 PostMethod method = new PostMethod(paramsPath); 120 String stringBody = "pname1=pvalue1&pname2=pvalue2"; 121 122 method.setRequestBody(stringBody); 123 method.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE); 124 125 verifyParams(method); 126 } 127 128 131 public void testStringBodyToBodyServlet() throws Exception { 132 PostMethod method = new PostMethod(bodyPath); 133 String stringBody = "pname1=pvalue1&pname2=pvalue2"; 134 135 method.setRequestBody(stringBody); 136 137 verifyBody(method); 138 } 139 140 143 public void testStreamBodyToParamServlet() throws Exception { 144 PostMethod method = new PostMethod(paramsPath); 145 InputStream streamBody = 146 new ByteArrayInputStream("pname1=pvalue1&pname2=pvalue2".getBytes()); 147 148 method.setRequestBody(streamBody); 149 method.setRequestHeader("Content-Type", PostMethod.FORM_URL_ENCODED_CONTENT_TYPE); 150 151 verifyParams(method); 152 } 153 154 157 public void testStreamBodyToBodyServlet() throws Exception { 158 PostMethod method = new PostMethod(bodyPath); 159 160 InputStream streamBody = 161 new ByteArrayInputStream("pname1=pvalue1&pname2=pvalue2".getBytes()); 162 method.setRequestBody(streamBody); 163 164 verifyBody(method); 165 } 166 167 170 public void testAddParametersToParamServlet() throws Exception { 171 PostMethod method = new PostMethod(paramsPath); 172 173 method.addParameter(new NameValuePair("pname1","pvalue1")); 174 method.addParameter(new NameValuePair("pname2","pvalue2")); 175 176 verifyParams(method); 177 } 178 179 182 public void testAddRemoveParametersToParamServlet() throws Exception { 183 PostMethod method = new PostMethod(paramsPath); 184 185 method.addParameter(new NameValuePair("pname0","pvalue0")); 186 method.addParameter(new NameValuePair("pname1","pvalue1")); 187 method.addParameter(new NameValuePair("pname2","pvalue2")); 188 method.addParameter(new NameValuePair("pname3","pvalue3")); 189 method.removeParameter("pname0"); 190 method.removeParameter("pname4"); 191 192 verifyParams(method); 193 } 194 195 198 public void testRemoveParameterReturnValue() throws Exception { 199 PostMethod method = new PostMethod(paramsPath); 200 201 method.addParameter("param", "whatever"); 202 assertTrue("Return value of the method is expected to be true", method.removeParameter("param")); 203 assertFalse("Return value of the method is expected to be false", method.removeParameter("param")); 204 } 205 206 209 public void testAddParameterFollowedBySetParameter() throws Exception { 210 PostMethod method = new PostMethod(paramsPath); 211 212 method.addParameter("param", "a"); 213 method.addParameter("param", "b"); 214 method.addParameter("param", "c"); 215 assertEquals("param=a¶m=b¶m=c", method.getRequestBodyAsString()); 216 method.setParameter("param", "a"); 217 assertEquals("param=a", method.getRequestBodyAsString()); 218 } 219 220 } 221 222 | Popular Tags |