HttpCall { public HttpResponse execute() { Request okRequest = buildOkHttpRequest(); try (Response okResponse = doExecute(okRequest)) { if (!okResponse.isSuccessful()) { throw new HttpException(okRequest.url(), okResponse.code(), okResponse.body().string()); } return HttpResponse.from(okResponse); } catch (IOException e) { throw new IllegalStateException(format("Can not call %s due to network failure", 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 execute_GET_request_should_return_response() throws Exception { server.enqueue(new MockResponse().setBody(PONG)); HttpResponse response = newCall("").execute(); verifySuccess(response, PONG); RecordedRequest recordedRequest = server.takeRequest(); verifyRecorded(recordedRequest, "GET", ""); }
@Test public void user_agent_should_be_set_with_default_value_if_not_manually_defined() throws Exception { server.enqueue(new MockResponse().setBody(PONG)); newCall("").execute(); RecordedRequest recordedRequest = server.takeRequest(); verifyHeader(recordedRequest, "User-Agent", "Orchestrator"); }
@Test public void Authorization_header_is_not_defined_by_default() throws Exception { server.enqueue(new MockResponse().setBody(PONG)); newCall("").execute(); RecordedRequest recordedRequest = server.takeRequest(); assertThat(recordedRequest.getHeader("Authorization")).isNull(); }
@Test public void execute_throws_HttpException_if_response_code_is_not_2xx() { server.enqueue(new MockResponse().setResponseCode(404).setBody("<error>")); try { newCall("api/system/ping").execute(); fail(); } catch (HttpException e) { assertThat(e.getMessage()).isEqualTo(format("URL [http: assertThat(e.getCode()).isEqualTo(404); assertThat(e.getUrl()).isEqualTo(server.url("api/system/ping").toString()); assertThat(e.getBody()).isEqualTo("<error>"); } }
@Test public void charset_of_response_body_is_utf8_if_header_ContentType_is_missing() throws Exception { server.enqueue(new MockResponse().setBody(PONG)); HttpResponse response = newCall("api/system/ping").execute(); assertThat(response.getCharset()).isEqualTo(UTF_8); }