MultiPartFormUploadServlet extends HttpServlet { @Override protected void doGet(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); boolean isMultipartContent = ServletFileUpload.isMultipartContent(request); if (!isMultipartContent) { out.println("You are not trying to upload"); } else { try { List<FileItem> fields = servletFileUpload.parseRequest(request); String msg = multiPartFileParser.parseMultiPartFiles(fields); LOGGER.info(urlAccessLogMessageAssembler.assembleMessage(request, msg)); out.write(msg); } catch (Exception e) { response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); out.write("File uploading failed! cause: " + e.getMessage()); LOGGER.info(urlAccessLogMessageAssembler.assembleMessage(request, e.getMessage()), e); } } } @Autowired  MultiPartFormUploadServlet(final ServletFileUploadFactory servletFileUploadFactory, final MultiPartFileParser multiPartFileParser,
                                      final UrlAccessLogMessageAssembler urlAccessLogMessageAssembler);   }
@Test public void testDoGetShouldSetResponseContentType() throws ServletException, IOException { given(request.getContentType()).willReturn("GET"); underTest.doGet(request, response); verify(response).setContentType("text/html"); }
@Test public void testDoGetShouldWriteResopnseWhenGotNotMultiPartRequest() throws ServletException, IOException { given(request.getContentType()).willReturn("GET"); underTest.doGet(request, response); verify(response).setContentType("text/html"); verify(printWriter).println("You are not trying to upload"); }
@Test public void testDoGetShouldSendErrorResponseWhenRequestParsingFailedByTheServletFileUpload() throws ServletException, IOException, FileUploadException { given(request.getMethod()).willReturn("POST"); given(servletFileUpload.parseRequest(request)).willThrow(new FileUploadException()); underTest.doGet(request, response); verify(response).setContentType("text/html"); verify(response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); verify(printWriter).write(Matchers.anyString()); }
@Test public void testDoGetShouldSendErrorResponseWhenFileParsingFailed() throws ServletException, IOException, FileUploadException { List<FileItem> uploadedFiles = new ArrayList<>(); given(request.getMethod()).willReturn("POST"); given(servletFileUpload.parseRequest(request)).willReturn(uploadedFiles); given(multiPartFileParser.parseMultiPartFiles(uploadedFiles)).willThrow(new CannotUploadExternalResourceException("", null)); underTest.doGet(request, response); verify(response).setContentType("text/html"); verify(response).setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); verify(printWriter).write(Matchers.anyString()); }
@Test public void testDoGetShouldSendFileParserMessageWhenUploadingSuccess() throws ServletException, IOException, FileUploadException { List<FileItem> uploadedFiles = new ArrayList<>(); given(request.getMethod()).willReturn("POST"); given(servletFileUpload.parseRequest(request)).willReturn(uploadedFiles); given(multiPartFileParser.parseMultiPartFiles(uploadedFiles)).willReturn(""); underTest.doGet(request, response); verify(response).setContentType("text/html"); verify(printWriter).write(""); }