use of com.meterware.httpunit.WebResponse in project javaee7-samples by javaee-samples.
the class MyResourceTest method testGetSubResourceWithCorrectCredentials.
@Test
public void testGetSubResourceWithCorrectCredentials() throws IOException, SAXException {
WebConversation conv = new WebConversation();
conv.setAuthentication("file", "u1", "p1");
GetMethodWebRequest getRequest = new GetMethodWebRequest(base + "/webresources/myresource/1");
WebResponse response = null;
try {
response = conv.getResponse(getRequest);
} catch (AuthorizationRequiredException e) {
fail(e.getMessage());
}
assertNotNull(response);
assertTrue(response.getText().contains("get1"));
}
use of com.meterware.httpunit.WebResponse in project javaee7-samples by javaee-samples.
the class MyResourceTest method testGetWithIncorrectCredentials.
@Test
public void testGetWithIncorrectCredentials() throws IOException, SAXException {
WebConversation conv = new WebConversation();
conv.setAuthentication("file", "random", "random");
GetMethodWebRequest getRequest = new GetMethodWebRequest(base + "/webresources/myresource");
try {
WebResponse response = conv.getResponse(getRequest);
} catch (AuthorizationRequiredException e) {
assertNotNull(e);
return;
}
fail("GET can be called with incorrect credentials");
}
use of com.meterware.httpunit.WebResponse 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");
}
use of com.meterware.httpunit.WebResponse 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());
}
}
use of com.meterware.httpunit.WebResponse 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());
}
}
Aggregations