Example usage for org.apache.commons.httpclient.methods StringRequestEntity StringRequestEntity

List of usage examples for org.apache.commons.httpclient.methods StringRequestEntity StringRequestEntity

Introduction

In this page you can find the example usage for org.apache.commons.httpclient.methods StringRequestEntity StringRequestEntity.

Prototype

public StringRequestEntity(String paramString1, String paramString2, String paramString3)
  throws UnsupportedEncodingException

Source Link

Usage

From source file:org.apache.wink.itest.EncodingParamTest.java

public void testSingleDecodedFormParam() throws Exception {
    try {/*from  ww w .  j a va2  s. c om*/
        PostMethod httpMethod = new PostMethod();
        httpMethod.setURI(new URI(BASE_URI_DECODE + "/region;appversion=", true));
        // httpMethod.setParameter("location", "The%20Southwest");
        httpMethod.setRequestEntity(new StringRequestEntity(
                "location=%21%20%2A%20%27%20%28%20%29%20%3B%20%3A%20%40%20%26%20%3D%20%2B%20%24%20%2C%20%2F%20%3F%20%25%20%23%20%5B%20%5D",
                "application/x-www-form-urlencoded", "UTF-8"));
        httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(httpMethod);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            String responseBody = httpMethod.getResponseBodyAsString();
            System.out.println(responseBody);
            assertEquals(200, result);
            assertEquals("getShopInRegionDecoded:location=! * ' ( ) ; : @ & = + $ , / ? % # [ ];appversion=",
                    responseBody);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

From source file:org.apache.wink.itest.EncodingParamTest.java

public void testSingleEncodedFormParam() throws Exception {
    try {/*from w  ww. j  ava  2 s  . c  o  m*/
        PostMethod httpMethod = new PostMethod();
        httpMethod.setURI(new URI(BASE_URI_ENCODE + "/region;appversion=1.1%2B", true));
        // httpMethod.setParameter("location", "The%20Southwest");
        httpMethod.setRequestEntity(new StringRequestEntity("location=The%20Southwest",
                "application/x-www-form-urlencoded", "UTF-8"));
        httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(httpMethod);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            String responseBody = httpMethod.getResponseBodyAsString();
            System.out.println(responseBody);
            assertEquals(200, result);
            assertEquals("getShopInRegion:location=The%20Southwest;appversion=1.1%2B", responseBody);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

From source file:org.apache.wink.itest.EncodingParamTest.java

public void testSingleEncodedFormParamMethod() throws Exception {
    try {/*www.  j ava  2  s. c om*/
        PostMethod httpMethod = new PostMethod();
        httpMethod.setURI(new URI(BASE_URI_ENCODE + "/method/region;appversion=1.1%2B", true));
        httpMethod.setRequestEntity(new StringRequestEntity("location=The%20Southwest",
                "application/x-www-form-urlencoded", "UTF-8"));
        httpclient = new HttpClient();

        try {
            int result = httpclient.executeMethod(httpMethod);
            System.out.println("Response status code: " + result);
            System.out.println("Response body: ");
            String responseBody = httpMethod.getResponseBodyAsString();
            System.out.println(responseBody);
            assertEquals(200, result);
            assertEquals("getShopInRegionMethod:location=The%20Southwest;appversion=1.1%2B", responseBody);
        } catch (IOException ioe) {
            ioe.printStackTrace();
            fail(ioe.getMessage());
        } finally {
            httpMethod.releaseConnection();
        }
    } catch (URIException e) {
        e.printStackTrace();
        fail(e.getMessage());
    }
}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsMappedProvidersTest.java

/**
 * Test the positive workflow where a comment with a message and author is
 * successfully posted to the Guestbook.
 * //ww w.j  av  a2 s.  c o m
 * @throws Exception
 */
public void testRegularWorkflow() throws Exception {
    HttpClient client = new HttpClient();

    PostMethod postMethod = new PostMethod(getBaseURI() + "/clear");
    client.executeMethod(postMethod);
    assertEquals(204, postMethod.getStatusCode());

    postMethod = new PostMethod(getBaseURI());
    postMethod.setRequestEntity(new StringRequestEntity(
            "<comment><message>Hello World!</message><author>Anonymous</author></comment>", "text/xml", null));
    client.executeMethod(postMethod);
    assertEquals(201, postMethod.getStatusCode());
    String newPostURILocation = postMethod.getResponseHeader("Location").getValue();

    GetMethod getMethod = new GetMethod(newPostURILocation);
    client.executeMethod(getMethod);
    assertEquals(200, getMethod.getStatusCode());

    Comment c = (Comment) JAXBContext.newInstance(Comment.class.getPackage().getName()).createUnmarshaller()
            .unmarshal(getMethod.getResponseBodyAsStream());
    assertEquals("Anonymous", c.getAuthor());
    assertEquals(1, c.getId().intValue());
    assertEquals("Hello World!", c.getMessage());
}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsMappedProvidersTest.java

/**
 * Tests a method that throws an emptily constructed
 * <code>WebApplicationException</code>.
 * //from   ww w  . j a  v a  2 s  .  co m
 * @throws Exception
 */
public void testWebApplicationExceptionDefaultMappedProvider() throws Exception {
    HttpClient client = new HttpClient();

    PostMethod postMethod = new PostMethod(getBaseURI());
    postMethod.setRequestEntity(new StringRequestEntity("<comment></comment>", "text/xml", null));
    client.executeMethod(postMethod);
    assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), postMethod.getStatusCode());
    assertEquals(getBaseURI(), postMethod.getResponseHeader("ExceptionPage").getValue());
    ServerContainerAssertions.assertExceptionBodyFromServer(500, postMethod.getResponseBodyAsString());
}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsMappedProvidersTest.java

/**
 * Tests a method that throws a <code>WebApplicationException</code> with an
 * integer status code./*from  w  w  w.ja  va2 s  .  c  o  m*/
 * 
 * @throws Exception
 */
public void testWebApplicationExceptionStatusCodeSetMappedProvider() throws Exception {
    HttpClient client = new HttpClient();
    PostMethod postMethod = new PostMethod(getBaseURI());
    postMethod.setRequestEntity(new StringRequestEntity(
            "<comment><message>Suppose to fail with missing author.</message></comment>", "text/xml", null));
    client.executeMethod(postMethod);
    assertEquals(497, postMethod.getStatusCode());
    assertEquals(getBaseURI(), postMethod.getResponseHeader("ExceptionPage").getValue());
    ServerContainerAssertions.assertExceptionBodyFromServer(497, postMethod.getResponseBodyAsString());

}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsMappedProvidersTest.java

/**
 * Tests a method that throws a <code>WebApplicationException</code> with a
 * Response.Status set./* w w  w . j a va  2 s  .c o  m*/
 * 
 * @throws Exception
 */
public void testWebApplicationExceptionResponseStatusSetMappedProvider() throws Exception {
    HttpClient client = new HttpClient();
    PostMethod postMethod = new PostMethod(getBaseURI());
    postMethod.setRequestEntity(new StringRequestEntity("", "text/xml", null));
    client.executeMethod(postMethod);
    assertEquals(496, postMethod.getStatusCode());
    assertEquals(getBaseURI(), postMethod.getResponseHeader("ExceptionPage").getValue());
    ServerContainerAssertions.assertExceptionBodyFromServer(496, postMethod.getResponseBodyAsString());
}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsMappedProvidersTest.java

/**
 * Tests a method that throws a <code>WebApplicationException</code> with a
 * Response with an entity (which will not get mapped via an exception
 * mapper).//from ww  w  .  ja v  a  2s  .  c o  m
 * 
 * @throws Exception
 */
public void testWebApplicationExceptionResponseWithEntitySetMappedProvider() throws Exception {
    HttpClient client = new HttpClient();

    PostMethod postMethod = new PostMethod(getBaseURI());
    postMethod.setRequestEntity(
            new StringRequestEntity("<comment><author>Anonymous</author></comment>", "text/xml", null));
    client.executeMethod(postMethod);
    assertEquals(400, postMethod.getStatusCode());

    CommentError c = (CommentError) JAXBContext.newInstance(CommentError.class.getPackage().getName())
            .createUnmarshaller().unmarshal(postMethod.getResponseBodyAsStream());
    assertEquals("Missing the message in the comment.", c.getErrorMessage());
}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsMappedProvidersTest.java

/**
 * Tests a method that throws a <code>WebApplicationException</code> with a
 * Response with no entity (which will not get mapped via an exception
 * mapper)./* w w  w  .  j a va2  s . c o m*/
 * 
 * @throws Exception
 */
public void testWebApplicationExceptionResponseWithNoEntitySetMappedProvider() throws Exception {
    HttpClient client = new HttpClient();

    PostMethod postMethod = new PostMethod(getBaseURI());
    postMethod.setRequestEntity(new StringRequestEntity(
            "<comment><message>throwemptywebappexception</message><author>Anonymous</author></comment>",
            "text/xml", null));
    client.executeMethod(postMethod);
    assertEquals(491, postMethod.getStatusCode());
    assertEquals("Some message", postMethod.getResponseHeader("throwemptyentitywebappexception").getValue());
    assertEquals(getBaseURI(), postMethod.getResponseHeader("ExceptionPage").getValue());

    CommentError c = (CommentError) JAXBContext.newInstance(CommentError.class.getPackage().getName())
            .createUnmarshaller().unmarshal(postMethod.getResponseBodyAsStream());
    assertEquals("WebApplicationExceptionMapProvider set message", c.getErrorMessage());
}

From source file:org.apache.wink.itest.exceptionmappers.JAXRSExceptionsMappedProvidersTest.java

/**
 * Tests a method that throws a subclass of
 * <code>WebApplicationException</code> with a Response.
 * /*from  www .ja  va 2 s. c  o  m*/
 * @throws Exception
 */
public void testCustomWebApplicationExceptionMappedProvider() throws Exception {
    HttpClient client = new HttpClient();

    PostMethod postMethod = new PostMethod(getBaseURI());
    postMethod.setRequestEntity(new StringRequestEntity(
            "<comment><message></message><author></author></comment>", "text/xml", null));
    client.executeMethod(postMethod);
    assertEquals(498, postMethod.getStatusCode());

    CommentError c = (CommentError) JAXBContext.newInstance(CommentError.class.getPackage().getName())
            .createUnmarshaller().unmarshal(postMethod.getResponseBodyAsStream());
    assertEquals("Cannot post an invalid message.", c.getErrorMessage());
}