use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class TestRestCaptureFilter method testCaptureRestException.
@Test
public void testCaptureRestException() {
final RestRequest req = request();
final RestResponse res = new RestResponseBuilder().setStatus(RestStatus.NOT_FOUND).build();
final Exception ex = new RestException(res);
FilterUtil.fireUntypedRequestError(getFilterChain(), req, ex);
Assert.assertEquals(res, getDb().replay(req));
}
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 TestCapRepFilter method testRestException.
@Test
public void testRestException() throws IOException {
Path dirPath = Files.createTempDirectory("caprep-test");
CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
FilterChain fc = FilterChains.createRestChain(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.fireRestRequest(fc, myRequest, requestContext, FilterUtil.emptyWireAttrs());
FilterUtil.fireRestError(fc, myRestException, requestContext, FilterUtil.emptyWireAttrs());
lastCallFilter.reset();
_filter.replay(dirPath.toString());
FilterUtil.fireSimpleRestRequest(fc);
Assert.assertNull(lastCallFilter.getLastErr());
FilterUtil.fireRestRequest(fc, myRequest);
Assert.assertTrue(lastCallFilter.getLastErr() instanceof RestException);
Assert.assertEquals(((RestException) lastCallFilter.getLastErr()).getResponse(), myErrorResponse);
}
use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class TestCapRepFilter method testRestResponse.
@Test
public void testRestResponse() throws IOException {
Path dirPath = Files.createTempDirectory("caprep-test");
RestCountFilter after = new RestCountFilter();
RestCountFilter before = new RestCountFilter();
CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
FilterChain fc = FilterChains.createRestChain(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.fireRestRequest(fc, myRequest, requestContext, FilterUtil.emptyWireAttrs());
FilterUtil.fireRestResponse(fc, myResponse, requestContext, FilterUtil.emptyWireAttrs());
Assert.assertEquals(after.getRestReqCount(), 1);
Assert.assertEquals(after.getRestResCount(), 1);
Assert.assertEquals(before.getRestReqCount(), 1);
Assert.assertEquals(before.getRestResCount(), 1);
lastCallFilter.reset();
_filter.passThrough();
FilterUtil.fireRestRequest(fc, myRequest);
Assert.assertEquals(after.getRestReqCount(), 2);
Assert.assertEquals(after.getRestResCount(), 1);
Assert.assertEquals(before.getRestReqCount(), 2);
Assert.assertEquals(before.getRestResCount(), 1);
Assert.assertNull(lastCallFilter.getLastRes());
_filter.replay(dirPath.toString());
FilterUtil.fireSimpleRestRequest(fc);
Assert.assertNull(lastCallFilter.getLastRes());
FilterUtil.fireRestRequest(fc, myRequest);
Assert.assertEquals(lastCallFilter.getLastRes(), myResponse);
Assert.assertEquals(after.getRestReqCount(), 3);
Assert.assertEquals(after.getRestResCount(), 1);
Assert.assertEquals(before.getRestReqCount(), 4);
Assert.assertEquals(before.getRestResCount(), 2);
}
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;
}
Aggregations