HttpCall { public File downloadToDirectory(File dir) { Request okRequest = buildOkHttpRequest(); try { return doDownloadToDirectory(dir, okRequest); } catch (ProtocolException|SocketException|SocketTimeoutException se) { try { return doDownloadToDirectory(dir, okRequest); } catch (IOException e2) { throw new IllegalStateException(format("Can not call %s", okRequest.url()), e2); } } catch (IOException e) { throw new IllegalStateException(format("Can not call %s", okRequest.url()), e); } } HttpCall(OkHttpClient okClient, HttpUrl baseUrl); HttpUrl getBaseUrl(); HttpCall setMethod(HttpMethod m); HttpCall setCredentials(String login, @Nullable String password); HttpCall setAdminCredentials(); HttpCall setAuthenticationToken(String token); HttpCall setParam(String key, @Nullable String value); HttpCall setParams(String key1, @Nullable String value1, String... otherKeysAndNames); HttpCall setHeader(String key, String value); HttpCall setTimeoutMs(long timeoutMs); HttpResponse execute(); HttpResponse executeUnsafely(); void downloadToFile(File file); File downloadToDirectory(File dir);  }
@Test public void downloadToDir_downloads_content_in_file_named_by_URL_if_ContentDisposition_header_is_not_present() throws Exception { server.enqueue(new MockResponse().setBody(PONG)); File dir = temp.newFolder(); newCall("api/system/ping.txt").downloadToDirectory(dir); assertThat(new File(dir, "ping.txt")).isFile().exists().hasContent(PONG); }
@Test public void downloadToDir_throws_ISE_if_ContentDisposition_header_is_not_present_and_URL_does_not_contain_filename() throws Exception { server.enqueue(new MockResponse().setBody(PONG)); File dir = temp.newFolder(); expectedException.expect(IllegalStateException.class); expectedException.expectMessage("Can not guess the target filename for download of " + server.url("") + ". Header Content-Disposition is missing or empty."); newCall("").downloadToDirectory(dir); }
@Test public void downloadToDir_throws_HttpException_if_response_code_is_not_2xx() throws Exception { server.enqueue(new MockResponse().setBody("<error>").setResponseCode(500)); File dir = temp.newFolder(); try { newCall("api/system/ping").downloadToDirectory(dir); fail(); } catch (HttpException e) { assertThat(e.getCode()).isEqualTo(500); assertThat(e.getUrl()).isEqualTo(server.url("api/system/ping").toString()); assertThat(e.getBody()).isEqualTo("<error>"); } }