Search in sources :

Example 76 with RestResponseBuilder

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

the class TestRestReplayFilter method testReplayWithRestException.

@Test
public void testReplayWithRestException() {
    final RestRequest req = request();
    final RestResponse res = new RestResponseBuilder().setStatus(RestStatus.NOT_FOUND).build();
    final CaptureLastCallFilter captureFilter = new CaptureLastCallFilter();
    final FilterChain fc = getFilterChain().addFirstRest(captureFilter);
    // Record a response for the request we will fire
    getDb().record(req, res);
    // We should be able to fire just the request - the response should be replayed from the
    // capture we set up above. The response should be a RestException since the status code is 404.
    FilterUtil.fireUntypedRequest(fc, req);
    Assert.assertTrue(captureFilter.getLastErr() instanceof RestException);
    Assert.assertEquals(res, ((RestException) captureFilter.getLastErr()).getResponse());
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) CaptureLastCallFilter(com.linkedin.r2.testutils.filter.CaptureLastCallFilter) RestResponse(com.linkedin.r2.message.rest.RestResponse) FilterChain(com.linkedin.r2.filter.FilterChain) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestException(com.linkedin.r2.message.rest.RestException) Test(org.testng.annotations.Test)

Example 77 with RestResponseBuilder

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

the class LoadBalancerEchoServer method getExceptionTypeFromRequest.

public RestResponse getExceptionTypeFromRequest(String request) {
    if (request.contains("PORT:" + _port)) {
        Pattern pattern = Pattern.compile("EXCEPTION=(\\w+)", Pattern.CASE_INSENSITIVE);
        Matcher matcher = pattern.matcher(request);
        int status = -9999;
        while (matcher.find()) {
            if (matcher.group(1).contains("NOT_FOUND")) {
                status = RestStatus.NOT_FOUND;
            } else if (matcher.group(1).contains("INTERNAL_SERVER_ERROR")) {
                status = RestStatus.INTERNAL_SERVER_ERROR;
            } else if (matcher.group(1).contains("BAD_REQUEST")) {
                status = RestStatus.BAD_REQUEST;
            }
            final RestResponse res = new RestResponseBuilder().setStatus(status).build();
            final RestException restException = new RestException(res);
            return restException.getResponse();
        }
    }
    return null;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestException(com.linkedin.r2.message.rest.RestException)

Example 78 with RestResponseBuilder

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

the class TestJetty404 method setup.

@BeforeClass
public void setup() throws IOException {
    _clientFactory = new HttpClientFactory.Builder().build();
    _client = new TransportClientAdapter(_clientFactory.getClient(Collections.<String, String>emptyMap()), true);
    _server = new HttpServerFactory().createH2cServer(PORT, "/correct-path", 50, new TransportDispatcher() {

        @Override
        public void handleRestRequest(RestRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<RestResponse> callback) {
            callback.onResponse(TransportResponseImpl.success(new RestResponseBuilder().build()));
        }

        @Override
        public void handleStreamRequest(StreamRequest req, Map<String, String> wireAttrs, RequestContext requestContext, TransportCallback<StreamResponse> callback) {
            req.getEntityStream().setReader(new DrainReader());
            callback.onResponse(TransportResponseImpl.success(new StreamResponseBuilder().build(EntityStreams.emptyStream())));
        }
    }, true);
    _server.start();
}
Also used : HttpServerFactory(com.linkedin.r2.transport.http.server.HttpServerFactory) TransportCallback(com.linkedin.r2.transport.common.bridge.common.TransportCallback) RestRequest(com.linkedin.r2.message.rest.RestRequest) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) TransportClientAdapter(com.linkedin.r2.transport.common.bridge.client.TransportClientAdapter) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamResponseBuilder(com.linkedin.r2.message.stream.StreamResponseBuilder) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) TransportDispatcher(com.linkedin.r2.transport.common.bridge.server.TransportDispatcher) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) DrainReader(com.linkedin.r2.message.stream.entitystream.DrainReader) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) BeforeClass(org.testng.annotations.BeforeClass)

Example 79 with RestResponseBuilder

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

the class TestDisruptFilter method testMinimumDelayRealDelayLessThanSpecified.

