HttpHelper { public File downloadFile(String url, Path targetDirectory) throws IOException { String filename = url.substring(url.lastIndexOf("/") + 1); return downloadFile(url, targetDirectory, filename); } HttpHelper(HttpClient httpClient); File downloadFile(String url, Path targetDirectory); File downloadFile(String url, Path targetDirectory, String filename);  }
@Test public void downloadFileWritesTheFileToDisk() throws Exception { wireMockRule.stubFor(get(urlPathEqualTo(FILE_REQUEST)) .willReturn(aResponse().withStatus(HttpConstants.STATUS_OK) .withBody(TEST_CONTENT))); Path targetDirectory = temporaryFolder.newFolder("target").toPath(); Path resultFile = httpHelper.downloadFile(wireMockRule.url(FILE_REQUEST), targetDirectory).toPath(); checkDownloadedFile(targetDirectory, resultFile); }
@Test public void downloadFileNameCanBeOverwritten() throws IOException { final String downloadUri = "/foo/bar.txt"; wireMockRule.stubFor(get(urlPathEqualTo(downloadUri)) .willReturn(aResponse().withStatus(HttpConstants.STATUS_OK) .withBody(TEST_CONTENT))); Path targetDirectory = temporaryFolder.newFolder("target").toPath(); Path resultFile = httpHelper.downloadFile(wireMockRule.url(downloadUri), targetDirectory, FILE_NAME).toPath(); checkDownloadedFile(targetDirectory, resultFile); }
@Test(expected = IOException.class) public void downloadFileThrowsExceptionOn404StatusCode() throws Exception { wireMockRule.stubFor(get(anyUrl()) .willReturn(aResponse().withStatus(HttpConstants.STATUS_ERR_NOT_FOUND))); Path targetDirectory = temporaryFolder.newFolder("target").toPath(); httpHelper.downloadFile(wireMockRule.url("/test"), targetDirectory); }