Search in sources :

Example 1 with HttpLayerFactory

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);
}
Also used : X509Certificate(java.security.cert.X509Certificate) Request(org.eclipse.jetty.server.Request) KeyPair(java.security.KeyPair) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) BeforeEach(org.junit.jupiter.api.BeforeEach) SSLContext(javax.net.ssl.SSLContext) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) ServerSocket(java.net.ServerSocket) SecureRandom(java.security.SecureRandom) WireMockConfiguration.wireMockConfig(com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig) BigInteger(java.math.BigInteger) URI(java.net.URI) Server(org.eclipse.jetty.server.Server) KeyPairGenerator(java.security.KeyPairGenerator) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) KeyStore(java.security.KeyStore) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test) Certificate(java.security.cert.Certificate) WireMock.exactly(com.github.tomakehurst.wiremock.client.WireMock.exactly) WireMock.getRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor) Handler(org.eclipse.jetty.server.Handler) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BCStyle(org.bouncycastle.asn1.x500.style.BCStyle) WireMock(com.github.tomakehurst.wiremock.client.WireMock) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) HttpServletRequest(javax.servlet.http.HttpServletRequest) WireMock.urlPathMatching(com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HttpLayerFactory(com.rabbitmq.http.client.HttpLayer.HttpLayerFactory) X500NameBuilder(org.bouncycastle.asn1.x500.X500NameBuilder) WireMock.get(com.github.tomakehurst.wiremock.client.WireMock.get) ValueSource(org.junit.jupiter.params.provider.ValueSource) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WireMock.verify(com.github.tomakehurst.wiremock.client.WireMock.verify) JdkHttpClientHttpLayer.maybeThrowClientServerException(com.rabbitmq.http.client.JdkHttpClientHttpLayer.maybeThrowClientServerException) ServerConnector(org.eclipse.jetty.server.ServerConnector) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ChronoUnit(java.time.temporal.ChronoUnit) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) WireMock.urlEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo) Connector(org.eclipse.jetty.server.Connector) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpLayerFactory(com.rabbitmq.http.client.HttpLayer.HttpLayerFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) URI(java.net.URI) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with HttpLayerFactory

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();
    }
}
Also used : X509Certificate(java.security.cert.X509Certificate) Request(org.eclipse.jetty.server.Request) KeyPair(java.security.KeyPair) SslConnectionFactory(org.eclipse.jetty.server.SslConnectionFactory) BeforeEach(org.junit.jupiter.api.BeforeEach) SSLContext(javax.net.ssl.SSLContext) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) ServerSocket(java.net.ServerSocket) SecureRandom(java.security.SecureRandom) WireMockConfiguration.wireMockConfig(com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig) BigInteger(java.math.BigInteger) URI(java.net.URI) Server(org.eclipse.jetty.server.Server) KeyPairGenerator(java.security.KeyPairGenerator) TrustManagerFactory(javax.net.ssl.TrustManagerFactory) WireMock.aResponse(com.github.tomakehurst.wiremock.client.WireMock.aResponse) KeyStore(java.security.KeyStore) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test) Certificate(java.security.cert.Certificate) WireMock.exactly(com.github.tomakehurst.wiremock.client.WireMock.exactly) WireMock.getRequestedFor(com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor) Handler(org.eclipse.jetty.server.Handler) HttpConnectionFactory(org.eclipse.jetty.server.HttpConnectionFactory) X509CertificateHolder(org.bouncycastle.cert.X509CertificateHolder) JcaX509CertificateConverter(org.bouncycastle.cert.jcajce.JcaX509CertificateConverter) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpVersion(org.eclipse.jetty.http.HttpVersion) JcaContentSignerBuilder(org.bouncycastle.operator.jcajce.JcaContentSignerBuilder) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BCStyle(org.bouncycastle.asn1.x500.style.BCStyle) WireMock(com.github.tomakehurst.wiremock.client.WireMock) SecureRequestCustomizer(org.eclipse.jetty.server.SecureRequestCustomizer) HttpServletRequest(javax.servlet.http.HttpServletRequest) WireMock.urlPathMatching(com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) HttpConfiguration(org.eclipse.jetty.server.HttpConfiguration) HttpLayerFactory(com.rabbitmq.http.client.HttpLayer.HttpLayerFactory) X500NameBuilder(org.bouncycastle.asn1.x500.X500NameBuilder) WireMock.get(com.github.tomakehurst.wiremock.client.WireMock.get) ValueSource(org.junit.jupiter.params.provider.ValueSource) JcaX509v3CertificateBuilder(org.bouncycastle.cert.jcajce.JcaX509v3CertificateBuilder) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) WireMock.verify(com.github.tomakehurst.wiremock.client.WireMock.verify) JdkHttpClientHttpLayer.maybeThrowClientServerException(com.rabbitmq.http.client.JdkHttpClientHttpLayer.maybeThrowClientServerException) ServerConnector(org.eclipse.jetty.server.ServerConnector) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) ChronoUnit(java.time.temporal.ChronoUnit) WireMock.stubFor(com.github.tomakehurst.wiremock.client.WireMock.stubFor) WireMock.urlEqualTo(com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo) Connector(org.eclipse.jetty.server.Connector) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HttpLayerFactory(com.rabbitmq.http.client.HttpLayer.HttpLayerFactory) URI(java.net.URI) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 3 with HttpLayerFactory

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();
}
Also used : Test(org.junit.jupiter.api.Test) JdkHttpClientHttpLayer.authorization(com.rabbitmq.http.client.JdkHttpClientHttpLayer.authorization) Duration(java.time.Duration) JdkHttpClientHttpLayer(com.rabbitmq.http.client.JdkHttpClientHttpLayer) ClientParameters(com.rabbitmq.http.client.ClientParameters) Client(com.rabbitmq.http.client.Client) HttpLayerFactory(com.rabbitmq.http.client.HttpLayer.HttpLayerFactory) ClientParameters(com.rabbitmq.http.client.ClientParameters) HttpLayerFactory(com.rabbitmq.http.client.HttpLayer.HttpLayerFactory) Client(com.rabbitmq.http.client.Client) Test(org.junit.jupiter.api.Test)

