Search in sources :

Example 91 with Client

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

the class SslConnectorConfigurationTest method testSSLAuth1.

/**
     * Test to see that SSLHandshakeException is thrown when client don't have
     * trusted key.
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testSSLAuth1() throws Exception {
    final SSLContext sslContext = getSslContext();
    final ClientConfig cc = new ClientConfig().connectorProvider(new ApacheConnectorProvider());
    final Client client = ClientBuilder.newBuilder().withConfig(cc).sslContext(sslContext).build();
    WebTarget target = client.target(Server.BASE_URI).register(LoggingFeature.class);
    boolean caught = false;
    try {
        target.path("/").request().get(String.class);
    } catch (Exception e) {
        caught = true;
    }
    assertTrue(caught);
}
Also used : ApacheConnectorProvider(org.glassfish.jersey.apache.connector.ApacheConnectorProvider) SSLContext(javax.net.ssl.SSLContext) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 92 with Client

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

the class SslConnectorConfigurationTest method testSSLWithAuth.

/**
     * Test to see that the correct Http status is returned.
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testSSLWithAuth() throws Exception {
    final SSLContext sslContext = getSslContext();
    final ClientConfig cc = new ClientConfig().connectorProvider(connectorProvider);
    final Client client = ClientBuilder.newBuilder().withConfig(cc).sslContext(sslContext).build();
    // client basic auth demonstration
    client.register(HttpAuthenticationFeature.basic("user", "password"));
    final WebTarget target = client.target(Server.BASE_URI).register(LoggingFeature.class);
    final Response response = target.path("/").request().get(Response.class);
    assertEquals(200, response.getStatus());
}
Also used : Response(javax.ws.rs.core.Response) SSLContext(javax.net.ssl.SSLContext) WebTarget(javax.ws.rs.client.WebTarget) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 93 with Client

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

the class SslConnectorHostnameVerifierTest method testHostnameVerifierApplied.

/**
     * Test to apply {@link HostnameVerifier} along with SSL in the predefined connectors
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testHostnameVerifierApplied() throws Exception {
    // Grizzly and Jetty connectors don't support Hostname Verification
    if (isExcluded(Arrays.asList(GrizzlyConnectorProvider.class, JettyConnectorProvider.class))) {
        return;
    }
    final Client client = ClientBuilder.newBuilder().withConfig(new ClientConfig().connectorProvider(connectorProvider)).register(HttpAuthenticationFeature.basic("user", "password")).hostnameVerifier(new CustomHostnameVerifier()).sslContext(getSslContext()).build();
    try {
        client.target(Server.BASE_URI).request().get(Response.class);
        fail("HostnameVerifier was not applied.");
    } catch (ProcessingException pex) {
        CustomHostnameVerifier.HostnameVerifierException hve = getHVE(pex);
        if (hve != null) {
            assertEquals(CustomHostnameVerifier.EX_VERIFIER_MESSAGE, hve.getMessage());
        } else {
            fail("Invalid wrapped exception.");
        }
    }
}
Also used : GrizzlyConnectorProvider(org.glassfish.jersey.grizzly.connector.GrizzlyConnectorProvider) JettyConnectorProvider(org.glassfish.jersey.jetty.connector.JettyConnectorProvider) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 94 with Client

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

the class SslHttpUrlConnectorTest method testSSLWithCustomSocketFactory.

/**
     * Test to see that the correct Http status is returned.
     *
     * @throws Exception in case of a test failure.
     */
@Test
public void testSSLWithCustomSocketFactory() throws Exception {
    final SSLContext sslContext = getSslContext();
    final CustomSSLSocketFactory socketFactory = new CustomSSLSocketFactory(sslContext);
    final ClientConfig cc = new ClientConfig().connectorProvider(new HttpUrlConnectorProvider().connectionFactory(new HttpUrlConnectorProvider.ConnectionFactory() {

        @Override
        public HttpURLConnection getConnection(final URL url) throws IOException {
            HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
            connection.setSSLSocketFactory(socketFactory);
            return connection;
        }
    }));
    final Client client = ClientBuilder.newBuilder().withConfig(cc).sslContext(sslContext).register(HttpAuthenticationFeature.basic("user", "password")).register(LoggingFeature.class).build();
    final Response response = client.target(Server.BASE_URI).path("/").request().get();
    assertEquals(200, response.getStatus());
    assertTrue(socketFactory.isVisited());
}
Also used : Response(javax.ws.rs.core.Response) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) LoggingFeature(org.glassfish.jersey.logging.LoggingFeature) SSLContext(javax.net.ssl.SSLContext) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

Example 95 with Client

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

the class ConstrainedToTest method testClientWithProviderClasses.

@Test
public void testClientWithProviderClasses() {
    Client client = ClientBuilder.newClient(new ClientConfig(ClientFilterConstrainedToServer.class, ClientFilterConstrainedToClient.class, ClientFilter.class));
    _testFilters(client);
}
Also used : Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

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