Search in sources :

Example 76 with RestResponse

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

the class TestCapRepFilter method testStreamException.

@Test
public void testStreamException() throws IOException {
    Path dirPath = Files.createTempDirectory("caprep-test");
    CaptureLastCallFilter lastCallFilter = new CaptureLastCallFilter();
    FilterChain fc = FilterChains.createStreamChain(StreamFilterAdapters.adaptRestFilter(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.fireStreamRequest(fc, Messages.toStreamRequest(myRequest), requestContext, FilterUtil.emptyWireAttrs());
    FilterUtil.fireStreamError(fc, Messages.toStreamException(myRestException), requestContext, FilterUtil.emptyWireAttrs());
    lastCallFilter.reset();
    _filter.replay(dirPath.toString());
    FilterUtil.fireSimpleStreamRequest(fc);
    Assert.assertNull(lastCallFilter.getLastErr());
    FilterUtil.fireStreamRequest(fc, Messages.toStreamRequest(myRequest));
    Assert.assertTrue(lastCallFilter.getLastErr() instanceof RestException);
    Assert.assertEquals(((RestException) lastCallFilter.getLastErr()).getResponse(), myErrorResponse);
}
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) RestException(com.linkedin.r2.message.rest.RestException) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) RequestContext(com.linkedin.r2.message.RequestContext) Test(org.testng.annotations.Test)

Example 77 with RestResponse

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

the class TestDefaultMessageSerializer method testRestResWithCookies.

@Test
public void testRestResWithCookies() throws IOException {
    final RestResponse expected = new RestResponseBuilder().addCookie("cookie-name1=cookie-value1").addCookie("cookie-name2=cookie-value2").build();
    assertMsgEquals(expected, _serializer.readRestResponse(getResource("rest-res-with-cookies.txt")));
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) Test(org.testng.annotations.Test)

Example 78 with RestResponse

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

the class TestDefaultMessageSerializer method testRestResWithHeadersAndCookies.

@Test
public void testRestResWithHeadersAndCookies() throws IOException {
    final RestResponse expected = new RestResponseBuilder().addCookie("cookie-name1=cookie-value1").addCookie("cookie-name2=cookie-value2").addHeaderValue("header-field1", "header-value1").build();
    assertMsgEquals(expected, _serializer.readRestResponse(getResource("rest-res-with-headers-and-cookies.txt")));
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) RestResponseBuilder(com.linkedin.r2.message.rest.RestResponseBuilder) Test(org.testng.annotations.Test)

Example 79 with RestResponse

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

the class TestDefaultMessageSerializer method testRestResponseReversible4.

@Test
public void testRestResponseReversible4() throws IOException {
    final RestResponse res = createRestResponse().builder().setHeader("field-key1", "field-val1").setHeader("field-key2", "field-val2").build();
    assertMsgEquals(res, readRestRes(writeRes(res)));
}
Also used : RestResponse(com.linkedin.r2.message.rest.RestResponse) Test(org.testng.annotations.Test)

Example 80 with RestResponse

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

the class AbstractClient method restRequest.

@Override
public void restRequest(RestRequest request, RequestContext requestContext, Callback<RestResponse> callback) {
    StreamRequest streamRequest = Messages.toStreamRequest(request);
    // IS_FULL_REQUEST flag, if set true, would result in the request being sent without using chunked transfer encoding
    // This is needed as the legacy R2 server (before 2.8.0) does not support chunked transfer encoding.
    requestContext.putLocalAttr(R2Constants.IS_FULL_REQUEST, true);
    // here we add back the content-length header for the response because some client code depends on this header
    streamRequest(streamRequest, requestContext, Messages.toStreamCallback(callback, true));
}
Also used : StreamRequest(com.linkedin.r2.message.stream.StreamRequest)

Aggregations

RestResponse (com.linkedin.r2.message.rest.RestResponse)231 Test (org.testng.annotations.Test)174 RestRequest (com.linkedin.r2.message.rest.RestRequest)147 RequestContext (com.linkedin.r2.message.RequestContext)108 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)100 URI (java.net.URI)74 RestException (com.linkedin.r2.message.rest.RestException)68 RestResponseBuilder (com.linkedin.r2.message.rest.RestResponseBuilder)55 ByteString (com.linkedin.data.ByteString)50 FutureCallback (com.linkedin.common.callback.FutureCallback)45 Callback (com.linkedin.common.callback.Callback)43 HashMap (java.util.HashMap)41 ExecutionException (java.util.concurrent.ExecutionException)40 Map (java.util.Map)38 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)35 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)29 URISyntaxException (java.net.URISyntaxException)27 FilterRequestContext (com.linkedin.restli.server.filter.FilterRequestContext)24 FilterChain (com.linkedin.r2.filter.FilterChain)23 BeforeTest (org.testng.annotations.BeforeTest)23