Search in sources :

Example 41 with Client

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

the class EncodingFilterTest method testContentEncodingSkippedForNoEntity.

@Test
public void testContentEncodingSkippedForNoEntity() {
    Client client = ClientBuilder.newClient(new ClientConfig(EncodingFilter.class, GZipEncoder.class, DeflateEncoder.class).property(ClientProperties.USE_ENCODING, "gzip").connectorProvider(new TestConnector()));
    Invocation.Builder invBuilder = client.target(UriBuilder.fromUri("/").build()).request();
    Response r = invBuilder.get();
    assertEquals("deflate,gzip,x-gzip", r.getHeaderString(ACCEPT_ENCODING));
    assertNull(r.getHeaderString(CONTENT_ENCODING));
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) DeflateEncoder(org.glassfish.jersey.message.DeflateEncoder) Invocation(javax.ws.rs.client.Invocation) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) GZipEncoder(org.glassfish.jersey.message.GZipEncoder) Test(org.junit.Test)

Example 42 with Client

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

the class HttpBasicAuthFilterTest method testGet.

@Test
public void testGet() {
    Client client = ClientBuilder.newClient(new ClientConfig(HttpAuthenticationFeature.basic("Uzivatelske jmeno", "Heslo")).connectorProvider(new TestConnector()));
    Invocation.Builder invBuilder = client.target(UriBuilder.fromUri("/").build()).request();
    Response r = invBuilder.get();
    assertEquals("Basic " + Base64.encodeAsString("Uzivatelske jmeno:Heslo"), r.getHeaderString(HttpHeaders.AUTHORIZATION));
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) Test(org.junit.Test)

Example 43 with Client

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

the class HttpBasicAuthFilterTest method testBlankUsernamePassword.

@Test
public void testBlankUsernamePassword() {
    Client client = ClientBuilder.newClient(new ClientConfig(HttpAuthenticationFeature.basic("", "")).connectorProvider(new TestConnector()));
    Invocation.Builder invBuilder = client.target(UriBuilder.fromUri("/").build()).request();
    Response r = invBuilder.get();
    assertEquals("Basic " + Base64.encodeAsString(":"), r.getHeaderString(HttpHeaders.AUTHORIZATION));
}
Also used : ClientResponse(org.glassfish.jersey.client.ClientResponse) Response(javax.ws.rs.core.Response) Invocation(javax.ws.rs.client.Invocation) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) Test(org.junit.Test)

Example 44 with Client

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

the class CustomConnectorTest method testConnectorProviderPreservedOnClientConfigCopy.

/**
     * Reproducer for JERSEY-2318.
     *
     * The test verifies that the {@link org.glassfish.jersey.client.spi.ConnectorProvider} configured
     * on one client instance is transferred to another client instance when the new client instance is
     * created from the original client instance configuration.
     */
@Test
public void testConnectorProviderPreservedOnClientConfigCopy() {
    final ClientConfig clientConfig = new ClientConfig().connectorProvider(new TestConnectorProvider());
    final Client client = ClientBuilder.newClient(clientConfig);
    try {
        Response response;
        final WebTarget target = client.target("http://wherever.org/");
        response = target.request().get();
        // let's first verify we are using the test loop-back connector.
        assertThat(response.getStatus(), equalTo(TestConnector.TEST_LOOPBACK_CODE));
        assertThat(response.readEntity(String.class), equalTo("http://wherever.org/"));
        // and now with the filter...
        target.register(TestClientFilter.class);
        response = target.request().get();
        // check if the connector provider has been propagated:
        assertThat(response.getStatus(), equalTo(TestConnector.TEST_LOOPBACK_CODE));
        // check if the filter has been invoked:
        assertThat(response.readEntity(String.class), equalTo("http://wherever.org/filtered"));
    } finally {
        client.close();
    }
}
Also used : Response(javax.ws.rs.core.Response) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 45 with Client

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

the class JerseyInvocationTest method testClearHeader.

/**
     * Regression test for JERSEY-2562.
     */
@Test
public void testClearHeader() {
    final Client client = ClientBuilder.newClient();
    final Invocation.Builder builder = client.target("http://localhost:8080/mypath").request();
    final JerseyInvocation invocation = (JerseyInvocation) builder.header("foo", "bar").header("foo", null).header("bar", "foo").buildGet();
    final MultivaluedMap<String, Object> headers = invocation.request().getHeaders();
    assertThat(headers.size(), is(1));
    assertThat(headers.keySet(), hasItem("bar"));
}
Also used : Invocation(javax.ws.rs.client.Invocation) 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