1 30 31 package org.apache.commons.httpclient; 32 33 import java.io.ByteArrayInputStream ; 34 import java.io.InputStream ; 35 36 import junit.framework.*; 37 import org.apache.commons.httpclient.methods.*; 38 39 59 public class TestWebappMethods extends TestWebappBase { 60 61 public TestWebappMethods(String testName) { 62 super(testName); 63 } 64 65 public static Test suite() { 66 TestSuite suite = new TestSuite(TestWebappMethods.class); 67 return suite; 68 } 69 70 public static void main(String args[]) { 71 String [] testCaseName = { TestWebappMethods.class.getName() }; 72 junit.textui.TestRunner.main(testCaseName); 73 } 74 75 77 80 public void testGetMethod() throws Exception { 81 HttpClient client = createHttpClient(); 82 GetMethod method = new GetMethod("/" + getWebappContext() + "/params"); 83 84 try { 85 client.executeMethod(method); 86 } catch (Throwable t) { 87 t.printStackTrace(); 88 fail("Unable to execute method : " + t.toString()); 89 } 90 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 91 assertEquals(200,method.getStatusCode()); 92 93 method.recycle(); 94 95 method.setPath("/" + getWebappContext() + "/params"); 96 try { 97 client.executeMethod(method); 98 } catch (Throwable t) { 99 t.printStackTrace(); 100 fail("Unable to execute method : " + t.toString()); 101 } 102 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: GET</title>") >= 0); 103 assertEquals(200,method.getStatusCode()); 104 } 105 106 109 public void testPostMethod() throws Exception { 110 HttpClient client = createHttpClient(); 111 PostMethod method = new PostMethod("/" + getWebappContext() + "/params"); 112 113 try { 114 client.executeMethod(method); 115 } catch (Throwable t) { 116 t.printStackTrace(); 117 fail("Unable to execute method : " + t.toString()); 118 } 119 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0); 120 assertEquals(200,method.getStatusCode()); 121 122 method.recycle(); 123 124 method.setPath("/" + getWebappContext() + "/params"); 125 try { 126 client.executeMethod(method); 127 } catch (Throwable t) { 128 t.printStackTrace(); 129 fail("Unable to execute method : " + t.toString()); 130 } 131 assertTrue(method.getResponseBodyAsString().indexOf("<title>Param Servlet: POST</title>") >= 0); 132 assertEquals(200,method.getStatusCode()); 133 } 134 135 138 public void testHeadMethod() throws Exception { 139 HttpClient client = createHttpClient(); 140 HeadMethod method = new HeadMethod("/" + getWebappContext() + "/params"); 141 try { 142 client.executeMethod(method); 143 } catch (Throwable t) { 144 t.printStackTrace(); 145 fail("Unable to execute method : " + t.toString()); 146 } 147 assertEquals(200,method.getStatusCode()); 148 149 method.recycle(); 150 151 method.setPath("/" + getWebappContext() + "/params"); 152 try { 153 client.executeMethod(method); 154 } catch (Throwable t) { 155 t.printStackTrace(); 156 fail("Unable to execute method : " + t.toString()); 157 } 158 assertEquals(200,method.getStatusCode()); 159 } 160 161 164 public void testOptionsMethod() throws Exception { 165 HttpClient client = createHttpClient(); 166 OptionsMethod method = new OptionsMethod("/" + getWebappContext() + "/params"); 167 try { 168 client.executeMethod(method); 169 } catch (Throwable t) { 170 t.printStackTrace(); 171 fail("Unable to execute method : " + t.toString()); 172 } 173 assertEquals(200,method.getStatusCode()); 174 assertTrue(method.getAllowedMethods().hasMoreElements()); 175 176 method.recycle(); 177 178 method.setPath("/" + getWebappContext() + "/params"); 179 try { 180 client.executeMethod(method); 181 } catch (Throwable t) { 182 t.printStackTrace(); 183 fail("Unable to execute method : " + t.toString()); 184 } 185 assertEquals(200,method.getStatusCode()); 186 assertTrue(method.getAllowedMethods().hasMoreElements()); 187 } 188 189 192 public void testOptionsStar() throws Exception { 193 HttpClient client = createHttpClient(); 194 OptionsMethod method = new OptionsMethod("*"); 195 try { 196 client.executeMethod(method); 197 } catch (Throwable t) { 198 t.printStackTrace(); 199 fail("Unable to execute method : " + t.toString()); 200 } 201 assertEquals(200,method.getStatusCode()); 202 assertTrue(method.getAllowedMethods().hasMoreElements()); 203 } 204 205 208 public void testDeleteMethod() throws Exception { 209 HttpClient client = createHttpClient(); 210 DeleteMethod method = new DeleteMethod("/" + getWebappContext() + "/params"); 211 try { 212 client.executeMethod(method); 213 } catch (Throwable t) { 214 t.printStackTrace(); 215 fail("Unable to execute method : " + t.toString()); 216 } 217 assertEquals(200,method.getStatusCode()); 218 219 method.recycle(); 220 221 method.setPath("/" + getWebappContext() + "/params"); 222 try { 223 client.executeMethod(method); 224 } catch (Throwable t) { 225 t.printStackTrace(); 226 fail("Unable to execute method : " + t.toString()); 227 } 228 assertEquals(200,method.getStatusCode()); 229 } 230 231 234 public void testPutMethod() throws Exception { 235 HttpClient client = createHttpClient(); 236 PutMethod method = new PutMethod("/" + getWebappContext() + "/params"); 237 try { 238 client.executeMethod(method); 239 } catch (Throwable t) { 240 t.printStackTrace(); 241 fail("Unable to execute method : " + t.toString()); 242 } 243 assertEquals(200,method.getStatusCode()); 244 assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<title>Param Servlet: PUT</title>") >= 0); 245 246 method.recycle(); 247 248 method.setPath("/" + getWebappContext() + "/params"); 249 try { 250 client.executeMethod(method); 251 } catch (Throwable t) { 252 t.printStackTrace(); 253 fail("Unable to execute method : " + t.toString()); 254 } 255 assertEquals(200,method.getStatusCode()); 256 assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<title>Param Servlet: PUT</title>") >= 0); 257 } 258 259 public void testPostBodyNVP() throws Exception { 260 HttpClient client = createHttpClient(); 261 PostMethod method = new PostMethod("/" + getWebappContext() + "/body"); 262 263 method.setRequestBody(new NameValuePair[] { 264 new NameValuePair("quote","It was the best of times, it was the worst of times.") } ); 265 try { 266 client.executeMethod(method); 267 } catch (Throwable t) { 268 t.printStackTrace(); 269 fail("Unable to execute method : " + t.toString()); 270 } 271 assertEquals(200,method.getStatusCode()); 272 assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0); 273 } 274 275 public void testPostBody() throws Exception { 276 HttpClient client = createHttpClient(); 277 PostMethod method = new PostMethod("/" + getWebappContext() + "/body"); 278 279 method.setRequestBody("quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times."); 280 try { 281 client.executeMethod(method); 282 } catch (Throwable t) { 283 t.printStackTrace(); 284 fail("Unable to execute method : " + t.toString()); 285 } 286 assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0); 287 assertEquals(200,method.getStatusCode()); 288 } 289 290 291 public void testPostBodyCustomLength() throws Exception { 292 HttpClient client = createHttpClient(); 293 PostMethod method = new PostMethod("/" + getWebappContext() + "/body"); 294 295 String bodyStr = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times."; 296 byte[] body = HttpConstants.getContentBytes(bodyStr); 297 298 method.setRequestBody(new ByteArrayInputStream (body)); 299 method.setRequestContentLength(body.length); 300 try { 301 client.executeMethod(method); 302 } catch (Throwable t) { 303 t.printStackTrace(); 304 fail("Unable to execute method : " + t.toString()); 305 } 306 assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0); 307 assertEquals(200,method.getStatusCode()); 308 } 309 310 311 public void testPostBodyAutoLength() throws Exception { 312 HttpClient client = createHttpClient(); 313 PostMethod method = new PostMethod("/" + getWebappContext() + "/body"); 314 315 String body = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times."; 316 method.setRequestBody(body); 317 method.setRequestContentLength(PostMethod.CONTENT_LENGTH_AUTO); 318 try { 319 client.executeMethod(method); 320 } catch (Throwable t) { 321 t.printStackTrace(); 322 fail("Unable to execute method : " + t.toString()); 323 } 324 assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0); 325 assertEquals(200,method.getStatusCode()); 326 } 327 328 329 public void testPostBodyChunked() { 330 HttpClient client = createHttpClient(); 331 PostMethod method = new PostMethod("/" + getWebappContext() + "/body"); 332 333 String body = "quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times."; 334 method.setRequestBody(body); 335 method.setRequestContentLength(PostMethod.CONTENT_LENGTH_CHUNKED); 336 try { 337 client.executeMethod(method); 338 } catch (Throwable t) { 339 t.printStackTrace(); 340 fail("Unable to execute method : " + t.toString()); 341 } 342 assertTrue(method.getResponseBodyAsString().indexOf("<tt>quote=It+was+the+best+of+times%2C+it+was+the+worst+of+times.</tt>") >= 0); 343 assertEquals(200,method.getStatusLine().getStatusCode()); 344 } 345 346 347 public void testPutBody() throws Exception { 348 HttpClient client = createHttpClient(); 349 PutMethod method = new PutMethod("/" + getWebappContext() + "/body"); 350 method.setRequestBody("This is data to be sent in the body of an HTTP PUT."); 351 try { 352 client.executeMethod(method); 353 } catch (Throwable t) { 354 t.printStackTrace(); 355 fail("Unable to execute method : " + t.toString()); 356 } 357 assertTrue(method.getResponseBodyAsString(),method.getResponseBodyAsString().indexOf("<tt>This is data to be sent in the body of an HTTP PUT.</tt>") >= 0); 358 assertEquals(200,method.getStatusCode()); 359 } 360 361 362 public void testPostMethodRecycle() { 363 HttpClient client = createHttpClient(); 364 PostMethod method = new PostMethod("/" + getWebappContext() + "/body"); 365 366 String bodyStr = "Like, hello, and stuff"; 367 byte [] body = HttpConstants.getContentBytes(bodyStr); 368 method.setRequestHeader("Content-Type", "text/plain"); 369 method.setRequestBody(new ByteArrayInputStream (body)); 370 method.setRequestContentLength(body.length); 371 try { 372 client.executeMethod(method); 373 } catch (Throwable t) { 374 t.printStackTrace(); 375 fail("Unable to execute method : " + t.toString()); 376 } 377 assertEquals(200,method.getStatusLine().getStatusCode()); 378 String response = method.getResponseBodyAsString(); 379 380 method.recycle(); 381 382 method.setPath("/" + getWebappContext() + "/body"); 383 method.setRequestHeader("Content-Type", "text/plain"); 384 method.setRequestBody(new ByteArrayInputStream (body)); 385 method.setRequestContentLength(body.length); 386 try { 387 client.executeMethod(method); 388 } catch (Throwable t) { 389 t.printStackTrace(); 390 fail("Unable to execute method : " + t.toString()); 391 } 392 assertEquals(200,method.getStatusLine().getStatusCode()); 393 response = method.getResponseBodyAsString(); 394 } 395 396 public void testEmptyPostMethod() throws Exception { 397 HttpClient client = createHttpClient(); 398 PostMethod method = new PostMethod("/" + getWebappContext() + "/body"); 399 400 method.setRequestHeader("Content-Type", "text/plain"); 401 client.executeMethod(method); 402 assertEquals(200,method.getStatusLine().getStatusCode()); 403 String response = method.getResponseBodyAsString(); 404 assertTrue(response.indexOf("No body submitted") >= 0); 405 406 method.recycle(); 407 408 method.setPath("/" + getWebappContext() + "/body"); 409 method.setRequestHeader("Content-Type", "text/plain"); 410 method.setRequestBody((String )null); 411 client.executeMethod(method); 412 assertEquals(200,method.getStatusLine().getStatusCode()); 413 response = method.getResponseBodyAsString(); 414 assertTrue(response.indexOf("No body submitted") >= 0); 415 416 method.recycle(); 417 418 method.setPath("/" + getWebappContext() + "/body"); 419 method.setRequestHeader("Content-Type", "text/plain"); 420 method.setRequestBody((InputStream )null); 421 client.executeMethod(method); 422 assertEquals(200,method.getStatusLine().getStatusCode()); 423 response = method.getResponseBodyAsString(); 424 assertTrue(response.indexOf("No body submitted") >= 0); 425 426 method.recycle(); 427 428 method.setPath("/" + getWebappContext() + "/body"); 429 method.setRequestHeader("Content-Type", "text/plain"); 430 method.setRequestBody(""); 431 client.executeMethod(method); 432 assertEquals(200,method.getStatusLine().getStatusCode()); 433 response = method.getResponseBodyAsString(); 434 assertTrue(response.indexOf("No body submitted") >= 0); 435 436 method.recycle(); 437 438 method.setPath("/" + getWebappContext() + "/body"); 439 method.setRequestHeader("Content-Type", "text/plain"); 440 method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED); 441 client.executeMethod(method); 442 assertEquals(200,method.getStatusLine().getStatusCode()); 443 response = method.getResponseBodyAsString(); 444 assertTrue(response.indexOf("No body submitted") >= 0); 445 446 method.recycle(); 447 448 method.setPath("/" + getWebappContext() + "/body"); 449 method.setRequestHeader("Content-Type", "text/plain"); 450 method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED); 451 method.setRequestBody((String )null); 452 client.executeMethod(method); 453 assertEquals(200,method.getStatusLine().getStatusCode()); 454 response = method.getResponseBodyAsString(); 455 assertTrue(response.indexOf("No body submitted") >= 0); 456 457 method.recycle(); 458 459 method.setPath("/" + getWebappContext() + "/body"); 460 method.setRequestHeader("Content-Type", "text/plain"); 461 method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED); 462 method.setRequestBody((InputStream )null); 463 client.executeMethod(method); 464 assertEquals(200,method.getStatusLine().getStatusCode()); 465 response = method.getResponseBodyAsString(); 466 assertTrue(response.indexOf("No body submitted") >= 0); 467 468 method.recycle(); 469 470 method.setPath("/" + getWebappContext() + "/body"); 471 method.setRequestHeader("Content-Type", "text/plain"); 472 method.setRequestContentLength(EntityEnclosingMethod.CONTENT_LENGTH_CHUNKED); 473 method.setRequestBody(""); 474 client.executeMethod(method); 475 assertEquals(200,method.getStatusLine().getStatusCode()); 476 response = method.getResponseBodyAsString(); 477 478 } 479 480 } 481 | Popular Tags |