Search in sources :

Example 16 with Client

use of javax.ws.rs.client.Client in project dropwizard by dropwizard.

the class DropwizardSSLConnectionSocketFactoryTest method shouldBeOkIfHostnameVerificationOnAndServerHostnameDoesntMatchAndNoopVerifierSpecified.

@Test
public void shouldBeOkIfHostnameVerificationOnAndServerHostnameDoesntMatchAndNoopVerifierSpecified() throws Exception {
    final Client client = new JerseyClientBuilder(TLS_APP_RULE.getEnvironment()).using(jerseyClientConfiguration).using(new NoopHostnameVerifier()).build("bad_host_noop_verifier_working");
    final Response response = client.target(String.format("https://localhost:%d", TLS_APP_RULE.getPort(3))).request().get();
    assertThat(response.getStatus()).isEqualTo(200);
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) Client(javax.ws.rs.client.Client) JerseyClientBuilder(io.dropwizard.client.JerseyClientBuilder) Test(org.junit.Test)

Example 17 with Client

use of javax.ws.rs.client.Client in project dropwizard by dropwizard.

the class JerseyClientBuilderTest method usesTheGivenThreadPool.

@Test
public void usesTheGivenThreadPool() throws Exception {
    final Client client = builder.using(executorService, objectMapper).build("test");
    for (Object o : client.getConfiguration().getInstances()) {
        if (o instanceof DropwizardExecutorProvider) {
            final DropwizardExecutorProvider provider = (DropwizardExecutorProvider) o;
            assertThat(provider.getExecutorService()).isSameAs(executorService);
        }
    }
}
Also used : Client(javax.ws.rs.client.Client) RxClient(org.glassfish.jersey.client.rx.RxClient) Test(org.junit.Test)

Example 18 with Client

use of javax.ws.rs.client.Client in project dropwizard by dropwizard.

the class JerseyClientBuilderTest method usesTheGivenThreadPoolAndEnvironmentsObjectMapper.

@Test
public void usesTheGivenThreadPoolAndEnvironmentsObjectMapper() throws Exception {
    final Client client = builder.using(environment).using(executorService).build("test");
    for (Object o : client.getConfiguration().getInstances()) {
        if (o instanceof DropwizardExecutorProvider) {
            final DropwizardExecutorProvider provider = (DropwizardExecutorProvider) o;
            assertThat(provider.getExecutorService()).isSameAs(executorService);
        }
    }
}
Also used : Client(javax.ws.rs.client.Client) RxClient(org.glassfish.jersey.client.rx.RxClient) Test(org.junit.Test)

Example 19 with Client

use of javax.ws.rs.client.Client in project dropwizard by dropwizard.

the class JerseyClientIntegrationTest method testFilterOnAWebTarget.

/**
     * Test for ConnectorProvider idempotency
     */
@Test
public void testFilterOnAWebTarget() {
    httpServer.createContext("/test", httpExchange -> {
        try {
            httpExchange.getResponseHeaders().add(HttpHeaders.CONTENT_TYPE, TEXT_PLAIN);
            httpExchange.sendResponseHeaders(200, 0);
            httpExchange.getResponseBody().write("Hello World!".getBytes(StandardCharsets.UTF_8));
        } finally {
            httpExchange.close();
        }
    });
    httpServer.start();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Client jersey = new JerseyClientBuilder(new MetricRegistry()).using(executor, JSON_MAPPER).build("test-jersey-client");
    String uri = "http://127.0.0.1:" + httpServer.getAddress().getPort() + "/test";
    WebTarget target = jersey.target(uri);
    target.register(new LoggingFeature());
    String firstResponse = target.request().buildGet().invoke().readEntity(String.class);
    assertThat(firstResponse).isEqualTo("Hello World!");
    String secondResponse = jersey.target(uri).request().buildGet().invoke().readEntity(String.class);
    assertThat(secondResponse).isEqualTo("Hello World!");
    executor.shutdown();
    jersey.close();
}
Also used : MetricRegistry(com.codahale.metrics.MetricRegistry) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) ExecutorService(java.util.concurrent.ExecutorService) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 20 with Client

use of javax.ws.rs.client.Client in project dropwizard by dropwizard.

the class JerseyClientIntegrationTest method testGet.

@Test
public void testGet() {
    httpServer.createContext("/player", httpExchange -> {
        try {
            assertThat(httpExchange.getRequestURI().getQuery()).isEqualTo("id=21");
            httpExchange.getResponseHeaders().add(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON);
            httpExchange.sendResponseHeaders(200, 0);
            httpExchange.getResponseBody().write(JSON_MAPPER.createObjectNode().put("email", "john@doe.me").put("name", "John Doe").toString().getBytes(StandardCharsets.UTF_8));
        } finally {
            httpExchange.close();
        }
    });
    httpServer.start();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Client jersey = new JerseyClientBuilder(new MetricRegistry()).using(executor, JSON_MAPPER).using(new JerseyClientConfiguration()).build("jersey-test");
    Response response = jersey.target("http://127.0.0.1:" + httpServer.getAddress().getPort() + "/player?id=21").request().buildGet().invoke();
    assertThat(response.getHeaderString(HttpHeaders.CONTENT_TYPE)).isEqualTo(APPLICATION_JSON);
    assertThat(response.getHeaderString(TRANSFER_ENCODING)).isEqualTo(CHUNKED);
    Person person = response.readEntity(Person.class);
    assertThat(person.email).isEqualTo("john@doe.me");
    assertThat(person.name).isEqualTo("John Doe");
    executor.shutdown();
    jersey.close();
}
Also used : Response(javax.ws.rs.core.Response) MetricRegistry(com.codahale.metrics.MetricRegistry) ExecutorService(java.util.concurrent.ExecutorService) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Aggregations

Client (javax.ws.rs.client.Client)227 Test (org.junit.Test)160 WebTarget (javax.ws.rs.client.WebTarget)96 Response (javax.ws.rs.core.Response)87 JerseyTest (org.glassfish.jersey.test.JerseyTest)76 ClientConfig (org.glassfish.jersey.client.ClientConfig)71 URL (java.net.URL)20 ClientResponse (org.glassfish.jersey.client.ClientResponse)19 JerseyClientBuilder (io.dropwizard.client.JerseyClientBuilder)18 Before (org.junit.Before)17 Invocation (javax.ws.rs.client.Invocation)15 ResourceConfig (org.glassfish.jersey.server.ResourceConfig)14 IOException (java.io.IOException)12 ProcessingException (javax.ws.rs.ProcessingException)12 HttpServer (org.glassfish.grizzly.http.server.HttpServer)10 URI (java.net.URI)9 JerseyClient (org.glassfish.jersey.client.JerseyClient)9 PrintWriter (java.io.PrintWriter)8 CountDownLatch (java.util.concurrent.CountDownLatch)8 SSLContext (javax.net.ssl.SSLContext)8