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.exceptionmappers.JAXRSExceptionsMappedProvidersTest.java

/**
 * Tests a method that throws a checked exception.
 * /*ww  w .  ja v  a  2 s  . c  o m*/
 * @throws Exception
 */
public void testCheckExceptionMappedProvider() throws Exception {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/-99999");
    putMethod.setRequestEntity(new StringRequestEntity(
            "<comment><id></id><message></message><author></author></comment>", "text/xml", null));
    client.executeMethod(putMethod);
    assertEquals(454, putMethod.getStatusCode());

    CommentError c = (CommentError) JAXBContext.newInstance(CommentError.class.getPackage().getName())
            .createUnmarshaller().unmarshal(putMethod.getResponseBodyAsStream());
    assertEquals("Unexpected ID.", c.getErrorMessage());
}

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

/**
 * Test the positive workflow where a comment with a message and author is
 * successfully posted to the Guestbook.
 * /*from   w w  w. jav  a 2 s . c  o m*/
 * @throws Exception
 */
public void testRegularWorkflow() throws Exception {
    /* FIXME: this is not a repeatable test */
    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.JAXRSExceptionsNoMapperTest.java

/**
 * Tests a method that throws an emptily constructed
 * <code>WebApplicationException</code>.
 * // ww  w  .j a  v a 2 s.  com
 * @throws Exception
 */
public void testWebApplicationExceptionDefaultNoMappingProvider() 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());
    ServerContainerAssertions.assertExceptionBodyFromServer(500, postMethod.getResponseBodyAsString());
}

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

/**
 * Tests a method that throws a <code>WebApplicationException</code> with an
 * integer status code./*from   ww w .jav  a  2s  . c  om*/
 * 
 * @throws Exception
 */
public void testWebApplicationExceptionStatusCodeSetNoMappingProvider() 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(499, postMethod.getStatusCode());
    ServerContainerAssertions.assertExceptionBodyFromServer(499, postMethod.getResponseBodyAsString());

}

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

/**
 * Tests a method that throws a <code>WebApplicationException</code> with a
 * Response.Status set./*from   www.j av a  2  s .  com*/
 * 
 * @throws Exception
 */
public void testWebApplicationExceptionResponseStatusSetNoMappingProvider() throws Exception {
    HttpClient client = new HttpClient();
    PostMethod postMethod = new PostMethod(getBaseURI());
    postMethod.setRequestEntity(new StringRequestEntity("", "text/xml", null));
    client.executeMethod(postMethod);
    assertEquals(Status.BAD_REQUEST.getStatusCode(), postMethod.getStatusCode());
    ServerContainerAssertions.assertExceptionBodyFromServer(400, postMethod.getResponseBodyAsString());

}

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

/**
 * Tests a method that throws a <code>WebApplicationException</code> with a
 * Response./*from www.  j av a  2  s. c  o m*/
 * 
 * @throws Exception
 */
public void testWebApplicationExceptionResponseSetNoMappingProvider() 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(Status.BAD_REQUEST.getStatusCode(), 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.JAXRSExceptionsNoMapperTest.java

/**
 * Tests a method that throws a subclass of
 * <code>WebApplicationException</code> with a Response.
 * // w ww .  j a  v a2 s  .  c om
 * @throws Exception
 */
public void testCustomWebApplicationExceptionNoMappingProvider() 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());
}

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

/**
 * Tests a method that throws a checked exception.
 * //  w w w .ja v a2s . co  m
 * @throws Exception
 */
public void testCheckExceptionNoMappingProvider() throws Exception {
    HttpClient client = new HttpClient();

    PutMethod putMethod = new PutMethod(getBaseURI() + "/-99999");
    putMethod.setRequestEntity(new StringRequestEntity(
            "<comment><id></id><message></message><author></author></comment>", "text/xml", null));
    client.executeMethod(putMethod);
    assertEquals(500, putMethod.getStatusCode());
    // assertLogContainsException("jaxrs.tests.exceptions.nomapping.server.GuestbookException: Unexpected ID.");
}

From source file:org.apache.wink.itest.exceptions.ExceptionsWhileTargettingTest.java

/**
 * Tests that a 415 error is thrown when request entity data sent is not
 * acceptable by the resource./* www .  j a va  2s . c o  m*/
 * 
 * @throws Exception
 */
public void test415WhenResourceMethodDoesNotAcceptRequestEntity() throws Exception {
    PutMethod putMethod = new PutMethod(getBaseURI() + "/targeting/resourcewithmethod");
    try {
        putMethod.setRequestEntity(new StringRequestEntity("some content", "text/plain", "UTF-8"));
        client.executeMethod(putMethod);
        assertEquals(200, putMethod.getStatusCode());
        assertEquals("some content", putMethod.getResponseBodyAsString());
    } finally {
        putMethod.releaseConnection();
    }

    putMethod = new PutMethod(getBaseURI() + "/targeting/resourcewithmethod");
    try {
        putMethod.setRequestEntity(new StringRequestEntity("some content", "customplain/something", "UTF-8"));
        client.executeMethod(putMethod);
        assertEquals(415, putMethod.getStatusCode());
        ServerContainerAssertions.assertExceptionBodyFromServer(415, putMethod.getResponseBodyAsString());

    } finally {
        putMethod.releaseConnection();
    }
}

From source file:org.apache.wink.itest.exceptions.ExceptionsWhileTargettingTest.java

/**
 * Tests that a 406 error is produced if server side cannot produce any
 * acceptable content type./*from  w w  w . j av  a2s . com*/
 * 
 * @throws Exception
 */
public void test406WhenResourceMethodDoesNotProduceResponseEntityType() throws Exception {
    PutMethod putMethod = new PutMethod(getBaseURI() + "/targeting/resourcewithmethod");

    try {
        putMethod.addRequestHeader("Accept", "text/plain");
        putMethod.setRequestEntity(new StringRequestEntity("some content", "text/plain", "UTF-8"));
        client.executeMethod(putMethod);

        assertEquals(200, putMethod.getStatusCode());
        assertEquals("some content", putMethod.getResponseBodyAsString());
    } finally {
        putMethod.releaseConnection();
    }

    putMethod = new PutMethod(getBaseURI() + "/targeting/resourcewithmethod");
    try {
        putMethod.addRequestHeader("Accept", "text/customplain");
        putMethod.setRequestEntity(new StringRequestEntity("some content", "text/plain", "UTF-8"));
        client.executeMethod(putMethod);

        assertEquals(406, putMethod.getStatusCode());
        ServerContainerAssertions.assertExceptionBodyFromServer(406, putMethod.getResponseBodyAsString());
    } finally {
        putMethod.releaseConnection();
    }
}