Search in sources :

Example 21 with ServletUnitClient

use of com.meterware.servletunit.ServletUnitClient in project camel by apache.

the class RestServletVerbTest method testPut.

@Test
public void testPut() throws Exception {
    final String body = "{ \"id\":\"1\", \"name\":\"Scott\" }";
    MockEndpoint mock = getMockEndpoint("mock:update");
    mock.expectedBodiesReceived(body);
    mock.expectedHeaderReceived("id", "1");
    mock.expectedHeaderReceived(Exchange.HTTP_METHOD, "PUT");
    WebRequest req = new PutMethodWebRequest(CONTEXT_URL + "/services/users/1", new ByteArrayInputStream(body.getBytes()), "application/json");
    ServletUnitClient client = newClient();
    client.setExceptionsThrownOnErrorStatus(false);
    WebResponse response = client.getResponse(req);
    assertEquals(200, response.getResponseCode());
    assertMockEndpointsSatisfied();
}
Also used : PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) 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) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) Test(org.junit.Test)

Example 22 with ServletUnitClient

use of com.meterware.servletunit.ServletUnitClient in project v7files by thiloplanz.

the class BucketsServletTest method testEchoPutGET.

public void testEchoPutGET() throws IOException, SAXException {
    ServletUnitClient sc = sr.newClient();
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", "1234");
        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 GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", "1234");
        try {
            sc.getResponse(request);
            fail("bucket not found => 404");
        } catch (HttpNotFoundException e) {
            assertEquals("Bucket '1' does not have a file matching digest '1234'", e.getResponseMessage());
        }
    }
    MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
    ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        request.setParameter("filename", "a.txt");
        WebResponse response = sc.getResponse(request);
        assertEquals("test", response.getText());
        assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
        assertEquals(4, response.getContentLength());
        assertEquals("attachment; filename=\"a.txt\"", response.getHeaderField("Content-Disposition"));
    }
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        request.setHeaderField("If-None-Match", sha.getDigest());
        WebResponse response = sc.getResponse(request);
        assertEquals(HttpServletResponse.SC_NOT_MODIFIED, response.getResponseCode());
    }
}
Also used : HttpNotFoundException(com.meterware.httpunit.HttpNotFoundException) MongoContentStorage(v7db.files.mongodb.MongoContentStorage) 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) ContentSHA(v7db.files.spi.ContentSHA) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest)

Example 23 with ServletUnitClient

use of com.meterware.servletunit.ServletUnitClient 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 24 with ServletUnitClient

use of com.meterware.servletunit.ServletUnitClient in project v7files by thiloplanz.

the class MiltonServletTest method testGET.

public void testGET() throws IOException, SAXException {
    ServletUnitClient sc = sr.newClient();
    sc.setAuthentication("V7Files", "admin", "admin");
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/a.txt");
        try {
            sc.getResponse(request);
            fail("file not found => 404");
        } catch (HttpNotFoundException e) {
        }
    }
    prepareMockData("test.v7files.files", new BasicBSONObject("_id", new ObjectId()).append("filename", "a.txt").append("parent", "webdav").append("created_at", new Date()).append("in", "abcd".getBytes()));
    assertGET(sc, "http://test/myServlet/a.txt", "abcd");
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/a.txt");
        WebResponse resp = sc.getResponse(request);
        String eTag = resp.getHeaderField("Etag");
        request.setHeaderField("If-None-Match", eTag);
        resp = sc.getResponse(request);
        assertEquals(HttpServletResponse.SC_NOT_MODIFIED, resp.getResponseCode());
        assertEquals("", resp.getText());
    }
}
Also used : BasicBSONObject(org.bson.BasicBSONObject) 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) ObjectId(org.bson.types.ObjectId) ServletUnitClient(com.meterware.servletunit.ServletUnitClient) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) Date(java.util.Date)

Example 25 with ServletUnitClient

use of com.meterware.servletunit.ServletUnitClient in project v7files by thiloplanz.

the class MiltonServletTest method testSimpleScenario.

public void testSimpleScenario() throws IOException, SAXException {
    ServletUnitClient sc = sr.newClient();
    sc.setAuthentication("V7Files", "admin", "admin");
    {
        WebRequest request = new MkColWebRequest("http://test/myServlet/1");
        WebResponse resp = sc.getResponse(request);
        assertEquals(HttpServletResponse.SC_CREATED, resp.getResponseCode());
    }
    {
        WebRequest request = new PutMethodWebRequest("http://test/myServlet/1/test.txt", new ByteArrayInputStream("testPUT".getBytes()), "text/plain");
        WebResponse resp = sc.getResponse(request);
        assertEquals(HttpServletResponse.SC_CREATED, resp.getResponseCode());
    }
    assertExists(sc, "http://test/myServlet/1/test.txt");
    {
        WebRequest request = new MoveWebRequest("http://test/myServlet/1", "/myServlet/2");
        WebResponse resp = sc.getResponse(request);
        assertEquals(HttpServletResponse.SC_CREATED, resp.getResponseCode());
    }
}
Also used : PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) WebResponse(com.meterware.httpunit.WebResponse) WebRequest(com.meterware.httpunit.WebRequest) GetMethodWebRequest(com.meterware.httpunit.GetMethodWebRequest) PutMethodWebRequest(com.meterware.httpunit.PutMethodWebRequest) ByteArrayInputStream(java.io.ByteArrayInputStream) ServletUnitClient(com.meterware.servletunit.ServletUnitClient)

Aggregations

ServletUnitClient (com.meterware.servletunit.ServletUnitClient)42 WebRequest (com.meterware.httpunit.WebRequest)41 WebResponse (com.meterware.httpunit.WebResponse)41 Test (org.junit.Test)33 PostMethodWebRequest (com.meterware.httpunit.PostMethodWebRequest)30 GetMethodWebRequest (com.meterware.httpunit.GetMethodWebRequest)27 ByteArrayInputStream (java.io.ByteArrayInputStream)27 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)16 PutMethodWebRequest (com.meterware.httpunit.PutMethodWebRequest)12 HeaderOnlyWebRequest (com.meterware.httpunit.HeaderOnlyWebRequest)5 HttpNotFoundException (com.meterware.httpunit.HttpNotFoundException)5 HttpException (com.meterware.httpunit.HttpException)3 BasicBSONObject (org.bson.BasicBSONObject)3 ObjectId (org.bson.types.ObjectId)2 MongoContentStorage (v7db.files.mongodb.MongoContentStorage)2 ContentSHA (v7db.files.spi.ContentSHA)2 Date (java.util.Date)1 List (java.util.List)1 FailedToCreateProducerException (org.apache.camel.FailedToCreateProducerException)1 FailedToCreateRouteException (org.apache.camel.FailedToCreateRouteException)1