Example 4 with HttpLayerFactory

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();
    }
}
Also used : HttpLayerFactory(com.rabbitmq.http.client.HttpLayer.HttpLayerFactory) URI(java.net.URI) WireMockServer(com.github.tomakehurst.wiremock.WireMockServer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

HttpLayerFactory (com.rabbitmq.http.client.HttpLayer.HttpLayerFactory)4 WireMockServer (com.github.tomakehurst.wiremock.WireMockServer)3 URI (java.net.URI)3 WireMock (com.github.tomakehurst.wiremock.client.WireMock)2 WireMock.aResponse (com.github.tomakehurst.wiremock.client.WireMock.aResponse)2 WireMock.exactly (com.github.tomakehurst.wiremock.client.WireMock.exactly)2 WireMock.get (com.github.tomakehurst.wiremock.client.WireMock.get)2 WireMock.getRequestedFor (com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor)2 WireMock.stubFor (com.github.tomakehurst.wiremock.client.WireMock.stubFor)2 WireMock.urlEqualTo (com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo)2 WireMock.urlPathMatching (com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching)2 WireMock.verify (com.github.tomakehurst.wiremock.client.WireMock.verify)2 WireMockConfiguration.wireMockConfig (com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig)2 JdkHttpClientHttpLayer.maybeThrowClientServerException (com.rabbitmq.http.client.JdkHttpClientHttpLayer.maybeThrowClientServerException)2 IOException (java.io.IOException)2 BigInteger (java.math.BigInteger)2 ServerSocket (java.net.ServerSocket)2 KeyPair (java.security.KeyPair)2 KeyPairGenerator (java.security.KeyPairGenerator)2 KeyStore (java.security.KeyStore)2