@Test
public void testMinimumDelayRealDelayLessThanSpecified() throws Exception {
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.minimumDelay(MINIMUM_LATENCY));
    ScheduledExecutorService scheduler = EasyMock.createStrictMock(ScheduledExecutorService.class);
    Capture<Long> delay = EasyMock.newCapture();
    EasyMock.expect(scheduler.schedule(EasyMock.anyObject(Runnable.class), EasyMock.captureLong(delay), EasyMock.anyObject(TimeUnit.class))).andDelegateTo(_scheduler);
    EasyMock.replay(scheduler);
    final DisruptFilter filter = new DisruptFilter(scheduler, _executor, REQUEST_TIMEOUT, _clock);
    final CountDownLatch latch = new CountDownLatch(2);
    final AtomicBoolean onRequestSuccess = new AtomicBoolean(false);
    final AtomicBoolean onResponseSuccess = new AtomicBoolean(false);
    final NextFilter<RestRequest, RestResponse> next = new NextFilter<RestRequest, RestResponse>() {

        @Override
        public void onRequest(RestRequest restRequest, RequestContext requestContext, Map<String, String> wireAttrs) {
            onRequestSuccess.set(true);
            latch.countDown();
        }

        @Override
        public void onResponse(RestResponse restResponse, RequestContext requestContext, Map<String, String> wireAttrs) {
            onResponseSuccess.set(true);
            latch.countDown();
        }

        @Override
        public void onError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs) {
            Assert.fail("onError should not be called.");
        }
    };
    filter.onRestRequest(new RestRequestBuilder(new URI(URI)).build(), requestContext, Collections.emptyMap(), next);
    filter.onRestResponse(new RestResponseBuilder().build(), requestContext, Collections.emptyMap(), next);
    Assert.assertTrue(latch.await(TEST_TIMEOUT, TimeUnit.MILLISECONDS), "Missing NextFilter invocation");
    Assert.assertTrue(onRequestSuccess.get(), "Unexpected method invocation");
    Assert.assertTrue(onResponseSuccess.get(), "Unexpected method invocation");
    Assert.assertTrue(delay.getValue().longValue() > 0);
    EasyMock.verify(scheduler);
    EasyMock.reset(scheduler);
}
Also used : ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) RestResponse(com.linkedin.r2.message.rest.RestResponse) NextFilter(com.linkedin.r2.filter.NextFilter) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RestRequest(com.linkedin.r2.message.rest.RestRequest) TimeUnit(java.util.concurrent.TimeUnit) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) Test(org.testng.annotations.Test)

Example 80 with RestResponseBuilder

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

the class TestDisruptFilter method testMinimumDelayRealDelayMoreThanSpecified.

@Test
public void testMinimumDelayRealDelayMoreThanSpecified() throws Exception {
    final RequestContext requestContext = new RequestContext();
    requestContext.putLocalAttr(DISRUPT_CONTEXT_KEY, DisruptContexts.minimumDelay(MINIMUM_LATENCY));
    final DisruptFilter filter = new DisruptFilter(_scheduler, _executor, REQUEST_TIMEOUT, _clock);
    final CountDownLatch latch = new CountDownLatch(2);
    final AtomicBoolean onRequestSuccess = new AtomicBoolean(false);
    final AtomicBoolean onResponseSuccess = new AtomicBoolean(false);
    final NextFilter<RestRequest, RestResponse> next = new NextFilter<RestRequest, RestResponse>() {

        @Override
        public void onRequest(RestRequest restRequest, RequestContext requestContext, Map<String, String> wireAttrs) {
            onRequestSuccess.set(true);
            latch.countDown();
        }

        @Override
        public void onResponse(RestResponse restResponse, RequestContext requestContext, Map<String, String> wireAttrs) {
            onResponseSuccess.set(true);
            latch.countDown();
        }

        @Override
        public void onError(Throwable ex, RequestContext requestContext, Map<String, String> wireAttrs) {
            Assert.fail("onError should not be called.");
        }
    };
    long currentTimeMs = 100;
    _clock.setCurrentTimeMillis(currentTimeMs);
    filter.onRestRequest(new RestRequestBuilder(new URI(URI)).build(), requestContext, Collections.emptyMap(), next);
    // Simulates that real processing took longer than the specified MINIMUM_LATENCY.
    _clock.setCurrentTimeMillis(currentTimeMs + MINIMUM_LATENCY);
    filter.onRestResponse(new RestResponseBuilder().build(), requestContext, Collections.emptyMap(), next);
    // Since the real processing is simulated and no delay should be added, we expect nextFilter should be invoked soon.
    Assert.assertTrue(latch.await(10, TimeUnit.MILLISECONDS), "Missing NextFilter invocation");
    Assert.assertTrue(onRequestSuccess.get(), "Unexpected method invocation");
    Assert.assertTrue(onResponseSuccess.get(), "Unexpected method invocation");
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) NextFilter(com.linkedin.r2.filter.NextFilter) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) URI(java.net.URI) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Map(java.util.Map) Test(org.testng.annotations.Test)

Aggregations

RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)87 RestResponse (com.linkedin.r2.message.rest.RestResponse)55 Test (org.testng.annotations.Test)53 RestRequest (com.linkedin.r2.message.rest.RestRequest)22 RestException (com.linkedin.r2.message.rest.RestException)18 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)17 RequestContext (com.linkedin.r2.message.RequestContext)15 ByteString (com.linkedin.data.ByteString)13 Map (java.util.Map)10 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)8 HashMap (java.util.HashMap)8 TransportCallback (com.linkedin.r2.transport.common.bridge.common.TransportCallback)6 RoutingException (com.linkedin.restli.server.RoutingException)6 DataMap (com.linkedin.data.DataMap)5 FilterChain (com.linkedin.r2.filter.FilterChain)5 CaptureLastCallFilter (com.linkedin.r2.testutils.filter.CaptureLastCallFilter)5 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)5 FilterResponseContext (com.linkedin.restli.server.filter.FilterResponseContext)5 IOException (java.io.IOException)5 URI (java.net.URI)5