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 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 TestRestBuilders method testAddCookieMultipleValues.
@Test
public void testAddCookieMultipleValues() {
final String cookie1 = "cookie1";
final String cookie2 = "cookie2";
final RestResponse res = new RestResponseBuilder().addCookie(cookie1).addCookie(cookie2).build();
Assert.assertNotNull(res.getHeaders());
Assert.assertNotNull(res.getCookies());
Assert.assertTrue(res.getHeaders().isEmpty());
Assert.assertEquals(res.getCookies().size(), 2);
Assert.assertEquals(res.getCookies().get(0), cookie1);
Assert.assertEquals(res.getCookies().get(1), cookie2);
}
use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class TestRestBuilders method testSetHeadersAndCookiesMultipleValues.
@Test
public void testSetHeadersAndCookiesMultipleValues() {
final String header1 = "key1";
final String header2 = "key2";
final String value1 = "value1";
final String value2 = "value2";
final String cookie1 = "cookie1";
final String cookie2 = "cookie2";
Map<String, String> headers = new HashMap<>();
headers.put(header1, value1);
headers.put(header2, value2);
List<String> cookies = new ArrayList<>();
cookies.add(cookie1);
cookies.add(cookie2);
final RestResponse res = new RestResponseBuilder().setHeaders(headers).setCookies(cookies).build();
Assert.assertNotNull(res.getHeaders());
Assert.assertNotNull(res.getCookies());
Assert.assertEquals(res.getHeaders().size(), 2);
Assert.assertEquals(res.getHeader(header1), value1);
Assert.assertEquals(res.getHeader(header2), value2);
Assert.assertEquals(res.getCookies().size(), 2);
Assert.assertEquals(res.getCookies().get(0), cookie1);
Assert.assertEquals(res.getCookies().get(1), cookie2);
}
use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class TestRestBuilders method testSetValidHeader.
@Test
public void testSetValidHeader() {
final String headerName = "testName";
final String headerValue = "testValue";
final RestResponse res = new RestResponseBuilder().setHeader(headerName, headerValue).build();
Assert.assertEquals(headerValue, res.getHeader(headerName));
}
Aggregations