use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.
the class TestCapRepFilter method testStreamResponse.
@Test
public void testStreamResponse() throws IOException {
Path dirPath = Files.createTempDirectory("caprep-test");
StreamCountFilter after = new StreamCountFilter();
StreamCountFilter before = new StreamCountFilter();
CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
FilterChain fc = FilterChains.createStreamChain(StreamFilterAdapters.adaptRestFilter(lastCallFilter), before, _filter, after);
RestRequest myRequest = new RestRequestBuilder(URI.create("/req1")).setEntity("123".getBytes()).build();
RestResponse myResponse = new RestResponseBuilder().setStatus(201).setEntity("321".getBytes()).build();
_filter.capture(dirPath.toString());
RequestContext requestContext = new RequestContext();
FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), requestContext, FilterUtil.emptyWireAttrs());
FilterUtil.fireStreamResponse(fc, Messages.toStreamResponse(myResponse), requestContext, FilterUtil.emptyWireAttrs());
Assert.assertEquals(after.getStreamReqCount(), 1);
Assert.assertEquals(after.getStreamResCount(), 1);
Assert.assertEquals(before.getStreamReqCount(), 1);
Assert.assertEquals(before.getStreamResCount(), 1);
lastCallFilter.reset();
_filter.passThrough();
FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest));
Assert.assertEquals(after.getStreamReqCount(), 2);
Assert.assertEquals(after.getStreamResCount(), 1);
Assert.assertEquals(before.getStreamReqCount(), 2);
Assert.assertEquals(before.getStreamResCount(), 1);
Assert.assertNull(lastCallFilter.getLastRes());
_filter.replay(dirPath.toString());
FilterUtil.fireSimpleStreamRequest(fc);
Assert.assertNull(lastCallFilter.getLastRes());
FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), new RequestContext(), FilterUtil.emptyWireAttrs());
Assert.assertEquals(lastCallFilter.getLastRes(), myResponse);
Assert.assertEquals(after.getStreamReqCount(), 3);
Assert.assertEquals(after.getStreamResCount(), 1);
Assert.assertEquals(before.getStreamReqCount(), 4);
Assert.assertEquals(before.getStreamResCount(), 2);
}
use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.
the class TestCapRepFilter method testStreamException.
@Test
public void testStreamException() throws IOException {
Path dirPath = Files.createTempDirectory("caprep-test");
CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
FilterChain fc = FilterChains.createStreamChain(StreamFilterAdapters.adaptRestFilter(lastCallFilter), _filter);
RestRequest myRequest = new RestRequestBuilder(URI.create("/req1")).setEntity("123".getBytes()).build();
RestResponse myErrorResponse = new RestResponseBuilder().setStatus(400).setEntity("321".getBytes()).build();
RestException myRestException = new RestException(myErrorResponse);
_filter.capture(dirPath.toString());
RequestContext requestContext = new RequestContext();
FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), requestContext, FilterUtil.emptyWireAttrs());
FilterUtil.fireStreamError(fc, Messages.toStreamException(myRestException), requestContext, FilterUtil.emptyWireAttrs());
lastCallFilter.reset();
_filter.replay(dirPath.toString());
FilterUtil.fireSimpleStreamRequest(fc);
Assert.assertNull(lastCallFilter.getLastErr());
FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest));
Assert.assertTrue(lastCallFilter.getLastErr() instanceof RestException);
Assert.assertEquals(((RestException) lastCallFilter.getLastErr()).getResponse(), myErrorResponse);
}
use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.
the class TestReplaceableFilter method testStreamingPassThrough.
@Test
public void testStreamingPassThrough() {
ReplaceableFilter filter = getFilter();
CountRequestChunksFilter countRequestChunksFilter = new CountRequestChunksFilter();
CountResponseChunksFilter countResponseChunksFilter = new CountResponseChunksFilter();
FilterChain filterChain = FilterChains.empty().addLast(countResponseChunksFilter).addLast(filter).addLast(countRequestChunksFilter);
int requestChunks = 100;
int responseChunks = 200;
StreamRequest streamRequest = new StreamRequestBuilder(URI.create("/test")).build(EntityStreams.newEntityStream(new Writer() {
private WriteHandle _wh;
int _count = 0;
@Override
public void onInit(WriteHandle wh) {
_wh = wh;
}
@Override
public void onWritePossible() {
while (_wh.remaining() > 0) {
if (_count < requestChunks) {
_wh.write(ByteString.copy(new byte[10]));
_count++;
} else {
_wh.done();
}
}
}
@Override
public void onAbort(Throwable e) {
}
}));
StreamResponse streamResponse = new StreamResponseBuilder().build(EntityStreams.newEntityStream(new Writer() {
private WriteHandle _wh;
int _count = 0;
@Override
public void onInit(WriteHandle wh) {
_wh = wh;
}
@Override
public void onWritePossible() {
while (_wh.remaining() > 0) {
if (_count < responseChunks) {
_wh.write(ByteString.copy(new byte[10]));
_count++;
} else {
_wh.done();
}
}
}
@Override
public void onAbort(Throwable e) {
}
}));
FilterUtil.fireStreamRequest(filterChain, streamRequest);
FilterUtil.fireStreamResponse(filterChain, streamResponse, new RequestContext(), new HashMap<>());
Assert.assertEquals(countRequestChunksFilter.getRequestChunks(), requestChunks);
Assert.assertEquals(countResponseChunksFilter.getResponseChunks(), responseChunks);
}
use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.
the class AbstractReplayFilterTest method testReplayWithNoMatch.
@Test
public void testReplayWithNoMatch() {
final RestRequest req = request();
final RestResponse res = response();
final CaptureLastCallFilter captureFilter = new CaptureLastCallFilter();
final FilterChain fc = getFilterChain().addFirstRest(captureFilter);
FilterUtil.fireUntypedRequestResponse(fc, req, res);
Assert.assertEquals(res, captureFilter.getLastRes());
}
use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.
the class TestServerTimeout method setup.
@BeforeClass
public void setup() throws IOException {
_clientFactory = new HttpClientFactory();
Map<String, Object> clientProperties = new HashMap<String, Object>();
clientProperties.put(HttpClientFactory.HTTP_REQUEST_TIMEOUT, String.valueOf(SERVER_IOHANDLER_TIMEOUT * 20));
clientProperties.put(HttpClientFactory.HTTP_POOL_MIN_SIZE, "1");
clientProperties.put(HttpClientFactory.HTTP_POOL_SIZE, "1");
_client = new TransportClientAdapter(_clientFactory.getClient(clientProperties), true);
final Map<URI, StreamRequestHandler> handlers = new HashMap<URI, StreamRequestHandler>();
handlers.put(BUGGY_SERVER_URI, new BuggyRequestHandler());
handlers.put(THROW_BUT_SHOULD_NOT_TIMEOUT_URI, new ThrowHandler());
handlers.put(BUGGY_FILTER_URI, new NormalHandler());
TransportDispatcher transportDispatcher = new TransportDispatcher() {
@Override
public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
throw new UnsupportedOperationException("This dispatcher only supports stream");
}
@Override
public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
StreamRequestHandler handler = handlers.get(req.getURI());
if (handler != null) {
handler.handleRequest(req, requestContext, new TransportCallbackAdapter<StreamResponse>(callback));
} else {
req.getEntityStream().setReader(new DrainReader());
callback.onResponse(TransportResponseImpl.<StreamResponse>error(new IllegalStateException("Handler not found for URI " + req.getURI())));
}
}
};
FilterChain filterChain = FilterChains.createStreamChain(new BuggyFilter());
_server = new HttpServerFactory(filterChain).createRAPServer(PORT, transportDispatcher, SERVER_IOHANDLER_TIMEOUT, true);
_server.start();
}
Aggregations