Search in sources :

Example 11 with PostMethodWebRequest

use of com.meterware.httpunit.PostMethodWebRequest in project camel by apache.

the class RestServletPostXmlJaxbPojoTest method testPostJaxbPojoNoContentType.

@Test
public void testPostJaxbPojoNoContentType() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:input");
    mock.expectedMessageCount(1);
    mock.message(0).body().isInstanceOf(UserJaxbPojo.class);
    String body = "<user name=\"Donald Duck\" id=\"456\"></user>";
    WebRequest req = new PostMethodWebRequest(CONTEXT_URL + "/services/users/new", new ByteArrayInputStream(body.getBytes()), "foo");
    ServletUnitClient client = newClient();
    client.setExceptionsThrownOnErrorStatus(false);
    WebResponse response = client.getResponse(req);
    assertEquals(200, response.getResponseCode());
    assertMockEndpointsSatisfied();
    UserJaxbPojo user = mock.getReceivedExchanges().get(0).getIn().getBody(UserJaxbPojo.class);
    assertNotNull(user);
    assertEquals(456, user.getId());
    assertEquals("Donald Duck", user.getName());
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ByteArrayInputStream(java.io.ByteArrayInputStream) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) Test(org.junit.Test)

Example 12 with PostMethodWebRequest

use of com.meterware.httpunit.PostMethodWebRequest in project v7files by thiloplanz.

the class BucketsServletTest method testFormPostPOST.

public void testFormPostPOST() throws IOException, SAXException {
    ServletUnitClient sc = sr.newClient();
    {
        PostMethodWebRequest request = new PostMethodWebRequest("http://test/myServlet/1");
        try {
            sc.getResponse(request);
            fail("bucket not found => 404");
        } catch (HttpNotFoundException e) {
            assertEquals("Bucket '1' not found", e.getResponseMessage());
        }
    }
    prepareBucket("1", "FormPost", null, null);
    {
        PostMethodWebRequest request = new PostMethodWebRequest("http://test/myServlet/1");
        try {
            sc.getResponse(request);
            fail("uploads not allowed => 405");
        } catch (HttpException e) {
            assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getResponseCode());
        }
    }
//
// TODO: an actual POST, see
// http://stackoverflow.com/questions/10891247/file-upload-post-request-with-servletunit
}
Also used : HttpNotFoundException(com.meterware.httpunit.HttpNotFoundException) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) HttpException(com.meterware.httpunit.HttpException)

Example 13 with PostMethodWebRequest

use of com.meterware.httpunit.PostMethodWebRequest in project javaee7-samples by javaee-samples.

the class MyResourceTest method testPost.

@Test
public void testPost() throws IOException, SAXException {
    WebConversation conv = new WebConversation();
    conv.setAuthentication("file", "u1", "p1");
    PostMethodWebRequest postRequest = new PostMethodWebRequest(base + "/webresources/myresource");
    try {
        WebResponse response = conv.getResponse(postRequest);
    } catch (HttpException e) {
        assertNotNull(e);
        assertEquals(403, e.getResponseCode());
        return;
    }
    fail("POST is not authorized and can still be called");
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebConversation(com.meterware.httpunit.WebConversation) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) HttpException(com.meterware.httpunit.HttpException) Test(org.junit.Test)

Example 14 with PostMethodWebRequest

use of com.meterware.httpunit.PostMethodWebRequest in project camel by apache.

the class RestServletPostXmlJaxbPojoTest method testPostJaxbPojo.

@Test
public void testPostJaxbPojo() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:input");
    mock.expectedMessageCount(1);
    mock.message(0).body().isInstanceOf(UserJaxbPojo.class);
    String body = "{\"id\": 123, \"name\": \"Donald Duck\"}";
    WebRequest req = new PostMethodWebRequest(CONTEXT_URL + "/services/users/new", new ByteArrayInputStream(body.getBytes()), "application/json");
    ServletUnitClient client = newClient();
    client.setExceptionsThrownOnErrorStatus(false);
    WebResponse response = client.getResponse(req);
    assertEquals(200, response.getResponseCode());
    assertMockEndpointsSatisfied();
    UserJaxbPojo user = mock.getReceivedExchanges().get(0).getIn().getBody(UserJaxbPojo.class);
    assertNotNull(user);
    assertEquals(123, user.getId());
    assertEquals("Donald Duck", user.getName());
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ByteArrayInputStream(java.io.ByteArrayInputStream) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) Test(org.junit.Test)

Example 15 with PostMethodWebRequest

use of com.meterware.httpunit.PostMethodWebRequest in project camel by apache.

the class RestServletVerbTest method testPost.

@Test
public void testPost() throws Exception {
    final String body = "{ \"id\":\"1\", \"name\":\"Scott\" }";
    MockEndpoint mock = getMockEndpoint("mock:create");
    mock.expectedBodiesReceived(body);
    mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "POST");
    WebRequest req = new PostMethodWebRequest(CONTEXT_URL + "/services/users", new ByteArrayInputStream(body.getBytes()), "application/json");
    ServletUnitClient client = newClient();
    client.setExceptionsThrownOnErrorStatus(false);
    WebResponse response = client.getResponse(req);
    assertEquals(200, response.getResponseCode());
    assertMockEndpointsSatisfied();
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) HeaderOnlyWebRequest(com.meterware.httpunit.HeaderOnlyWebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) ByteArrayInputStream(java.io.ByteArrayInputStream) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) Test(org.junit.Test)

Aggregations

PostMethodWebRequest (com.meterware.httpunit.PostMethodWebRequest)23 WebResponse (com.meterware.httpunit.WebResponse)22 ServletUnitClient (com.meterware.servletunit.ServletUnitClient)22 Test (org.junit.Test)22 WebRequest (com.meterware.httpunit.WebRequest)21 ByteArrayInputStream (java.io.ByteArrayInputStream)21 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)14 GetMethodWebRequest (com.meterware.httpunit.GetMethodWebRequest)7 HttpException (com.meterware.httpunit.HttpException)3 HeaderOnlyWebRequest (com.meterware.httpunit.HeaderOnlyWebRequest)1 HttpNotFoundException (com.meterware.httpunit.HttpNotFoundException)1 PutMethodWebRequest (com.meterware.httpunit.PutMethodWebRequest)1 WebConversation (com.meterware.httpunit.WebConversation)1 List (java.util.List)1 FailedToCreateProducerException (org.apache.camel.FailedToCreateProducerException)1 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)1