use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.
the class ResponseReadAndBufferEntityTest method testCloseUnreadResponseWithEntityStreamThatFailsToClose.
/**
* This method tests behavior of input stream operations in case the underlying input stream throws an exception when closed.
* Reproducer for JRFCAF-1344.
* <p>
* UC-3 : Try to close the response - underlying exception should be reported.
*/
@Test
public void testCloseUnreadResponseWithEntityStreamThatFailsToClose() throws Exception {
final CorruptableInputStream entityStream = new CorruptableInputStream();
final WebTarget target = target("response/corrupted");
target.register(new ClientResponseFilter() {
@Override
public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext) {
responseContext.setEntityStream(entityStream);
}
});
final Response response = target.request().buildGet().invoke();
entityStream.setCorruptClose(true);
try {
response.close();
fail("ProcessingException expected when closing the context and underlying stream throws an IOException.");
} catch (ProcessingException pe) {
assertThat(pe.getCause(), instanceOf(IOException.class));
}
}
use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.
the class Jersey2878ITCase method configureClient.
@Override
protected void configureClient(ClientConfig config) {
ClientResponseFilter trackInputStreams = new ClientResponseFilter() {
@Override
public void filter(ClientRequestContext requestContext, ClientResponseContext responseContext) throws IOException {
responseInputStreams.add(responseContext.getEntityStream());
}
};
config.register(trackInputStreams);
}
use of javax.ws.rs.client.ClientRequestContext in project jersey by jersey.
the class OAuth2Test method testFlow.
private void testFlow(final boolean isArray) {
ClientIdentifier clientId = new ClientIdentifier(CLIENT_PUBLIC, CLIENT_SECRET);
final String authUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("authorization").build().toString();
final String accessTokenUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("access-token").build().toString();
final String refreshTokenUri = UriBuilder.fromUri(getBaseUri()).path("oauth").path("refresh-token").build().toString();
final String state = STATE;
final Client client = ClientBuilder.newClient();
if (isArray) {
client.register(new ClientRequestFilter() {
@Override
public void filter(final ClientRequestContext requestContext) throws IOException {
requestContext.getHeaders().putSingle("isArray", true);
}
});
}
final OAuth2CodeGrantFlow.Builder builder = OAuth2ClientSupport.authorizationCodeGrantFlowBuilder(clientId, authUri, accessTokenUri);
final OAuth2CodeGrantFlow flow = builder.client(client).refreshTokenUri(refreshTokenUri).property(OAuth2CodeGrantFlow.Phase.AUTHORIZATION, "readOnly", "true").property(OAuth2CodeGrantFlow.Phase.AUTHORIZATION, OAuth2Parameters.STATE, state).scope("contact").build();
final String finalAuthorizationUri = flow.start();
final Response response = ClientBuilder.newClient().target(finalAuthorizationUri).request().get();
assertEquals(200, response.getStatus());
final String code = response.readEntity(String.class);
assertEquals(CODE, code);
final TokenResult result = flow.finish(code, state);
assertEquals("access-token-aab999f", result.getAccessToken());
assertEquals(new Long(3600), result.getExpiresIn());
assertEquals("access-token", result.getTokenType());
final TokenResult refreshResult = flow.refreshAccessToken(result.getRefreshToken());
assertEquals("access-token-new", refreshResult.getAccessToken());
assertEquals(new Long(3600), refreshResult.getExpiresIn());
assertEquals("access-token", refreshResult.getTokenType());
if (isArray) {
final Collection<String> array = (Collection<String>) refreshResult.getAllProperties().get("access_token");
assertThat(array.size(), is(1));
assertThat(array, hasItem("access-token-new"));
}
}
use of javax.ws.rs.client.ClientRequestContext in project robozonky by RoboZonky.
the class AuthenticatedFilterTest method hasToken.
@Test
void hasToken() throws URISyntaxException {
final ClientRequestContext crc = mock(ClientRequestContext.class);
when(crc.getUri()).thenReturn(new URI("http://somewhere"));
when(crc.getHeaders()).thenReturn(new MultivaluedHashMap<>());
getTestedFilter().filter(crc);
assertThat(crc.getHeaders().getFirst("Authorization")).isEqualTo("Bearer " + String.valueOf(AuthenticatedFilterTest.TOKEN.getAccessToken()));
}
use of javax.ws.rs.client.ClientRequestContext in project robozonky by RoboZonky.
the class AuthenticatedFilterTest method changes400to401.
@Test
void changes400to401() throws IOException {
final int expectedCode = 400;
final ClientRequestContext ctx = mock(ClientRequestContext.class);
final ClientResponseContext ctx2 = mock(ClientResponseContext.class);
final ZonkyApiToken token = new ZonkyApiToken("", "", 299);
when(ctx2.hasEntity()).thenReturn(true);
when(ctx2.getHeaders()).thenReturn(new MultivaluedMapImpl<>());
when(ctx2.getEntityStream()).thenReturn(c(token));
when(ctx2.getStatusInfo()).thenReturn(Response.Status.fromStatusCode(expectedCode));
when(ctx2.getStatus()).thenReturn(expectedCode);
final RoboZonkyFilter filter = new AuthenticatedFilter(token);
filter.filter(ctx, ctx2);
verify(ctx2, times(1)).setStatus(401);
}
Aggregations