/** Test that the body can be set as a String without an explict content type */
  public void testStringBodyToBodyServlet() throws Exception {
    PostMethod method = new PostMethod("/");
    String stringBody = "pname1=pvalue1&pname2=pvalue2";

    method.setRequestEntity(new StringRequestEntity(stringBody, null, null));
    this.server.setHttpService(new EchoService());
    try {
      this.client.executeMethod(method);
      assertEquals(200, method.getStatusCode());
      String body = method.getResponseBodyAsString();
      assertEquals("pname1=pvalue1&pname2=pvalue2", body);
    } finally {
      method.releaseConnection();
    }
  }
  /** Test that parameters can be added. */
  public void testAddParametersToParamServlet() throws Exception {
    PostMethod method = new PostMethod("/");

    method.addParameter(new NameValuePair("pname1", "pvalue1"));
    method.addParameter(new NameValuePair("pname2", "pvalue2"));

    this.server.setHttpService(new EchoService());
    try {
      this.client.executeMethod(method);
      assertEquals(200, method.getStatusCode());
      String body = method.getResponseBodyAsString();
      assertEquals("pname1=pvalue1&pname2=pvalue2", body);
    } finally {
      method.releaseConnection();
    }
  }
Ejemplo n.º 3
0
 /**
  * Test that {@link PostMethod#addParameter(java.lang.String,java.lang.String)} and {@link
  * PostMethod#setQueryString(java.lang.String)} combine properly.
  */
 public void testPostMethodParameterAndQueryString() throws Exception {
   this.server.setHttpService(new QueryInfoService());
   PostMethod method = new PostMethod("/");
   method.setQueryString("query=string");
   method.setRequestBody(
       new NameValuePair[] {
         new NameValuePair("param", "eter"), new NameValuePair("para", "meter")
       });
   try {
     this.client.executeMethod(method);
     assertEquals(200, method.getStatusCode());
     String response = method.getResponseBodyAsString();
     assertTrue(response.indexOf("QueryString=\"query=string\"") >= 0);
     assertFalse(response.indexOf("QueryString=\"param=eter&para=meter\"") >= 0);
   } finally {
     method.releaseConnection();
   }
 }