use of javax.ws.rs.client.Client in project jersey by jersey.
the class OauthClientAuthorizationFlowTest method testOAuthClientFlow.
@Test
public void testOAuthClientFlow() throws Exception {
final String uri = getBaseUri().toString();
final OAuth1AuthorizationFlow authFlow = OAuth1ClientSupport.builder(new ConsumerCredentials("dpf43f3p2l4k3l03", "kd94hf93k423kf44")).timestamp("1191242090").nonce("hsu94j3884jdopsl").signatureMethod("PLAINTEXT").authorizationFlow(uri + "request_token", uri + "access_token", uri + "authorize").enableLogging().build();
// Check we have correct authorization URI.
final String authorizationUri = authFlow.start();
assertThat(authorizationUri, containsString("authorize?oauth_token=hh5s93j4hdidpola"));
// For the purpose of the test I need parameters (and there is no way how to do it now).
final Field paramField = authFlow.getClass().getDeclaredField("parameters");
paramField.setAccessible(true);
final OAuth1Parameters params = (OAuth1Parameters) paramField.get(authFlow);
// Update parameters.
params.timestamp("1191242092").nonce("dji430splmx33448");
final AccessToken accessToken = authFlow.finish();
assertThat(accessToken, equalTo(new AccessToken("nnch734d00sl2jdk", "pfkkdhi9sl3r4s00")));
// Update parameters before creating a feature (i.e. changing signature method).
params.nonce("kllo9940pd9333jh").signatureMethod("HMAC-SHA1").timestamp("1191242096");
// Check Authorized Client.
final Client flowClient = authFlow.getAuthorizedClient().register(LoggingFeature.class);
String responseEntity = flowClient.target(uri).path("/photos").queryParam("file", "vacation.jpg").queryParam("size", "original").request().get(String.class);
assertThat("Flow Authorized Client", responseEntity, equalTo("PHOTO"));
// Check Feature.
final Client featureClient = ClientBuilder.newClient().register(authFlow.getOAuth1Feature()).register(LoggingFeature.class);
responseEntity = featureClient.target(uri).path("/photos").queryParam("file", "vacation.jpg").queryParam("size", "original").request().get(String.class);
assertThat("Feature Client", responseEntity, equalTo("PHOTO"));
}
use of javax.ws.rs.client.Client in project javaee7-samples by javaee-samples.
the class FilterServletTest method filtered_servlet_should_return_enhanced_foobar_text.
@Test
@RunAsClient
public void filtered_servlet_should_return_enhanced_foobar_text() throws MalformedURLException {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(URI.create(new URL(base, "filtered/TestServlet").toExternalForm()));
Response response = target.request().get();
Assert.assertThat(response.readEntity(String.class), is(equalTo("foo--bar--bar")));
}
use of javax.ws.rs.client.Client in project javaee7-samples by javaee-samples.
the class FilterServletTest method standard_servlet_should_return_simple_text.
@Test
@RunAsClient
public void standard_servlet_should_return_simple_text() throws MalformedURLException {
Client client = ClientBuilder.newClient();
WebTarget target = client.target(URI.create(new URL(base, "TestServlet").toExternalForm()));
Response response = target.request().get();
Assert.assertThat(response.readEntity(String.class), is(equalTo("bar")));
}
use of javax.ws.rs.client.Client in project jersey by jersey.
the class UnderlyingCookieStoreAccessTest method testCookieStoreInstanceAccess.
@Test
public void testCookieStoreInstanceAccess() {
final Client client = ClientBuilder.newClient(new ClientConfig().connectorProvider(new ApacheConnectorProvider()));
final CookieStore csOnClient = ApacheConnectorProvider.getCookieStore(client);
// important: the web target instance in this test must be only created AFTER the client has been pre-initialized
// (see org.glassfish.jersey.client.Initializable.preInitialize method). This is here achieved by calling the
// connector provider's static getCookieStore method above.
final WebTarget target = client.target("http://localhost/");
final CookieStore csOnTarget = ApacheConnectorProvider.getCookieStore(target);
assertNotNull("CookieStore instance set on JerseyClient should not be null.", csOnClient);
assertNotNull("CookieStore instance set on JerseyWebTarget should not be null.", csOnTarget);
assertSame("CookieStore instance set on JerseyClient should be the same instance as the one set on JerseyWebTarget" + "(provided the target instance has not been further configured).", csOnClient, csOnTarget);
}
use of javax.ws.rs.client.Client in project jersey by jersey.
the class RetryHandlerTest method testRetryGet.
@Test
public void testRetryGet() throws IOException {
ClientConfig cc = new ClientConfig();
cc.connectorProvider(new ApacheConnectorProvider());
cc.property(ApacheClientProperties.RETRY_HANDLER, (HttpRequestRetryHandler) (exception, executionCount, context) -> true);
cc.property(ClientProperties.READ_TIMEOUT, READ_TIMEOUT_MS);
Client client = ClientBuilder.newClient(cc);
WebTarget r = client.target(getBaseUri());
assertEquals("GET", r.request().get(String.class));
}
Aggregations