Search in sources :

Example 46 with Client

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

the class JerseyInvocationTest method buildInvocationWithHeaders.

private JerseyInvocation buildInvocationWithHeaders(final MultivaluedMap<String, Object> headers) {
    final Client c = ClientBuilder.newClient();
    final Invocation.Builder builder = c.target("http://localhost:8080/mypath").request();
    return (JerseyInvocation) builder.header("unexpected-header", "unexpected-header").headers(headers).buildGet();
}
Also used : Invocation(javax.ws.rs.client.Invocation) Client(javax.ws.rs.client.Client)

Example 47 with Client

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

the class JerseyInvocationTest method overrideHttpMethodBasedComplianceCheckTest.

/**
     * Checks that presence of request entity fo HTTP DELETE method does not fail in Jersey.
     * Instead, the request is propagated up to HttpURLConnection, where it fails with
     * {@code ProtocolException}.
     * <p/>
     * See also JERSEY-1711.
     *
     * @see #overrideHttpMethodBasedComplianceCheckNegativeTest()
     */
@Test
public void overrideHttpMethodBasedComplianceCheckTest() {
    final Client c1 = ClientBuilder.newClient().property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true);
    try {
        c1.target("http://localhost:8080/myPath").request().method("DELETE", Entity.text("body"));
        fail("ProcessingException expected.");
    } catch (final ProcessingException ex) {
        assertThat(ex.getCause().getClass(), anyOf(CoreMatchers.<Class<?>>equalTo(ProtocolException.class), CoreMatchers.<Class<?>>equalTo(ConnectException.class)));
    }
    final Client c2 = ClientBuilder.newClient();
    try {
        c2.target("http://localhost:8080/myPath").request().property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true).method("DELETE", Entity.text("body"));
        fail("ProcessingException expected.");
    } catch (final ProcessingException ex) {
        assertThat(ex.getCause().getClass(), anyOf(CoreMatchers.<Class<?>>equalTo(ProtocolException.class), CoreMatchers.<Class<?>>equalTo(ConnectException.class)));
    }
    final Client c3 = ClientBuilder.newClient().property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, false);
    try {
        c3.target("http://localhost:8080/myPath").request().property(ClientProperties.SUPPRESS_HTTP_COMPLIANCE_VALIDATION, true).method("DELETE", Entity.text("body"));
        fail("ProcessingException expected.");
    } catch (final ProcessingException ex) {
        assertThat(ex.getCause().getClass(), anyOf(CoreMatchers.<Class<?>>equalTo(ProtocolException.class), CoreMatchers.<Class<?>>equalTo(ConnectException.class)));
    }
}
Also used : ProtocolException(java.net.ProtocolException) Client(javax.ws.rs.client.Client) ProcessingException(javax.ws.rs.ProcessingException) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 48 with Client

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

the class ClientProviderInstanceInjectionTest method test.

/**
     * Tests that instance of a feature or other provider will not be injected on the client-side.
     */
@Test
public void test() {
    final Client client = ClientBuilder.newBuilder().register(new MyFilterFeature()).register(new MyInjecteeBinder()).build();
    final Response response = client.target("http://foo.bar").request().get();
    assertEquals(200, response.getStatus());
    assertEquals("null,null", response.readEntity(String.class));
}
Also used : Response(javax.ws.rs.core.Response) Client(javax.ws.rs.client.Client) Test(org.junit.Test)

Example 49 with Client

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

the class CsrfProtectionFilterTest method setUp.

@Before
public void setUp() {
    Client client = ClientBuilder.newClient(new ClientConfig(CsrfProtectionFilter.class).connectorProvider(new TestConnector()));
    invBuilder = client.target(UriBuilder.fromUri("/").build()).request();
}
Also used : Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) Before(org.junit.Before)

Example 50 with Client

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

the class EncodingFilterTest method testContentEncodingViaFeature.

@Test
public void testContentEncodingViaFeature() {
    Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new TestConnector()).register(new EncodingFeature("gzip", GZipEncoder.class, DeflateEncoder.class)));
    Invocation.Builder invBuilder = client.target(UriBuilder.fromUri("/").build()).request();
    Response r = invBuilder.post(Entity.entity("Hello world", MediaType.TEXT_PLAIN_TYPE));
    assertEquals("deflate,gzip,x-gzip", r.getHeaderString(ACCEPT_ENCODING));
    assertEquals("gzip", r.getHeaderString(CONTENT_ENCODING));
}
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)

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