use of org.glassfish.jersey.client.ClientConfig 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);
}
use of org.glassfish.jersey.client.ClientConfig 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());
}
use of org.glassfish.jersey.client.ClientConfig 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.");
}
}
}
use of org.glassfish.jersey.client.ClientConfig 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());
}
use of org.glassfish.jersey.client.ClientConfig in project jersey by jersey.
the class AsyncTest method testClientThreadPool.
@Test
@Ignore("Unstable test.")
public void testClientThreadPool() throws Exception {
final AsyncInvoker invoker = ClientBuilder.newClient(new ClientConfig().property(ClientProperties.ASYNC_THREADPOOL_SIZE, 9)).target(getBaseUri()).path("threadpool").request().async();
final CountDownLatch latch = new CountDownLatch(100);
final int threadCount = Thread.activeCount();
final List<Thread> threads = new ArrayList<Thread>(20);
for (int i = 0; i < 20; i++) {
threads.add(new Thread(new Runnable() {
@Override
public void run() throws RuntimeException {
for (int i = 0; i < 5; i++) {
try {
assertThat(invoker.get().get().readEntity(String.class), equalTo("GET"));
assertThat(Thread.activeCount() - threadCount - 20, lessThanOrEqualTo(10));
latch.countDown();
} catch (final InterruptedException e) {
fail();
} catch (final ExecutionException e) {
fail();
}
}
}
}));
}
for (final Thread thread : threads) {
thread.start();
}
assertTrue(latch.await(10 * getAsyncTimeoutMultiplier(), TimeUnit.SECONDS));
}
Aggregations