Search in sources :

Example 66 with RestResponseBuilder

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);
}
Also used : Path(java.nio.file.Path) 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) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) RestCountFilter(com.linkedin.r2.testutils.filter.RestCountFilter) Test(org.testng.annotations.Test)

Example 67 with RestResponseBuilder

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));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) RestException(com.linkedin.r2.message.rest.RestException) RestException(com.linkedin.r2.message.rest.RestException) Test(org.testng.annotations.Test)

Example 68 with RestResponseBuilder

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);
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) Test(org.testng.annotations.Test)

Example 69 with RestResponseBuilder

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);
}
Also used : HashMap(java.util.HashMap) RestResponse(com.linkedin.r2.message.rest.RestResponse) ArrayList(java.util.ArrayList) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) Test(org.testng.annotations.Test)

Example 70 with RestResponseBuilder

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));
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) 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