Search in sources :

Example 21 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest 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 22 with RestRequest

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

the class TestDefaultMessageSerializer method testRestReqWithHeaders.

@Test
public void testRestReqWithHeaders() throws IOException {
    final RestRequest expected = new RestRequestBuilder(URI.create("http://localhost:1234")).setHeader("field-name1", "field-val1").setHeader("field-name2", "field-val2").build();
    assertMsgEquals(expected, _serializer.readRestRequest(getResource("rest-req-with-headers.txt")));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) Test(org.testng.annotations.Test)

Example 23 with RestRequest

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

the class TestDefaultMessageSerializer method testRestRequestReversible6.

@Test
public void testRestRequestReversible6() throws IOException {
    final RestRequest req = createRestRequest().builder().setHeader("set-cookie", "field-value1").addCookie("cookie-key1=cookie-value1").build();
    assertMsgEquals(req, readRestReq(writeReq(req)));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) Test(org.testng.annotations.Test)

Example 24 with RestRequest

use of com.linkedin.r2.message.rest.RestRequest 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)

Example 25 with RestRequest

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

the class AbstractCaptureFilterTest method testSameRequestDifferentResponses.

@Test
public void testSameRequestDifferentResponses() {
    final RestRequest req = request();
    final RestResponse res1 = response();
    final RestResponse res2 = res1.builder().setEntity("This is a different response".getBytes()).build();
    FilterUtil.fireUntypedRequestResponse(getFilterChain(), req, res1);
    FilterUtil.fireUntypedRequestResponse(getFilterChain(), req, res2);
    // Last one wins
    Assert.assertEquals(res2, getDb().<Response>replay(req));
}
Also used : RestRequest(com.linkedin.r2.message.rest.RestRequest) RestResponse(com.linkedin.r2.message.rest.RestResponse) Test(org.testng.annotations.Test)

Aggregations

RestRequest (com.linkedin.r2.message.rest.RestRequest)293 Test (org.testng.annotations.Test)243 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)174 RequestContext (com.linkedin.r2.message.RequestContext)154 RestResponse (com.linkedin.r2.message.rest.RestResponse)147 URI (java.net.URI)124 ByteString (com.linkedin.data.ByteString)65 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)62 FutureCallback (com.linkedin.common.callback.FutureCallback)50 RestException (com.linkedin.r2.message.rest.RestException)49 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)44 ExecutionException (java.util.concurrent.ExecutionException)43 StreamResponse (com.linkedin.r2.message.stream.StreamResponse)38 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)36 HashMap (java.util.HashMap)35 StreamRequest (com.linkedin.r2.message.stream.StreamRequest)33 Map (java.util.Map)28 Callback (com.linkedin.common.callback.Callback)27 URISyntaxException (java.net.URISyntaxException)25 ResourceContext (com.linkedin.restli.server.ResourceContext)24