Search in sources :

Example 1 with HttpException

use of com.meterware.httpunit.HttpException 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 2 with HttpException

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

the class HttpClientRouteTest method testHttpRestricMethod.

@Test
public void testHttpRestricMethod() throws Exception {
    WebRequest req = new PostMethodWebRequest(CONTEXT_URL + "/services/testHttpMethodRestrict", new ByteArrayInputStream(POST_DATA.getBytes()), "text/xml; charset=UTF-8");
    ServletUnitClient client = newClient();
    WebResponse response = client.getResponse(req);
    assertEquals("The response message is wrong ", "OK", response.getResponseMessage());
    assertEquals("The response body is wrong", POST_DATA, response.getText());
    // Send other web method request
    req = new GetMethodWebRequest(CONTEXT_URL + "/services/testHttpMethodRestrict");
    try {
        response = client.getResponse(req);
        fail("Expect the exception here");
    } catch (Exception ex) {
        HttpException httpException = (HttpException) ex;
        assertEquals("Get a wrong response code", 405, httpException.getResponseCode());
    }
}
Also used : WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) HttpException(com.meterware.httpunit.HttpException) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) FailedToCreateProducerException(org.apache.camel.FailedToCreateProducerException) FailedToCreateRouteException(org.apache.camel.FailedToCreateRouteException) HttpException(com.meterware.httpunit.HttpException) Test(org.junit.Test)

Example 3 with HttpException

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

the class BucketsServletTest method testEchoPutPUT.

public void testEchoPutPUT() throws IOException, SAXException {
    ServletUnitClient sc = sr.newClient();
    {
        WebRequest request = new PutMethodWebRequest("http://test/myServlet/1", new ByteArrayInputStream("testPUT".getBytes()), "text/plain");
        try {
            sc.getResponse(request);
            fail("bucket not found => 404");
        } catch (HttpNotFoundException e) {
            assertEquals("Bucket '1' not found", e.getResponseMessage());
        }
    }
    prepareBucket("1", "EchoPut", null, null);
    {
        WebRequest request = new PutMethodWebRequest("http://test/myServlet/1", new ByteArrayInputStream("testPUT".getBytes()), "text/plain");
        request.setParameter("sha", "1234");
        try {
            sc.getResponse(request);
            fail("uploads not allowed => 405");
        } catch (HttpException e) {
            assertEquals(HttpServletResponse.SC_METHOD_NOT_ALLOWED, e.getResponseCode());
        }
    }
    prepareBucket("2", "EchoPut", null, "EchoPut");
    {
        WebRequest request = new PutMethodWebRequest("http://test/myServlet/2", new ByteArrayInputStream("testPUT".getBytes()), "text/plain");
        WebResponse response = sc.getResponse(request);
        assertEquals(DigestUtils.shaHex("testPUT".getBytes()), response.getText());
        assertMockMongoContainsDocument("test.v7files.content", DigestUtils.sha("testPUT".getBytes()));
        WebRequest get = new GetMethodWebRequest("http://test/myServlet/2");
        get.setParameter("sha", response.getText());
        assertEquals("testPUT", sc.getResponse(get).getText());
    }
}
Also used : PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) HttpNotFoundException(com.meterware.httpunit.HttpNotFoundException) WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) PostMethodWebRequest(com.meterware.httpunit.PostMethodWebRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) HttpException(com.meterware.httpunit.HttpException) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest)

Aggregations

HttpException (com.meterware.httpunit.HttpException)3 PostMethodWebRequest (com.meterware.httpunit.PostMethodWebRequest)3 ServletUnitClient (com.meterware.servletunit.ServletUnitClient)3 GetMethodWebRequest (com.meterware.httpunit.GetMethodWebRequest)2 HttpNotFoundException (com.meterware.httpunit.HttpNotFoundException)2 WebRequest (com.meterware.httpunit.WebRequest)2 WebResponse (com.meterware.httpunit.WebResponse)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 PutMethodWebRequest (com.meterware.httpunit.PutMethodWebRequest)1 FailedToCreateProducerException (org.apache.camel.FailedToCreateProducerException)1 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)1 Test (org.junit.Test)1