"\r\n" +
"This is the content of the file\n" +
"\r\n" +
"-----1234--\r\n";
ServletFileUpload upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(-1);
HttpServletRequest req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
List<FileItem> fileItems = upload.parseRequest(req);
assertEquals(1, fileItems.size());
FileItem item = fileItems.get(0);
assertEquals("This is the content of the file\n", new String(item.get()));
upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(40);
req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
fileItems = upload.parseRequest(req);
assertEquals(1, fileItems.size());
item = fileItems.get(0);
assertEquals("This is the content of the file\n", new String(item.get()));
// provided Content-Length is larger than the FileSizeMax -> handled by ctor
upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(5);
req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
try {
upload.parseRequest(req);
fail("Expected exception.");
} catch (FileUploadBase.FileSizeLimitExceededException e) {
assertEquals(5, e.getPermittedSize());
}
// provided Content-Length is wrong, actual content is larger -> handled by LimitedInputStream
upload = new ServletFileUpload(new DiskFileItemFactory());
upload.setFileSizeMax(15);
req = new MockHttpServletRequest(request.getBytes("US-ASCII"), CONTENT_TYPE);
try {
upload.parseRequest(req);
fail("Expected exception.");
} catch (FileUploadBase.FileSizeLimitExceededException e) {
assertEquals(15, e.getPermittedSize());
}
}