public static HttpClient of(final CloseableHttpAsyncClient client) { return new IntegrationTestHttpClient(client); }
@Override protected CompletionStage<HttpResponse> executeDelegate(final HttpRequest httpRequest) throws Throwable { final HttpUriRequest realHttpRequest = toApacheRequest(httpRequest); final CompletableFuture<org.apache.http.HttpResponse> apacheResponseFuture = new CompletableFuture<>(); apacheHttpClient.execute(realHttpRequest, new CompletableFutureCallbackAdapter<>(apacheResponseFuture)); return apacheResponseFuture.thenApply(apacheResponse -> convertApacheToSphereResponse(apacheResponse, httpRequest)); }
protected static HttpClient newHttpClient() { //NO SSL Client: this client doesn't perform ssl certification check, which is a necessity to run tests on CI CloseableHttpAsyncClient asyncClient = createNoSSLClient(); return IntegrationTestHttpClient.of(asyncClient); }
private HttpUriRequest toApacheRequest(final HttpRequest httpRequest) throws UnsupportedEncodingException { final String method = httpRequest.getHttpMethod().toString(); final String uri = httpRequest.getUrl(); final RequestBuilder builder = RequestBuilder .create(method) .setUri(uri); httpRequest.getHeaders().getHeadersAsMap().forEach((name, values) -> values.forEach(value -> builder.addHeader(name, value))); if (httpRequest.getBody() != null) { final HttpRequestBody body = httpRequest.getBody(); final HttpEntity httpEntity; if (body instanceof StringHttpRequestBody) { final StringEntity stringEntity = new StringEntity(((StringHttpRequestBody) body).getString(), StandardCharsets.UTF_8); stringEntity.setContentType(ContentType.APPLICATION_JSON.toString()); httpEntity = stringEntity; } else if (body instanceof FileHttpRequestBody) { httpEntity = new FileEntity(((FileHttpRequestBody)body).getFile()); } else if (body instanceof FormUrlEncodedHttpRequestBody) { httpEntity = urlEncodedOf((FormUrlEncodedHttpRequestBody) body); } else { throw new HttpException("Cannot interpret request " + httpRequest); } builder.setEntity(httpEntity); } return builder.build(); }