use of com.rabbitmq.http.client.HttpLayer.HttpLayerFactory in project hop by rabbitmq.
the class JdkHttpClientHttpLayerTest method tls.
@Test
void tls() throws Exception {
int port = randomNetworkPort();
AbstractHandler httpHandler = new AbstractHandler() {
@Override
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException {
String json = "[]";
response.setStatus(HttpServletResponse.SC_OK);
response.setContentLength(json.length());
response.setContentType("application/json");
response.getWriter().print(json);
baseRequest.setHandled(true);
}
};
KeyStore keyStore = startHttpsServer(port, httpHandler);
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");
tmf.init(keyStore);
SSLContext sslContext = SSLContext.getInstance("TLSv1.3");
sslContext.init(null, tmf.getTrustManagers(), null);
HttpLayerFactory factory = JdkHttpClientHttpLayer.configure().clientBuilderConsumer(builder -> builder.sslContext(sslContext)).requestBuilderConsumer(builder -> {
}).create();
HttpLayer httpLayer = factory.create(new ClientParameters());
URI uri = new URI("https://localhost:" + port + "/foo");
httpLayer.get(uri, String[].class);
}
use of com.rabbitmq.http.client.HttpLayer.HttpLayerFactory in project hop by rabbitmq.
the class JdkHttpClientHttpLayerTest method builderConsumers.
@Test
void builderConsumers() throws Exception {
WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
wireMockServer.start();
try {
WireMock.configureFor("http", "localhost", wireMockServer.port());
stubFor(get(urlPathMatching("/foo")).willReturn(aResponse().withBody("[]")));
AtomicBoolean clientBuilderConsumerCalled = new AtomicBoolean(false);
AtomicBoolean requestBuilderConsumerCalled = new AtomicBoolean(false);
HttpLayerFactory factory = JdkHttpClientHttpLayer.configure().clientBuilderConsumer(builder -> clientBuilderConsumerCalled.set(true)).requestBuilderConsumer(builder -> requestBuilderConsumerCalled.set(true)).create();
HttpLayer httpLayer = factory.create(new ClientParameters());
URI uri = new URI("http://localhost:" + wireMockServer.port() + "/foo");
httpLayer.get(uri, String[].class);
verify(exactly(1), getRequestedFor(urlEqualTo("/foo")));
assertThat(clientBuilderConsumerCalled).isTrue();
assertThat(requestBuilderConsumerCalled).isTrue();
} finally {
wireMockServer.stop();
}
}
use of com.rabbitmq.http.client.HttpLayer.HttpLayerFactory in project hop by rabbitmq.
the class AppTest method createUseClient.
@Test
void createUseClient() throws Exception {
HttpLayerFactory httpLayerFactory = JdkHttpClientHttpLayer.configure().clientBuilderConsumer(clientBuilder -> clientBuilder.connectTimeout(Duration.ofSeconds(10))).requestBuilderConsumer(requestBuilder -> requestBuilder.timeout(Duration.ofSeconds(10)).setHeader("Authorization", authorization("guest", "guest"))).create();
Client c = new Client(new ClientParameters().url("http://127.0.0.1:15672/api/").username("guest").password("guest").httpLayerFactory(httpLayerFactory));
c.getOverview();
}
use of com.rabbitmq.http.client.HttpLayer.HttpLayerFactory in project hop by rabbitmq.
the class JdkHttpClientHttpLayerTest method clientServerErrorsShouldTriggerClientServerExceptions.
@Test
void clientServerErrorsShouldTriggerClientServerExceptions() throws Exception {
WireMockServer wireMockServer = new WireMockServer(wireMockConfig().dynamicPort());
wireMockServer.start();
try {
WireMock.configureFor("http", "localhost", wireMockServer.port());
stubFor(get(urlPathMatching("/client-error")).willReturn(WireMock.forbidden()));
stubFor(get(urlPathMatching("/client-not-found")).willReturn(WireMock.notFound()));
stubFor(get(urlPathMatching("/server-error")).willReturn(WireMock.serviceUnavailable()));
HttpLayerFactory factory = JdkHttpClientHttpLayer.configure().create();
HttpLayer httpLayer = factory.create(new ClientParameters());
URI baseUri = new URI("http://localhost:" + wireMockServer.port());
assertThatThrownBy(() -> httpLayer.get(baseUri.resolve("/client-error"), String[].class)).isInstanceOf(HttpClientException.class);
verify(exactly(1), getRequestedFor(urlEqualTo("/client-error")));
assertThat(httpLayer.get(baseUri.resolve("/client-not-found"), String[].class)).isNull();
verify(exactly(1), getRequestedFor(urlEqualTo("/client-not-found")));
assertThatThrownBy(() -> httpLayer.get(baseUri.resolve("/server-error"), String[].class)).isInstanceOf(HttpServerException.class);
verify(exactly(1), getRequestedFor(urlEqualTo("/server-error")));
} finally {
wireMockServer.stop();
}
}
Aggregations