use of javax.ws.rs.client.Client in project jersey by jersey.
the class EncodingFilterTest method testClosingClientResponseStreamRetrievedByResponseOnError.
/**
* Reproducer for JERSEY-2028.
*
* @see #testClosingClientResponseStreamRetrievedByValueOnError
*/
@Test
public void testClosingClientResponseStreamRetrievedByResponseOnError() {
final TestInputStream responseStream = new TestInputStream();
Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new TestConnector() {
@Override
public ClientResponse apply(ClientRequest requestContext) throws ProcessingException {
final ClientResponse responseContext = new ClientResponse(Response.Status.OK, requestContext);
responseContext.header(CONTENT_ENCODING, "gzip");
responseContext.setEntityStream(responseStream);
return responseContext;
}
}).register(new EncodingFeature(GZipEncoder.class, DeflateEncoder.class)));
final Response response = client.target(UriBuilder.fromUri("/").build()).request().get();
assertEquals(Response.Status.OK.getStatusCode(), response.getStatus());
assertEquals("gzip", response.getHeaderString(CONTENT_ENCODING));
try {
response.readEntity(String.class);
fail("Exception caused by invalid gzip stream expected.");
} catch (ProcessingException ex) {
assertTrue("Response input stream not closed when exception is thrown.", responseStream.isClosed);
}
}
use of javax.ws.rs.client.Client in project jersey by jersey.
the class ShutdownHookLeakTest method testShutdownHookDoesNotLeak.
@SuppressWarnings("unchecked")
@Test
public void testShutdownHookDoesNotLeak() throws Exception {
final Client client = ClientBuilder.newClient();
final WebTarget target = client.target("http://example.com");
final Collection shutdownHooks = getShutdownHooks(client);
for (int i = 0; i < ITERATIONS; i++) {
// Create/Initialize client runtime.
target.property("Washington", "Irving").request().property("how", "now").buildGet().property("Irving", "Washington");
}
System.gc();
int notEnqueued = 0;
int notNull = 0;
for (final Object o : shutdownHooks) {
if (((WeakReference<JerseyClient.ShutdownHook>) o).get() != null) {
notNull++;
}
if (!((WeakReference<JerseyClient.ShutdownHook>) o).isEnqueued()) {
notEnqueued++;
}
}
assertThat("Non-null shutdown hook references count should not copy number of property invocation", // 66 % seems like a reasonable threshold for this test to keep it stable
notNull, is(lessThan(THRESHOLD)));
assertThat("Shutdown hook references count not enqueued in the ReferenceQueue should not copy number of property invocation", // 66 % seems like a reasonable threshold for this test to keep it stable
notEnqueued, is(lessThan(THRESHOLD)));
}
use of javax.ws.rs.client.Client in project jersey by jersey.
the class WebTargetPropertiesTest method testPropagation.
@Test
public void testPropagation() {
Client c = ClientBuilder.newBuilder().newClient();
c.property("a", "val");
WebTarget w1 = c.target("http://a");
w1.property("b", "val");
WebTarget w2 = w1.path("c");
w2.property("c", "val");
assertTrue(c.getConfiguration().getProperties().containsKey("a"));
assertTrue(w1.getConfiguration().getProperties().containsKey("a"));
assertTrue(w2.getConfiguration().getProperties().containsKey("a"));
assertFalse(c.getConfiguration().getProperties().containsKey("b"));
assertTrue(w1.getConfiguration().getProperties().containsKey("b"));
assertTrue(w2.getConfiguration().getProperties().containsKey("b"));
assertFalse(c.getConfiguration().getProperties().containsKey("c"));
assertFalse(w1.getConfiguration().getProperties().containsKey("c"));
assertTrue(w2.getConfiguration().getProperties().containsKey("c"));
w2.property("a", null);
assertTrue(c.getConfiguration().getProperties().containsKey("a"));
assertTrue(w1.getConfiguration().getProperties().containsKey("a"));
assertFalse(w2.getConfiguration().getProperties().containsKey("a"));
}
use of javax.ws.rs.client.Client in project jersey by jersey.
the class HttpUrlConnectorTest method createNonRoutableTarget.
private WebTarget createNonRoutableTarget() {
Client client = ClientBuilder.newClient();
client.property(ClientProperties.CONNECT_TIMEOUT, TimeoutBASE);
// the following address should not be routable, connections will timeout
return client.target("http://10.255.255.254/");
}
use of javax.ws.rs.client.Client in project jersey by jersey.
the class JerseyClientTest method testCustomBinders.
@Test
public void testCustomBinders() {
final CustomBinder binder = new CustomBinder();
Client client = ClientBuilder.newClient().register(binder).register(CustomProvider.class);
Response resp = client.target("test").request().get();
assertEquals("Foo", resp.readEntity(String.class));
}
Aggregations