@Test public void testDownload() throws IOException { doAnswer(verifyRequestContent()) .when(responseHandleFunction) .apply(any(HttpServletRequest.class), any(HttpServletResponse.class)); FileDownloadRequest fileDownloadRequest = new FileDownloadRequest("localhost", port, SESSION_ID); File downloadedFile = fileDownloadRequest.download(fileToGet.getAbsolutePath()); try (FileInputStream fileInputStream = new FileInputStream(downloadedFile)) { String s = IOUtils.toString(fileInputStream, StandardCharsets.UTF_8); assertThat(s, is(EXPECTED_CONTENT)); } verify(responseHandleFunction, times(1)).apply(any(HttpServletRequest.class), any(HttpServletResponse.class)); }
public File download(String pathToFile, String extension) { String encodedPath; encodedPath = Base64.getUrlEncoder().encodeToString(pathToFile.getBytes(StandardCharsets.UTF_8)); HttpGet request = new HttpGet(String.format(FILE_DOWNLOAD_EXTENSION_PATH, sessionId, encodedPath)); try { CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse execute = httpClient.execute(httpHost, request); int statusCode = execute.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK == statusCode) { File downloadResult = File.createTempFile("download_result", extension); try ( FileOutputStream outputStream = new FileOutputStream(downloadResult); InputStream responseStream = execute.getEntity().getContent()) { IOUtils.copy(responseStream, outputStream); } return downloadResult; } else { String message = IOUtils.toString(execute.getEntity().getContent(), "UTF-8"); throw new FileDownloadException(message, statusCode); } } catch (IOException e) { throw new FileDownloadException(e); } }
FileDownloadRequest { public File download(String pathToFile, String extension) { String encodedPath; encodedPath = Base64.getUrlEncoder().encodeToString(pathToFile.getBytes(StandardCharsets.UTF_8)); HttpGet request = new HttpGet(String.format(FILE_DOWNLOAD_EXTENSION_PATH, sessionId, encodedPath)); try { CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse execute = httpClient.execute(httpHost, request); int statusCode = execute.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK == statusCode) { File downloadResult = File.createTempFile("download_result", extension); try ( FileOutputStream outputStream = new FileOutputStream(downloadResult); InputStream responseStream = execute.getEntity().getContent()) { IOUtils.copy(responseStream, outputStream); } return downloadResult; } else { String message = IOUtils.toString(execute.getEntity().getContent(), "UTF-8"); throw new FileDownloadException(message, statusCode); } } catch (IOException e) { throw new FileDownloadException(e); } } }
FileDownloadRequest { public File download(String pathToFile, String extension) { String encodedPath; encodedPath = Base64.getUrlEncoder().encodeToString(pathToFile.getBytes(StandardCharsets.UTF_8)); HttpGet request = new HttpGet(String.format(FILE_DOWNLOAD_EXTENSION_PATH, sessionId, encodedPath)); try { CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse execute = httpClient.execute(httpHost, request); int statusCode = execute.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK == statusCode) { File downloadResult = File.createTempFile("download_result", extension); try ( FileOutputStream outputStream = new FileOutputStream(downloadResult); InputStream responseStream = execute.getEntity().getContent()) { IOUtils.copy(responseStream, outputStream); } return downloadResult; } else { String message = IOUtils.toString(execute.getEntity().getContent(), "UTF-8"); throw new FileDownloadException(message, statusCode); } } catch (IOException e) { throw new FileDownloadException(e); } } FileDownloadRequest(String host, int port, String sessionId); }
FileDownloadRequest { public File download(String pathToFile, String extension) { String encodedPath; encodedPath = Base64.getUrlEncoder().encodeToString(pathToFile.getBytes(StandardCharsets.UTF_8)); HttpGet request = new HttpGet(String.format(FILE_DOWNLOAD_EXTENSION_PATH, sessionId, encodedPath)); try { CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse execute = httpClient.execute(httpHost, request); int statusCode = execute.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK == statusCode) { File downloadResult = File.createTempFile("download_result", extension); try ( FileOutputStream outputStream = new FileOutputStream(downloadResult); InputStream responseStream = execute.getEntity().getContent()) { IOUtils.copy(responseStream, outputStream); } return downloadResult; } else { String message = IOUtils.toString(execute.getEntity().getContent(), "UTF-8"); throw new FileDownloadException(message, statusCode); } } catch (IOException e) { throw new FileDownloadException(e); } } FileDownloadRequest(String host, int port, String sessionId); File download(String pathToFile, String extension); File download(String pathToFile); }
FileDownloadRequest { public File download(String pathToFile, String extension) { String encodedPath; encodedPath = Base64.getUrlEncoder().encodeToString(pathToFile.getBytes(StandardCharsets.UTF_8)); HttpGet request = new HttpGet(String.format(FILE_DOWNLOAD_EXTENSION_PATH, sessionId, encodedPath)); try { CloseableHttpClient httpClient = HttpClients.createDefault(); CloseableHttpResponse execute = httpClient.execute(httpHost, request); int statusCode = execute.getStatusLine().getStatusCode(); if (HttpStatus.SC_OK == statusCode) { File downloadResult = File.createTempFile("download_result", extension); try ( FileOutputStream outputStream = new FileOutputStream(downloadResult); InputStream responseStream = execute.getEntity().getContent()) { IOUtils.copy(responseStream, outputStream); } return downloadResult; } else { String message = IOUtils.toString(execute.getEntity().getContent(), "UTF-8"); throw new FileDownloadException(message, statusCode); } } catch (IOException e) { throw new FileDownloadException(e); } } FileDownloadRequest(String host, int port, String sessionId); File download(String pathToFile, String extension); File download(String pathToFile); static final String FILE_DOWNLOAD_EXTENSION_PATH; }