Search in sources :

Example 6 with HttpResponse

use of org.apache.hc.core5.http.HttpResponse in project feign by OpenFeign.

the class AsyncApacheHttp5Client method toFeignResponse.

Response toFeignResponse(SimpleHttpResponse httpResponse, Request request) {
    final int statusCode = httpResponse.getCode();
    final String reason = httpResponse.getReasonPhrase();
    final Map<String, Collection<String>> headers = new HashMap<String, Collection<String>>();
    for (final Header header : httpResponse.getHeaders()) {
        final String name = header.getName();
        final String value = header.getValue();
        Collection<String> headerValues = headers.get(name);
        if (headerValues == null) {
            headerValues = new ArrayList<String>();
            headers.put(name, headerValues);
        }
        headerValues.add(value);
    }
    return Response.builder().status(statusCode).reason(reason).headers(headers).request(request).body(httpResponse.getBodyBytes()).build();
}
Also used : Header(org.apache.hc.core5.http.Header)

Example 7 with HttpResponse

use of org.apache.hc.core5.http.HttpResponse in project feign by OpenFeign.

the class AsyncApacheHttp5Client method execute.

@Override
public CompletableFuture<Response> execute(Request request, Options options, Optional<HttpClientContext> requestContext) {
    final SimpleHttpRequest httpUriRequest = toClassicHttpRequest(request, options);
    final CompletableFuture<Response> result = new CompletableFuture<>();
    final FutureCallback<SimpleHttpResponse> callback = new FutureCallback<SimpleHttpResponse>() {

        @Override
        public void completed(SimpleHttpResponse httpResponse) {
            result.complete(toFeignResponse(httpResponse, request));
        }

        @Override
        public void failed(Exception ex) {
            result.completeExceptionally(ex);
        }

        @Override
        public void cancelled() {
            result.cancel(false);
        }
    };
    client.execute(httpUriRequest, configureTimeouts(options, requestContext.orElseGet(HttpClientContext::new)), callback);
    return result;
}
Also used : SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) CompletableFuture(java.util.concurrent.CompletableFuture) SimpleHttpRequest(org.apache.hc.client5.http.async.methods.SimpleHttpRequest) HttpClientContext(org.apache.hc.client5.http.protocol.HttpClientContext) SimpleHttpResponse(org.apache.hc.client5.http.async.methods.SimpleHttpResponse) FutureCallback(org.apache.hc.core5.concurrent.FutureCallback)

Example 8 with HttpResponse

use of org.apache.hc.core5.http.HttpResponse in project pact-jvm by DiUS.

the class ArticlesTest method testArticles.

@Test
@PactTestFor(pactMethod = "articles")
void testArticles(MockServer mockServer) throws IOException {
    ClassicHttpResponse httpResponse = (ClassicHttpResponse) Request.get(mockServer.getUrl() + "/articles.json").execute().returnResponse();
    assertThat(httpResponse.getCode(), is(equalTo(200)));
    assertThat(IOUtils.toString(httpResponse.getEntity().getContent()), is(equalTo("{\"articles\":[{\"variants\":{\"0032\":{\"description\":\"sample description\"}}}]}")));
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 9 with HttpResponse

use of org.apache.hc.core5.http.HttpResponse in project pact-jvm by DiUS.

the class ArticlesTest method testArticlesDoNotExist.

@Test
@PactTestFor(pactMethod = "articlesDoNotExist")
void testArticlesDoNotExist(MockServer mockServer) throws IOException {
    ClassicHttpResponse httpResponse = (ClassicHttpResponse) Request.get(mockServer.getUrl() + "/articles.json").execute().returnResponse();
    assertThat(httpResponse.getCode(), is(equalTo(404)));
    assertThat(IOUtils.toString(httpResponse.getEntity().getContent()), is(equalTo("")));
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Example 10 with HttpResponse

use of org.apache.hc.core5.http.HttpResponse in project pact-jvm by DiUS.

the class Defect1070Test method testApi.

@Test
@PactTestFor
void testApi(MockServer mockServer) throws IOException {
    ClassicHttpResponse httpResponse = (ClassicHttpResponse) Request.get(mockServer.getUrl() + "/api/test/1234").execute().returnResponse();
    assertThat(httpResponse.getCode(), is(equalTo(200)));
    assertThat(IOUtils.toString(httpResponse.getEntity().getContent()), is(equalTo("[{\"id\":32432,\"name\":\"testId254\",\"size\":1445211}]")));
}
Also used : ClassicHttpResponse(org.apache.hc.core5.http.ClassicHttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)8 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)7 HttpResponse (org.apache.hc.core5.http.HttpResponse)3 Header (org.apache.hc.core5.http.Header)2 PactVerifications (au.com.dius.pact.consumer.junit.PactVerifications)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 SimpleHttpRequest (org.apache.hc.client5.http.async.methods.SimpleHttpRequest)1 SimpleHttpResponse (org.apache.hc.client5.http.async.methods.SimpleHttpResponse)1 BasicCookieStore (org.apache.hc.client5.http.cookie.BasicCookieStore)1 Cookie (org.apache.hc.client5.http.cookie.Cookie)1 HttpAsyncClientBuilder (org.apache.hc.client5.http.impl.async.HttpAsyncClientBuilder)1 PoolingAsyncClientConnectionManager (org.apache.hc.client5.http.impl.nio.PoolingAsyncClientConnectionManager)1 HttpClientContext (org.apache.hc.client5.http.protocol.HttpClientContext)1 RedirectStrategy (org.apache.hc.client5.http.protocol.RedirectStrategy)1 DefaultClientTlsStrategy (org.apache.hc.client5.http.ssl.DefaultClientTlsStrategy)1 FutureCallback (org.apache.hc.core5.concurrent.FutureCallback)1 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 BasicNameValuePair (org.apache.hc.core5.http.message.BasicNameValuePair)1 TlsStrategy (org.apache.hc.core5.http.nio.ssl.TlsStrategy)1 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)1