AppLogController { @RequestMapping(value = "/logs/{fileName:.+}", method = {RequestMethod.GET, RequestMethod.POST}) public ResponseEntity<String> getLogFileContent(@PathVariable("fileName") final String fileName, @RequestParam(value = "source", defaultValue = "false") final boolean source, @RequestHeader(value = "User-Agent", defaultValue = "") final String userAgent) { String body = logFileProvider.getLogContent(fileName); body = convertLineBreaksIfOnWindows(body, userAgent); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.TEXT_PLAIN); if (!source) { headers.set(CONTENT_DISPOSITION, String.format(ATTACHMENT_TEMPLATE, fileName)); } ResponseEntity<String> responseEntity = new ResponseEntity<String>(body, headers, HttpStatus.OK); return responseEntity; }  @RequestMapping(value = "/applog", method = RequestMethod.GET) String applog(); @ResponseBody @RequestMapping(value = "/logs", method = {RequestMethod.GET, RequestMethod.POST}) Map<String, Collection<String>> getLogFiles(); @RequestMapping(value = "/logs/{fileName:.+}", method = {RequestMethod.GET, RequestMethod.POST}) ResponseEntity<String> getLogFileContent(@PathVariable("fileName") final String fileName,
            @RequestParam(value = "source", defaultValue = "false") final boolean source,
            @RequestHeader(value = "User-Agent", defaultValue = "") final String userAgent);  }
@Test public void testGetLogFileContentWhenSourceIsTrueShouldNotSetContentDisposition() { String expectedBody = "content"; String fileName = "something"; given(logFileProvider.getLogContent(fileName)).willReturn(expectedBody); ResponseEntity<String> result = underTest.getLogFileContent(fileName, true, NOT_IMPORTANT); assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType()); assertEquals(HttpStatus.OK, result.getStatusCode()); assertNull(result.getHeaders().get(CONTENT_DISPOSITION)); assertEquals(expectedBody, result.getBody()); }
@Test public void testGetLogFileContentWhenSourceIsFalseShouldSetContentDispositionToAttachment() { String expectedBody = "content"; String fileName = "something"; given(logFileProvider.getLogContent(fileName)).willReturn(expectedBody); ResponseEntity<String> result = underTest.getLogFileContent(fileName, false, NOT_IMPORTANT); assertEquals(MediaType.TEXT_PLAIN, result.getHeaders().getContentType()); assertEquals(HttpStatus.OK, result.getStatusCode()); assertEquals(String.format(ATTACHMENT_TEMPLATE, fileName), result.getHeaders().getFirst(CONTENT_DISPOSITION)); assertEquals(expectedBody, result.getBody()); }
@Test public void testGetLogFileContentWhenUserIsOnWindowsShouldConvertLineBreaks() { String expectedBody = "content\r\n"; String userAgentWindows = "SOMETHINGSOMETHING-WINDOWS"; String body = "content\n"; String fileName = "something"; given(logFileProvider.getLogContent(fileName)).willReturn(body); ResponseEntity<String> result = underTest.getLogFileContent(fileName, true, userAgentWindows); assertEquals(expectedBody, result.getBody()); }