use of org.apache.hc.client5.http.fluent.Request in project feign by OpenFeign.
the class AsyncApacheHttp5ClientTest method throwsFeignExceptionIncludingBody.
@Test
public void throwsFeignExceptionIncludingBody() throws Throwable {
server.enqueue(new MockResponse().setBody("success!"));
final TestInterfaceAsync api = AsyncFeign.asyncBuilder().decoder((response, type) -> {
throw new IOException("timeout");
}).target(TestInterfaceAsync.class, "http://localhost:" + server.getPort());
final CompletableFuture<?> cf = api.body("Request body");
server.takeRequest();
try {
unwrap(cf);
} catch (final FeignException e) {
assertThat(e.getMessage()).isEqualTo("timeout reading POST http://localhost:" + server.getPort() + "/");
assertThat(e.contentUTF8()).isEqualTo("Request body");
return;
}
fail();
}
use of org.apache.hc.client5.http.fluent.Request in project feign by OpenFeign.
the class ApacheHttp5Client method execute.
@Override
public Response execute(Request request, Request.Options options) throws IOException {
ClassicHttpRequest httpUriRequest;
try {
httpUriRequest = toClassicHttpRequest(request, options);
} catch (final URISyntaxException e) {
throw new IOException("URL '" + request.url() + "' couldn't be parsed into a URI", e);
}
final HttpHost target = HttpHost.create(URI.create(request.url()));
final HttpClientContext context = configureTimeouts(options);
final ClassicHttpResponse httpResponse = (ClassicHttpResponse) client.execute(target, httpUriRequest, context);
return toFeignResponse(httpResponse, request);
}
use of org.apache.hc.client5.http.fluent.Request in project feign by OpenFeign.
the class ApacheHttp5Client method configureTimeouts.
protected HttpClientContext configureTimeouts(Request.Options options) {
final HttpClientContext context = new HttpClientContext();
// per request timeouts
final RequestConfig requestConfig = (client instanceof Configurable ? RequestConfig.copy(((Configurable) client).getConfig()) : RequestConfig.custom()).setConnectTimeout(options.connectTimeout(), options.connectTimeoutUnit()).setResponseTimeout(options.readTimeout(), options.readTimeoutUnit()).build();
context.setRequestConfig(requestConfig);
return context;
}
use of org.apache.hc.client5.http.fluent.Request in project metrics by dropwizard.
the class InstrumentedHttpAsyncClientsTest method registersExpectedMetricsGivenNameStrategy.
@Test
public void registersExpectedMetricsGivenNameStrategy() throws Exception {
client = InstrumentedHttpAsyncClients.custom(metricRegistry, metricNameStrategy).disableAutomaticRetries().build();
client.start();
final SimpleHttpRequest request = SimpleHttpRequests.get("http://localhost:" + httpServer.getAddress().getPort() + "/");
final String metricName = "some.made.up.metric.name";
httpServer.createContext("/", exchange -> {
exchange.sendResponseHeaders(200, 0L);
exchange.setStreams(null, null);
exchange.getResponseBody().write("TEST".getBytes(StandardCharsets.US_ASCII));
exchange.close();
});
httpServer.start();
when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(metricName);
final Future<SimpleHttpResponse> responseFuture = client.execute(request, new FutureCallback<SimpleHttpResponse>() {
@Override
public void completed(SimpleHttpResponse result) {
assertThat(result.getBodyText()).isEqualTo("TEST");
}
@Override
public void failed(Exception ex) {
fail();
}
@Override
public void cancelled() {
fail();
}
});
responseFuture.get(1L, TimeUnit.SECONDS);
verify(registryListener).onTimerAdded(eq(metricName), any(Timer.class));
}
use of org.apache.hc.client5.http.fluent.Request in project metrics by dropwizard.
the class InstrumentedHttpAsyncClientsTest method usesCustomClientConnectionManager.
@Test
public void usesCustomClientConnectionManager() throws Exception {
try (PoolingAsyncClientConnectionManager clientConnectionManager = spy(new PoolingAsyncClientConnectionManager())) {
client = InstrumentedHttpAsyncClients.custom(metricRegistry, metricNameStrategy, clientConnectionManager).disableAutomaticRetries().build();
client.start();
final SimpleHttpRequest request = SimpleHttpRequests.get("http://localhost:" + httpServer.getAddress().getPort() + "/");
final String metricName = "some.made.up.metric.name";
httpServer.createContext("/", exchange -> {
exchange.sendResponseHeaders(200, 0L);
exchange.setStreams(null, null);
exchange.getResponseBody().write("TEST".getBytes(StandardCharsets.US_ASCII));
exchange.close();
});
httpServer.start();
when(metricNameStrategy.getNameFor(any(), any(HttpRequest.class))).thenReturn(metricName);
final Future<SimpleHttpResponse> responseFuture = client.execute(request, new FutureCallback<SimpleHttpResponse>() {
@Override
public void completed(SimpleHttpResponse result) {
assertThat(result.getCode()).isEqualTo(200);
}
@Override
public void failed(Exception ex) {
fail();
}
@Override
public void cancelled() {
fail();
}
});
responseFuture.get(1L, TimeUnit.SECONDS);
verify(clientConnectionManager, atLeastOnce()).connect(any(), any(), any(), any(), any(), any());
}
}
Aggregations