use of javax.ws.rs.client.ClientResponseFilter 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.ClientResponseFilter 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.ClientResponseFilter in project jersey by jersey.
the class ResponseReadAndBufferEntityTest method testBufferEntityReadsOriginalStreamTest.
@Test
public void testBufferEntityReadsOriginalStreamTest() throws Exception {
final WebTarget target = target("response/corrupted");
final CorruptableInputStream cis = new CorruptableInputStream();
target.register(new ClientResponseFilter() {
@Override
public void filter(final ClientRequestContext requestContext, final ClientResponseContext responseContext) {
responseContext.setEntityStream(cis);
}
});
final Response response = target.request().buildGet().invoke();
try {
cis.setCorruptRead(true);
response.bufferEntity();
fail("ProcessingException expected.");
} catch (ProcessingException pe) {
// expected
assertThat(pe.getCause(), instanceOf(IOException.class));
}
}
use of javax.ws.rs.client.ClientResponseFilter in project jersey by jersey.
the class ResponseReadAndBufferEntityTest method testReadBufferedEntityMultipleTimesFromStreamThatFailsToClose.
/**
* 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-2 : Read buffered entity multiple times and then try to close the context
*/
@Test
public void testReadBufferedEntityMultipleTimesFromStreamThatFailsToClose() 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);
response.bufferEntity();
assertEquals("Close not invoked on underlying input stream.", 1, entityStream.getCloseCount());
String entity;
entity = response.readEntity(String.class, null);
assertThat("Unexpected response.", entity.toString(), equalTo(Resource.ENTITY));
entity = response.readEntity(String.class, null);
assertThat("Unexpected response.", entity.toString(), equalTo(Resource.ENTITY));
// Close should not fail and should be idempotent
response.close();
response.close();
response.close();
assertEquals("Close invoked too many times on underlying input stream.", 1, entityStream.getCloseCount());
try {
// UC-2.1 : Try to read a buffered entity from a closed context
response.readEntity(String.class, null);
fail("IllegalStateException expected when reading from a closed buffered context.");
// UC-2.1 : END
} catch (IllegalStateException ise) {
// expected
}
// UC-2 : END
entityStream.reset();
}
use of javax.ws.rs.client.ClientResponseFilter in project jersey by jersey.
the class ResponseReadAndBufferEntityTest method testReadUnbufferedEntityFromStreamThatFailsToClose.
/**
* 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-1 : Read unbuffered entity and then try to close the context
*/
@Test
public void testReadUnbufferedEntityFromStreamThatFailsToClose() 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);
// Read entity should not fail - we silently consume the underlying IOException from closed input stream.
final String entity = response.readEntity(String.class, null);
assertThat("Unexpected response.", entity.toString(), equalTo(Resource.ENTITY));
assertEquals("Close not invoked on underlying input stream.", 1, entityStream.getCloseCount());
// Close should not fail and should be idempotent
response.close();
response.close();
response.close();
assertEquals("Close invoked too many times on underlying input stream.", 1, entityStream.getCloseCount());
try {
// UC-1.1 : Try to read an unbuffered entity from a closed context
response.readEntity(String.class, null);
fail("IllegalStateException expected when reading from a closed context.");
// UC-1.1 : END
} catch (IllegalStateException ise) {
// expected
}
}
Aggregations