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());
}
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;
}
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();
}
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);
}
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");
}
Aggregations