Search in sources :

Example 41 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestStreamFilterAdapters method testResponseFilterAdapterChangeError.

@Test
public void testResponseFilterAdapterChangeError() {
    FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
        }

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            if (ex instanceof RestException) {
                RestResponse res = ((RestException) ex).getResponse();
                String newEntityStr = res.getEntity().asString("UTF8").replace('1', '0');
                nextFilter.onError(new RestException((res.builder().setEntity(newEntityStr.getBytes()).build())), requestContext, wireAttrs);
            } else {
                nextFilter.onError(new IllegalStateException(), requestContext, wireAttrs);
            }
        }
    });
    fc.onStreamError(simpleStreamException("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    Throwable capturedEx = _beforeFilter.getThrowable();
    Assert.assertTrue(capturedEx instanceof StreamException);
    ((StreamException) capturedEx).getResponse().getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("should not happen");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "02345");
        }
    }));
    fc.onStreamError(new IllegalArgumentException(), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    capturedEx = _beforeFilter.getThrowable();
    Assert.assertTrue(capturedEx instanceof IllegalStateException);
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) FilterChain(com.linkedin.r2.filter.FilterChain) RestException(com.linkedin.r2.message.rest.RestException) ByteString(com.linkedin.data.ByteString) StreamException(com.linkedin.r2.message.stream.StreamException) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 42 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestStreamFilterAdapters method testResponseFilterAdapterPassThrough.

@Test
public void testResponseFilterAdapterPassThrough() {
    FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestResponse(RestResponse res, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onResponse(res, requestContext, wireAttrs);
        }

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onError(ex, requestContext, wireAttrs);
        }
    });
    fc.onStreamResponse(simpleStreamResponse("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    StreamResponse capturedResponse = _beforeFilter.getResponse();
    capturedResponse.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("should not happen");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "12345");
        }
    }));
    fc.onStreamError(simpleStreamException("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    Throwable capturedEx = _beforeFilter.getThrowable();
    Assert.assertTrue(capturedEx instanceof StreamException);
    ((StreamException) capturedEx).getResponse().getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("should not happen");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "12345");
        }
    }));
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) FilterChain(com.linkedin.r2.filter.FilterChain) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) StreamException(com.linkedin.r2.message.stream.StreamException) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 43 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestStreamFilterAdapters method testRequestFilterAdapterCallsOnResponse.

@Test
public void testRequestFilterAdapterCallsOnResponse() {
    FilterChain fc = adaptAndCreateFilterChain(new RestFilter() {

        @Override
        public void onRestRequest(RestRequest req, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            nextFilter.onResponse(simpleRestResponse(req.getEntity().asString("UTF8")), requestContext, wireAttrs);
        }
    });
    fc.onStreamRequest(simpleStreamRequest("12345"), FilterUtil.emptyRequestContext(), FilterUtil.emptyWireAttrs());
    StreamResponse capturedReq = _beforeFilter.getResponse();
    capturedReq.getEntityStream().setReader(new FullEntityReader(new Callback<ByteString>() {

        @Override
        public void onError(Throwable e) {
            Assert.fail("shouldn't have error");
        }

        @Override
        public void onSuccess(ByteString result) {
            Assert.assertEquals(result.asString("UTF8"), "12345");
        }
    }));
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) ByteString(com.linkedin.data.ByteString) FilterChain(com.linkedin.r2.filter.FilterChain) StreamResponse(com.linkedin.r2.message.stream.StreamResponse) ByteString(com.linkedin.data.ByteString) FullEntityReader(com.linkedin.r2.message.stream.entitystream.FullEntityReader) RestRequest(com.linkedin.r2.message.rest.RestRequest) Callback(com.linkedin.common.callback.Callback) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 44 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestFilterChainImpl method testStreamResponseFilter.

@Test
public void testStreamResponseFilter() {
    final RestCountFilter restCountFilter = new RestCountFilter();
    final StreamCountFilter streamCountFilter = new StreamCountFilter();
    FilterChain fc = FilterChains.createRestChain(restCountFilter);
    fc = fc.addLast(streamCountFilter);
    fireStreamResponse(fc);
    assertRestCounts(0, 0, 0, restCountFilter);
    assertStreamCounts(0, 1, 0, streamCountFilter);
}
Also used : StreamCountFilter(com.linkedin.r2.testutils.filter.StreamCountFilter) FilterChain(com.linkedin.r2.filter.FilterChain) RestCountFilter(com.linkedin.r2.testutils.filter.RestCountFilter) Test(org.testng.annotations.Test)

Example 45 with FilterChain

use of com.linkedin.r2.filter.FilterChain in project rest.li by linkedin.

the class TestFilterChainImpl method testRestResponseFilter.

@Test
public void testRestResponseFilter() {
    final RestCountFilter restCountFilter = new RestCountFilter();
    final StreamCountFilter streamCountFilter = new StreamCountFilter();
    FilterChain fc = FilterChains.createRestChain(restCountFilter);
    fc = fc.addLast(streamCountFilter);
    fireRestResponse(fc);
    assertRestCounts(0, 1, 0, restCountFilter);
    assertStreamCounts(0, 0, 0, streamCountFilter);
}
Also used : StreamCountFilter(com.linkedin.r2.testutils.filter.StreamCountFilter) FilterChain(com.linkedin.r2.filter.FilterChain) RestCountFilter(com.linkedin.r2.testutils.filter.RestCountFilter) Test(org.testng.annotations.Test)

Aggregations

FilterChain (com.linkedin.r2.filter.FilterChain)45 Test (org.testng.annotations.Test)38 RequestContext (com.linkedin.r2.message.RequestContext)28 RestRequest (com.linkedin.r2.message.rest.RestRequest)20 RestResponse (com.linkedin.r2.message.rest.RestResponse)17 RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)15 HashMap (java.util.HashMap)12 ByteString (com.linkedin.data.ByteString)11 Map (java.util.Map)11 RestException (com.linkedin.r2.message.rest.RestException)10 RestCountFilter (com.linkedin.r2.testutils.filter.RestCountFilter)10 StreamCountFilter (com.linkedin.r2.testutils.filter.StreamCountFilter)10 NextFilter (com.linkedin.r2.filter.NextFilter)9 Callback (com.linkedin.common.callback.Callback)8 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)8 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)8 CaptureLastCallFilter (com.linkedin.r2.testutils.filter.CaptureLastCallFilter)7 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)6 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)5