Search in sources :

Example 16 with RestFilter

use of com.linkedin.r2.filter.message.rest.RestFilter 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 17 with RestFilter

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

the class TestFilterChainImpl method testFilterOrderTwoChains.

@Test
public void testFilterOrderTwoChains() {
    AtomicInteger count = new AtomicInteger(0);
    CheckOrderFilter filter1 = new CheckOrderFilter(count);
    CheckOrderFilter filter2 = new CheckOrderFilter(count);
    List<RestFilter> restFilters = new ArrayList<RestFilter>();
    restFilters.add(filter1);
    restFilters.add(filter2);
    List<StreamFilter> streamFilters = new ArrayList<StreamFilter>();
    streamFilters.add(filter1);
    streamFilters.add(filter2);
    final FilterChain fc = FilterChains.create(restFilters, streamFilters);
    fireRestRequest(fc);
    Assert.assertEquals(filter1.getOrder(), 1);
    Assert.assertEquals(filter2.getOrder(), 2);
    fireRestResponse(fc);
    Assert.assertEquals(filter1.getOrder(), 4);
    Assert.assertEquals(filter2.getOrder(), 3);
    fireRestError(fc);
    Assert.assertEquals(filter1.getOrder(), 6);
    Assert.assertEquals(filter2.getOrder(), 5);
    count.set(0);
    fireStreamRequest(fc);
    Assert.assertEquals(filter1.getOrder(), 1);
    Assert.assertEquals(filter2.getOrder(), 2);
    fireStreamResponse(fc);
    Assert.assertEquals(filter1.getOrder(), 4);
    Assert.assertEquals(filter2.getOrder(), 3);
    fireStreamError(fc);
    Assert.assertEquals(filter1.getOrder(), 6);
    Assert.assertEquals(filter2.getOrder(), 5);
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FilterChain(com.linkedin.r2.filter.FilterChain) ArrayList(java.util.ArrayList) StreamFilter(com.linkedin.r2.filter.message.stream.StreamFilter) Test(org.testng.annotations.Test)

Example 18 with RestFilter

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

the class TestServerRetryFilter method testNestedException.

@Test
public void testNestedException() {
    String retryMessage = "this is a retry";
    ServerRetryFilter retryFilter = new ServerRetryFilter();
    RestFilter captureFilter = new RestFilter() {

        @Override
        public void onRestError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs, NextFilter<RestRequest, RestResponse> nextFilter) {
            Assert.assertEquals(wireAttrs.get(R2Constants.RETRY_MESSAGE_ATTRIBUTE_KEY), retryMessage);
        }
    };
    FilterChain filterChain = FilterChains.createRestChain(captureFilter, retryFilter);
    Throwable nestedException = new RetriableRequestException(retryMessage);
    for (int i = 0; i < 5; i++) {
        nestedException = new RuntimeException(nestedException);
    }
    FilterUtil.fireRestError(filterChain, new RestException(null, nestedException), new HashMap<String, String>());
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) ServerRetryFilter(com.linkedin.r2.filter.transport.ServerRetryFilter) NextFilter(com.linkedin.r2.filter.NextFilter) FilterChain(com.linkedin.r2.filter.FilterChain) RestException(com.linkedin.r2.message.rest.RestException) RetriableRequestException(com.linkedin.r2.RetriableRequestException) RequestContext(com.linkedin.r2.message.RequestContext) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 19 with RestFilter

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

the class TestReplaceableFilter method testSetAndGet.

@Test
public void testSetAndGet() {
    final ReplaceableFilter filter = getFilter();
    final RestFilter newFilter = new PassThroughFilter();
    Assert.assertTrue(!filter.getFilter().equals(newFilter));
    filter.setFilter(newFilter);
    Assert.assertEquals(newFilter, filter.getFilter());
}
Also used : RestFilter(com.linkedin.r2.filter.message.rest.RestFilter) ReplaceableFilter(com.linkedin.r2.caprep.ReplaceableFilter) PassThroughFilter(com.linkedin.r2.caprep.PassThroughFilter) BaseFilterTest(com.linkedin.r2.testutils.filter.BaseFilterTest) Test(org.testng.annotations.Test)

Aggregations

RestFilter (com.linkedin.r2.filter.message.rest.RestFilter)19 Test (org.testng.annotations.Test)16 FilterChain (com.linkedin.r2.filter.FilterChain)15 RequestContext (com.linkedin.r2.message.RequestContext)14 RestRequest (com.linkedin.r2.message.rest.RestRequest)9 Callback (com.linkedin.common.callback.Callback)8 ByteString (com.linkedin.data.ByteString)8 NextFilter (com.linkedin.r2.filter.NextFilter)8 RestResponse (com.linkedin.r2.message.rest.RestResponse)8 FullEntityReader (com.linkedin.r2.message.stream.entitystream.FullEntityReader)8 Map (java.util.Map)8 HashMap (java.util.HashMap)6 RestException (com.linkedin.r2.message.rest.RestException)4 StreamException (com.linkedin.r2.message.stream.StreamException)4 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)4 RetriableRequestException (com.linkedin.r2.RetriableRequestException)3 ServerRetryFilter (com.linkedin.r2.filter.transport.ServerRetryFilter)3 RemoteInvocationException (com.linkedin.r2.RemoteInvocationException)2 ClientRetryFilter (com.linkedin.r2.filter.transport.ClientRetryFilter)2 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)2