LogFileHandler { public void writeFileContentToResponse(final HttpServletRequest req, final HttpServletResponse resp, final String pathInfo, final Path path) throws IOException { String requestedFilename = pathInfo.substring(1); InputStream messageFileInputStream = messageFileReader.readFile(requestedFilename, path); if (messageFileExists(messageFileInputStream)) { writeFileToResponse(req, resp, requestedFilename, messageFileInputStream); } else { writeErrorToResponse(resp); } }  void writeFileNamesToResponse(final HttpServletResponse resp, final Path path); void writeFileContentToResponse(final HttpServletRequest req, final HttpServletResponse resp, final String pathInfo, final Path path);  }
@Test public final void testWriteFileContentToResponseShouldWriteErrorMessageWhenPathIsAnInvalidFileName() throws IOException { given(messageFileReader.readFile("invalidfilename", path)).willReturn(null); underTest.writeFileContentToResponse(request, response, "invalidfilename", path); verify(response).setContentType("text/html"); verify(printWriter).write("Requested file not found."); verify(printWriter).flush(); verify(printWriter).close(); }
@Test public final void testWriteFileContentToResponseShouldSendFileWhenPathIsAValidFileName() throws IOException { given(messageFileReader.readFile("filename", path)).willReturn(fileInputStream); given(request.getHeader("User-Agent")).willReturn("windows"); underTest.writeFileContentToResponse(request, response, "/filename", path); verify(response).setContentType("text/plain"); verify(response).setHeader("Content-Disposition", "attachment;filename=filename"); verify(printWriter).write(FILE_CONTENT); verify(printWriter).flush(); verify(printWriter).close(); }
@Test public final void testWriteFileContentToResponseShouldSendFileAsAttachmentWhenPathIsAValidFileName() throws IOException { given(request.getPathInfo()).willReturn("/filename"); given(messageFileReader.readFile("filename", path)).willReturn(fileInputStream); given(request.getHeader("User-Agent")).willReturn("windows"); underTest.writeFileContentToResponse(request, response, "/filename", path); verify(response).setContentType("text/plain"); verify(response).setHeader("Content-Disposition", "attachment;filename=filename"); verify(printWriter).write(FILE_CONTENT); verify(printWriter).flush(); verify(printWriter).close(); }
@Test public final void testWriteFileContentToResponseShouldSendFileContentWhenTheSourceParameterIsTrue() throws IOException { given(messageFileReader.readFile("filename", path)).willReturn(fileInputStream); given(request.getParameter("source")).willReturn("true"); given(request.getHeader("User-Agent")).willReturn("windows"); underTest.writeFileContentToResponse(request, response, "/filename", path); verify(response).setContentType("text/plain"); verify(printWriter).write(FILE_CONTENT); verify(printWriter).flush(); verify(printWriter).close(); }
@Test public final void testWriteFileContentToResponseShouldSendFileAsAttachmentWhenTheSourceParameterIsNotTrue() throws IOException { given(messageFileReader.readFile("filename", path)).willReturn(fileInputStream); given(request.getParameter("source")).willReturn(""); given(request.getHeader("User-Agent")).willReturn("windows"); underTest.writeFileContentToResponse(request, response, "/filename", path); verify(response).setContentType("text/plain"); verify(response).setHeader("Content-Disposition", "attachment;filename=filename"); verify(printWriter).write(FILE_CONTENT); verify(printWriter).flush(); verify(printWriter).close(); }
@Test public final void testWriteFileContentToResponseShouldNotReplaceNewLineWhenNotOnWindows() throws IOException { given(messageFileReader.readFile("filename", path)).willReturn(fileInputStream); given(request.getParameter("source")).willReturn(""); given(request.getHeader("User-Agent")).willReturn("mac"); String fileContent = "multi\nline"; given(inputStreamConverter.toString(fileInputStream)).willReturn(fileContent); underTest.writeFileContentToResponse(request, response, "/filename", path); verify(printWriter).write(fileContent); }
@Test public final void testWriteFileContentToResponseShouldNotReplaceNewLineWhenNoUserAgentHeader() throws IOException { given(messageFileReader.readFile("filename", path)).willReturn(fileInputStream); given(request.getParameter("source")).willReturn(""); given(request.getHeader("User-Agent")).willReturn(null); String fileContent = "multi\nline"; given(inputStreamConverter.toString(fileInputStream)).willReturn(fileContent); underTest.writeFileContentToResponse(request, response, "/filename", path); verify(printWriter).write(fileContent); }