ServletFileUpload.parseRequest () returns an empty list for MockMultipartHttpServletRequest

Having looked at some of the existing answers on this topic, I still cannot understand what I can do wrong ...

I have the following query declaration in my Unit test:

final MockMultipartHttpServletRequest mockRequest = new MockMultipartHttpServletRequest(); mockRequest.setMethod("POST"); mockRequest.setContentType(contentType); mockRequest.setRequestURI("/upload"); mockRequest.addParameter("test_param", "test_value"); mockRequest.addFile(new MockMultipartFile("file1", "test_upload1.txt", "text/plain", "fileContent1".getBytes())); mockRequest.addFile(new MockMultipartFile("file2", "test_upload2.txt", "text/plain", "fileContent2".getBytes())); mockRequest.setContent("dummyContent".getBytes()); 

Then I use another class to handle:

 public void processServletRequest(final HttpServletRequest request) { if (ServletFileUpload.isMultipartContent(request)) { final FileItemFactory factory = new DiskFileItemFactory(); final ServletFileUpload upload = new ServletFileUpload(factory); List<FileItem> items = null; try { items = upload.parseRequest(request); } catch (final FileUploadException e) { this.logger.error("Could not upload document", e); return; } } 

}

But upload.parseRequest (request) always returns an empty list.

There is nothing else between these two places that could read the request input stream (without calling getParameter () or anything like that).

It is not possible to check if there is a problem with unit test and mock request or processing ... Any help would be appreciated.

EDIT: Well, manual testing seems successful, which means that I expect the servlet to work fine, and the problem is how I create the request with layouts, however, comparing it with other samples that I saw here and there, I I can’t understand what happened. The fact that the isMultipartContent () test succeeds also tells me that I am not doing this completely wrong ...

Maybe there is something else that I should use to create a dummy request, not this Spring helper class?

EDIT2: I gave up and decided to create the contents of my request manually by adding parameters and file metadata, creating a StringBuilder. This will open up if someone can understand what I did wrong before, but at least my test now works.

EDIT3: Adding a snippet of code from the last response as specified in the comment.

 public void createMultipartFormDataRequest(MockHttpServletRequest request, String resourceName, String partName) throws IOException { // Load resource being uploaded byte[] fileContent = FileCopyUtils.copyToByteArray(getClass().getResourceAsStream(resourceName)); // Create part & entity from resource Part[] parts = new Part[] { new FilePart(partName, new ByteArrayPartSource(resourceName, fileContent)) }; MultipartRequestEntity multipartRequestEntity = new MultipartRequestEntity(parts, new PostMethod().getParams()); // Serialize request body ByteArrayOutputStream requestContent = new ByteArrayOutputStream(); multipartRequestEntity.writeRequest(requestContent); // Set request body to HTTP servlet request request.setContent(requestContent.toByteArray()); // Set content type to HTTP servlet request (important, includes Mime boundary string) request.setContentType(multipartRequestEntity.getContentType()); } 
+4
source share
1 answer

Source: https://habr.com/ru/post/1488329/


All Articles