Search in sources :

Example 86 with Client

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

the class ClientPreInitTest method testNonInitialized.

@Test
public void testNonInitialized() {
    Client client = ClientBuilder.newClient();
    client.register(MyResponseFilter.class);
    client.register(TestReader.class);
    final WebTarget target = client.target(super.getBaseUri()).path("resource");
    assertFalse(TestReader.initialized);
    final Response resourceResponse = target.request().get();
    checkResponse(resourceResponse, "resource:<null>");
    assertTrue(TestReader.initialized);
}
Also used : Response(javax.ws.rs.core.Response) JerseyWebTarget(org.glassfish.jersey.client.JerseyWebTarget) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) JerseyClient(org.glassfish.jersey.client.JerseyClient) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 87 with Client

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

the class ClientBufferingDisabledTest method testDisableBufferingWithFixedLengthViaProperty.

/**
     * Test that buffering can be disabled with {@link HttpURLConnection}. By default, the
     * {@code HttpURLConnection} buffers the output entity in order to calculate the
     * Content-length request attribute. This cause problems for large entities.
     * <p>
     * This test uses {@link HttpUrlConnectorProvider#USE_FIXED_LENGTH_STREAMING} to enable
     * fix length streaming on {@code HttpURLConnection}.
     */
@Test
public void testDisableBufferingWithFixedLengthViaProperty() {
    postLatch = new CountDownLatch(1);
    // This IS sends out 10 chunks and waits whether they were received on the server. This tests
    // whether the buffering is disabled.
    InputStream is = getInputStream();
    final HttpUrlConnectorProvider connectorProvider = new HttpUrlConnectorProvider();
    ClientConfig clientConfig = new ClientConfig().connectorProvider(connectorProvider);
    clientConfig.property(HttpUrlConnectorProvider.USE_FIXED_LENGTH_STREAMING, true);
    Client client = ClientBuilder.newClient(clientConfig);
    final Response response = client.target(getBaseUri()).path("resource").request().header(HttpHeaders.CONTENT_LENGTH, LENGTH).post(Entity.entity(is, MediaType.APPLICATION_OCTET_STREAM));
    Assert.assertEquals(200, response.getStatus());
    final long count = response.readEntity(long.class);
    Assert.assertEquals("Unexpected content length received.", LENGTH, count);
}
Also used : Response(javax.ws.rs.core.Response) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) InputStream(java.io.InputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 88 with Client

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

the class ClientBufferingDisabledTest method testDisableBufferingWithChunkEncoding.

/**
     * Test that buffering can be disabled with {@link HttpURLConnection}. By default, the
     * {@code HttpURLConnection} buffers the output entity in order to calculate the
     * Content-length request attribute. This cause problems for large entities.
     * <p>
     * In Jersey 1.x chunk encoding with {@code HttpURLConnection} was causing bugs
     * which occurred from time to time. This looks to be a case also in Jersey 2.x. This test
     * has failed unpredictably on some machines. Therefore it is disabled now.
     * </p>
     */
@Test
@Ignore("fails unpredictable (see javadoc)")
public void testDisableBufferingWithChunkEncoding() {
    postLatch = new CountDownLatch(1);
    // This IS sends out 10 chunks and waits whether they were received on the server. This tests
    // whether the buffering is disabled.
    InputStream is = getInputStream();
    final HttpUrlConnectorProvider connectorProvider = new HttpUrlConnectorProvider().chunkSize(CHUNK);
    ClientConfig clientConfig = new ClientConfig().connectorProvider(connectorProvider);
    Client client = ClientBuilder.newClient(clientConfig);
    final Response response = client.target(getBaseUri()).path("resource").request().post(Entity.entity(is, MediaType.APPLICATION_OCTET_STREAM));
    Assert.assertEquals(200, response.getStatus());
    final long count = response.readEntity(long.class);
    Assert.assertEquals("Unexpected content length received.", LENGTH, count);
}
Also used : Response(javax.ws.rs.core.Response) HttpUrlConnectorProvider(org.glassfish.jersey.client.HttpUrlConnectorProvider) InputStream(java.io.InputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Ignore(org.junit.Ignore) Test(org.junit.Test) JerseyTest(org.glassfish.jersey.test.JerseyTest)

Example 89 with Client

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

the class ClientDestroyTest method testClientInvokePreDestroyMethodOnProviderClass.

@Test
public void testClientInvokePreDestroyMethodOnProviderClass() throws Exception {
    final Client client = ClientBuilder.newClient().register(MyFilter.class).register(MyReader.class).register(MyFeature.class);
    assertThat(client.target(getBaseUri()).request().get(String.class), is("reader-resource-bar"));
    checkDestroyed(false);
    client.close();
    checkDestroyed(true);
}
Also used : Client(javax.ws.rs.client.Client) JerseyClient(org.glassfish.jersey.client.JerseyClient) JerseyTest(org.glassfish.jersey.test.JerseyTest) Test(org.junit.Test)

Example 90 with Client

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

the class ClipboardTest method testClipboard.

@Test
public void testClipboard() throws Exception {
    final Client client = client();
    final WebTarget clipboard = client.target(getBaseUri()).path(App.ROOT_PATH);
    Response response;
    response = clipboard.request("text/plain").get();
    assertEquals(204, response.getStatus());
    response = clipboard.request("text/plain").put(Entity.text("Hello"));
    assertEquals(204, response.getStatus());
    assertEquals("Hello", clipboard.request("text/plain").get(String.class));
    response = clipboard.request("text/plain").post(Entity.text(" World!"));
    assertEquals(200, response.getStatus());
    assertEquals("Hello World!", clipboard.request("text/plain").get(String.class));
    response = clipboard.request("text/plain").delete();
    assertEquals(204, response.getStatus());
    assertEquals(204, clipboard.request("text/plain").get().getStatus());
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) JerseyTest(org.glassfish.jersey.test.JerseyTest) 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