Search in sources :

Example 81 with Client

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

the class BufferingTest method makeRequest.

private void makeRequest(ClientConfig cc, String entity, String expected) {
    Client client = ClientBuilder.newClient(cc);
    WebTarget target = client.target(UriBuilder.fromUri(getBaseUri()).path("resource").build());
    Response response = target.request().post(Entity.entity(entity, MediaType.TEXT_PLAIN_TYPE));
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(expected, response.readEntity(String.class));
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 82 with Client

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

the class BufferingTest method testWithChunkEncodingPerRequest.

/**
     * Tests that {@link org.glassfish.jersey.client.ClientProperties#REQUEST_ENTITY_PROCESSING} can be defined
     * per request with different values.
     */
private void testWithChunkEncodingPerRequest(ClientConfig cc) {
    cc.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.CHUNKED);
    cc.property(ClientProperties.CHUNKED_ENCODING_SIZE, 3000);
    Client client = ClientBuilder.newClient(cc);
    WebTarget target = client.target(UriBuilder.fromUri(getBaseUri()).path("resource").build());
    String entity = getVeryLongString();
    Response response = target.request().post(Entity.entity(entity, MediaType.TEXT_PLAIN_TYPE));
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals("chunked", response.readEntity(String.class));
    response = target.property(ClientProperties.REQUEST_ENTITY_PROCESSING, RequestEntityProcessing.BUFFERED).request().post(Entity.entity(entity, MediaType.TEXT_PLAIN_TYPE));
    Assert.assertEquals(200, response.getStatus());
    Assert.assertEquals(String.valueOf(entity.length()), response.readEntity(String.class));
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client)

Example 83 with Client

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

the class ClientBufferingDisabledTest method testDisableBufferingWithFixedLengthViaMethod.

/**
     * 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#useFixedLengthStreaming()} to enable
     * fix length streaming on {@code HttpURLConnection}.
     */
@Test
public void testDisableBufferingWithFixedLengthViaMethod() {
    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().useFixedLengthStreaming();
    ClientConfig clientConfig = new ClientConfig().connectorProvider(connectorProvider);
    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 84 with Client

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

the class ClientPreInitTest method testReusingPreinitializedConfig.

@Test
public void testReusingPreinitializedConfig() {
    Client client = ClientBuilder.newClient();
    client.register(TestReader.class);
    final WebTarget target = client.target(super.getBaseUri()).path("resource");
    target.register(MyResponseFilter.class);
    ((JerseyWebTarget) target).preInitialize();
    assertTrue(TestReader.initialized);
    final WebTarget childTarget = target.path("child");
    final Response response = childTarget.request().get();
    checkResponse(response, "child:<null>");
    final Response resourceResponse = target.request().get();
    checkResponse(resourceResponse, "resource:<null>");
}
Also used : Response(javax.ws.rs.core.Response) JerseyWebTarget(org.glassfish.jersey.client.JerseyWebTarget) 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 85 with Client

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

the class ClientPreInitTest method testReusingPreinitializedConfig2.

@Test
public void testReusingPreinitializedConfig2() {
    Client client = ClientBuilder.newClient();
    client.register(TestReader.class);
    client.register(MyResponseFilter.class);
    assertFalse(TestReader.initialized);
    ((JerseyClient) client).preInitialize();
    assertTrue(TestReader.initialized);
    final WebTarget target = client.target(super.getBaseUri()).path("resource");
    final WebTarget childTarget = target.path("child");
    final Response response = childTarget.request().get();
    checkResponse(response, "child:<null>");
    final Response resourceResponse = target.request().get();
    checkResponse(resourceResponse, "resource:<null>");
}
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) JerseyClient(org.glassfish.jersey.client.JerseyClient) 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