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());
}