@Before public void setUp() { JerseyClient client = JerseyClientFactory.clientBuilder().transport(new TestTransport()).build(); client.configuration().register(CsrfProtectionFilter.class); invBuilder = client.target(UriBuilder.fromUri("/").build()).request(); }
@Test public void testCookies() { ClientConfig cc = new ClientConfig(); cc.connectorProvider(new ApacheConnectorProvider()); JerseyClient client = JerseyClientBuilder.createClient(cc); WebTarget r = client.target(getBaseUri()); assertEquals("NO-COOKIE", r.request().get(String.class)); assertEquals("value", r.request().get(String.class)); final ApacheConnector connector = (ApacheConnector) client.getConfiguration().getConnector(); assertNotNull(connector.getCookieStore().getCookies()); assertEquals(1, connector.getCookieStore().getCookies().size()); assertEquals("value", connector.getCookieStore().getCookies().get(0).getValue()); }
@After public void tearDown() throws Exception { for (LifeCycle lifeCycle : environment.lifecycle().getManagedObjects()) { lifeCycle.stop(); } assertThat(client.isClosed()).isTrue(); }
@Test public void when_no_read_timeout_override_then_client_request_times_out() { thrown.expect(ProcessingException.class); thrown.expectCause(any(SocketTimeoutException.class)); client.target(testUri + "/long_running").request().get(); }
@Test public void when_read_timeout_override_created_then_client_requests_completes_successfully() { client .target(testUri + "/long_running") .property(ClientProperties.READ_TIMEOUT, SLEEP_TIME_IN_MILLIS * 2) .request() .get(); }
@Test public void testDisabledCookies() { ClientConfig cc = new ClientConfig(); cc.property(ApacheClientProperties.DISABLE_COOKIES, true); cc.connectorProvider(new ApacheConnectorProvider()); JerseyClient client = JerseyClientBuilder.createClient(cc); WebTarget r = client.target(getBaseUri()); assertEquals("NO-COOKIE", r.request().get(String.class)); assertEquals("NO-COOKIE", r.request().get(String.class)); final ApacheConnector connector = (ApacheConnector) client.getConfiguration().getConnector(); if (connector.getCookieStore() != null) { assertTrue(connector.getCookieStore().getCookies().isEmpty()); } else { assertNull(connector.getCookieStore()); } }
@Test public void when_configuration_overridden_to_disallow_redirects_temporary_redirect_status_returned() { assertThat( client .target(testUri + "/redirect") .property(ClientProperties.FOLLOW_REDIRECTS, false) .request() .get(Response.class) .getStatus()) .isEqualTo(HttpStatus.SC_TEMPORARY_REDIRECT); }
@Test public void when_jersey_client_runtime_is_garbage_collected_apache_client_is_not_closed() { for (int j = 0; j < 5; j++) { System.gc(); // We actually want GC here final String response = client .target(testUri + "/long_running") .property(ClientProperties.READ_TIMEOUT, SLEEP_TIME_IN_MILLIS * 2) .request() .get(String.class); assertThat(response).isEqualTo("success"); } }
/** * In first assertion we prove, that a request takes no longer than: <em>request_time < * connect_timeout + error_margin</em> (1) * * <p>In the second we show that if we set <b>connect_timeout</b> to <b>set_connect_timeout + * increase + error_margin</b> then <em>request_time > connect_timeout + increase + * error_margin</em> (2) * * <p> * * <p>Now, (1) and (2) can hold at the same time if then connect_timeout update was successful. */ @Test public void connect_timeout_override_changes_how_long_it_takes_for_a_connection_to_timeout() { // before override WebTarget target = client.target(NON_ROUTABLE_ADDRESS); // This can't be tested without a real connection try { target.request().get(Response.class); } catch (ProcessingException e) { if (e.getCause() instanceof HttpHostConnectException) { return; } } assertThatConnectionTimeoutFor(target) .isLessThan(DEFAULT_CONNECT_TIMEOUT_IN_MILLIS + ERROR_MARGIN_IN_MILLIS); // after override final int newTimeout = DEFAULT_CONNECT_TIMEOUT_IN_MILLIS + INCREASE_IN_MILLIS + ERROR_MARGIN_IN_MILLIS; final WebTarget newTarget = target.property(ClientProperties.CONNECT_TIMEOUT, newTimeout); assertThatConnectionTimeoutFor(newTarget).isGreaterThan(newTimeout); }
@Test public void when_no_override_then_redirected_request_successfully_redirected() { assertThat(client.target(testUri + "/redirect").request().get(String.class)) .isEqualTo("redirected"); }
static { JerseyClient jerseyClient = JerseyClientBuilder.createClient(); jerseyWebTarget = jerseyClient.target("http://localhost:8081/api/v1"); }