Search in sources :

Example 51 with Client

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);
    }
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) ClientRequest(org.glassfish.jersey.client.ClientRequest) ProcessingException(javax.ws.rs.ProcessingException) Test(org.junit.Test)

Example 52 with Client

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)));
}
Also used : Collection(java.util.Collection) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 53 with Client

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"));
}
Also used : WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 54 with Client

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/");
}
Also used : Client(javax.ws.rs.client.Client)

Example 55 with Client

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));
}
Also used : Response(javax.ws.rs.core.Response) 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