use of com.meterware.httpunit.WebResponse in project camel by apache.
the class RestServletPostJsonPojoListTest method testPostJaxbPojo.
@Test
public void testPostJaxbPojo() throws Exception {
MockEndpoint mock = getMockEndpoint("mock:input");
mock.expectedMessageCount(1);
String body = "[ {\"id\": 123, \"name\": \"Donald Duck\"}, {\"id\": 456, \"name\": \"John Doe\"} ]";
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());
assertEquals("application/json", response.getContentType());
assertMockEndpointsSatisfied();
List list = mock.getReceivedExchanges().get(0).getIn().getBody(List.class);
assertNotNull(list);
assertEquals(2, list.size());
UserPojo user = (UserPojo) list.get(0);
assertEquals(123, user.getId());
assertEquals("Donald Duck", user.getName());
user = (UserPojo) list.get(1);
assertEquals(456, user.getId());
assertEquals("John Doe", user.getName());
}
use of com.meterware.httpunit.WebResponse 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());
}
use of com.meterware.httpunit.WebResponse 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();
}
use of com.meterware.httpunit.WebResponse 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());
}
}
use of com.meterware.httpunit.WebResponse 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());
}
}
Aggregations