Search in sources :

Example 46 with ServletUnitClient

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

the class RestServletBindingModeJsonWithContractTest method testBindingModeJsonWithContract.

@Test
public void testBindingModeJsonWithContract() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:input");
    mock.expectedMessageCount(1);
    mock.message(0).body().isInstanceOf(UserPojoEx.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());
    String answer = response.getText();
    assertTrue("Unexpected response: " + answer, answer.contains("\"active\":true"));
    assertMockEndpointsSatisfied();
    Object obj = mock.getReceivedExchanges().get(0).getIn().getBody();
    assertEquals(UserPojoEx.class, obj.getClass());
    UserPojoEx user = (UserPojoEx) obj;
    assertNotNull(user);
    assertEquals(123, user.getId());
    assertEquals("Donald Duck", user.getName());
    assertEquals(true, user.isActive());
}
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 47 with ServletUnitClient

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

the class RestServletBindingModeXmlTest method testBindingModeWrong.

@Test
public void testBindingModeWrong() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:input");
    mock.expectedMessageCount(0);
    // we bind to xml, but send in json, which is not possible
    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(500, response.getResponseCode());
    assertMockEndpointsSatisfied();
}
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 48 with ServletUnitClient

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

the class RestServletBindingModeXmlTest method testBindingMode.

@Test
public void testBindingMode() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:input");
    mock.expectedMessageCount(1);
    mock.message(0).body().isInstanceOf(UserJaxbPojo.class);
    String body = "<user name=\"Donald Duck\" id=\"123\"></user>";
    WebRequest req = new PostMethodWebRequest(CONTEXT_URL + "/services/users/new", new ByteArrayInputStream(body.getBytes()), "application/xml");
    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 49 with ServletUnitClient

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

the class BucketsServletTest method testFormPostGET.

public void testFormPostGET() throws IOException, SAXException {
    BasicBSONObject bucket = prepareBucket("1", "FormPost", null, null);
    MongoContentStorage storage = new MongoContentStorage(getMongo().getDB("test"));
    ContentSHA sha = storage.storeContent(new ByteArrayInputStream("test".getBytes()));
    ServletUnitClient sc = sr.newClient();
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        try {
            sc.getResponse(request);
            fail("bucket not found => 404");
        } catch (HttpNotFoundException e) {
            assertEquals(String.format("Bucket '1' does not have a file matching digest '%s'", sha.getDigest()), e.getResponseMessage());
        }
    }
    bucket.append("FormPost", new BasicBSONObject("data", new BasicBSONObject("files", new BasicBSONObject("file", new BasicBSONObject("filename", "a.txt").append("sha", sha.getSHA())))));
    {
        WebRequest request = new GetMethodWebRequest("http://test/myServlet/1");
        request.setParameter("sha", sha.getDigest());
        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.setParameter("filename", "x.txt");
        WebResponse response = sc.getResponse(request);
        assertEquals("test", response.getText());
        assertEquals(sha.getDigest(), response.getHeaderField("ETag"));
        assertEquals(4, response.getContentLength());
        assertEquals("attachment; filename=\"x.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 : BasicBSONObject(org.bson.BasicBSONObject) 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 50 with ServletUnitClient

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

ServletUnitClient (com.meterware.servletunit.ServletUnitClient)58 WebResponse (com.meterware.httpunit.WebResponse)56 WebRequest (com.meterware.httpunit.WebRequest)52 Test (org.junit.Test)47 PostMethodWebRequest (com.meterware.httpunit.PostMethodWebRequest)40 GetMethodWebRequest (com.meterware.httpunit.GetMethodWebRequest)34 ByteArrayInputStream (java.io.ByteArrayInputStream)27 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)16 PutMethodWebRequest (com.meterware.httpunit.PutMethodWebRequest)12 Document (org.w3c.dom.Document)7 HttpNotFoundException (com.meterware.httpunit.HttpNotFoundException)6 HeaderOnlyWebRequest (com.meterware.httpunit.HeaderOnlyWebRequest)5 HttpException (com.meterware.httpunit.HttpException)3 WebLink (com.meterware.httpunit.WebLink)3 HashSet (java.util.HashSet)3 BasicBSONObject (org.bson.BasicBSONObject)3 ObjectId (org.bson.types.ObjectId)2 MongoContentStorage (v7db.files.mongodb.MongoContentStorage)2 ContentSHA (v7db.files.spi.ContentSHA)2 InputStream (java.io.InputStream)1