Search in sources :

Example 21 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.

the class TestRestLiResponseHandler method testApplicationSpecifiedHeaders.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testApplicationSpecifiedHeaders(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    String testHeaderName = "X-LI-TEST-HEADER";
    String testHeaderValue = "test";
    ResourceModel resourceModel = buildResourceModel(StatusCollectionResource.class);
    ResourceMethodDescriptor methodDescriptor = resourceModel.findFinderMethod("search");
    ResourceContextImpl context = new ResourceContextImpl();
    context.setResponseHeader(testHeaderName, testHeaderValue);
    RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptTypeData.acceptHeaders, Collections.emptySet(), context);
    RoutingResult routingResult = new RoutingResult(context, methodDescriptor);
    RestLiResponse response = buildPartialRestResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), routingResult, buildStatusList(3));
    Assert.assertEquals(response.getHeader(testHeaderName), testHeaderValue);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) ByteString(com.linkedin.data.ByteString) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test)

Example 22 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.

the class TestRestLiResponseHandler method testSetResponseCookies.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "basicData")
public void testSetResponseCookies(AcceptTypeData acceptTypeData, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    String testHeaderName = "XXX";
    String testHeaderValue = "head";
    ResourceModel resourceModel = buildResourceModel(StatusCollectionResource.class);
    ResourceMethodDescriptor methodDescriptor = resourceModel.findFinderMethod("search");
    ResourceContextImpl context = new ResourceContextImpl();
    context.setResponseHeader(testHeaderName, testHeaderValue);
    context.addResponseCookie(new HttpCookie("cook1", "value1"));
    context.addResponseCookie(new HttpCookie("cook2", "value2"));
    RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptTypeData.acceptHeaders, Collections.emptySet(), context);
    RoutingResult routingResult = new RoutingResult(context, methodDescriptor);
    // this is a valid response
    RestLiResponse response = buildPartialRestResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), routingResult, buildStatusList(1));
    List<HttpCookie> cookies = Arrays.asList(new HttpCookie("cook1", "value1"), new HttpCookie("cook2", "value2"));
    Assert.assertEquals(response.getCookies(), cookies);
    response = buildPartialRestResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), routingResult, // this is an invalid response
    new RestLiServiceException(HttpStatus.S_404_NOT_FOUND));
    // but the cookie should still be valid
    Assert.assertEquals(response.getCookies(), cookies);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) ResourceMethodDescriptor(com.linkedin.restli.internal.server.model.ResourceMethodDescriptor) ResourceModel(com.linkedin.restli.internal.server.model.ResourceModel) RestLiTestHelper.buildResourceModel(com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) ByteString(com.linkedin.data.ByteString) HttpCookie(java.net.HttpCookie) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test)

Example 23 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.

the class TestRestLiResponseHandler method testPartialRestResponse.

@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusActionDataPartial")
public void testPartialRestResponse(AcceptTypeData acceptTypeData, String response1, String response2, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
    final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
    RestLiResponse response;
    RoutingResult routingResult1 = buildRoutingResultAction(Status.class, request, acceptTypeData.acceptHeaders);
    // #1 simple record template
    response = buildPartialRestResponse(request, routingResult1, buildStatusRecord());
    checkResponse(response, HttpStatus.S_200_OK, 1, false, true, errorResponseHeaderName);
    assertEquals(response.getEntity().toString(), response1);
    // #2 DataTemplate response
    StringMap map = new StringMap();
    map.put("key1", "value1");
    map.put("key2", "value2");
    RoutingResult routingResult2 = buildRoutingResultAction(StringMap.class, request, acceptTypeData.acceptHeaders);
    response = buildPartialRestResponse(request, routingResult2, map);
    checkResponse(response, HttpStatus.S_200_OK, 1, false, true, errorResponseHeaderName);
    // Obtain the maps necessary for comparison
    final DataMap actualMap;
    final DataMap expectedMap;
    actualMap = response.getDataMap();
    expectedMap = JACKSON_DATA_CODEC.stringToMap(response2);
    assertEquals(actualMap, expectedMap);
    RoutingResult routingResult3 = buildRoutingResultAction(Void.TYPE, request, acceptTypeData.acceptHeaders);
    // #3 empty response
    response = buildPartialRestResponse(request, routingResult3, null);
    checkResponse(response, HttpStatus.S_200_OK, 1, false, false, errorResponseHeaderName);
    assertEquals(response.getEntity(), null);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) StringMap(com.linkedin.data.template.StringMap) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) DataMap(com.linkedin.data.DataMap) Test(org.testng.annotations.Test)

Example 24 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.

the class BaseRestLiServer method buildPreRoutingError.

protected RestLiResponseException buildPreRoutingError(Throwable throwable, Request request) {
    Map<String, String> requestHeaders = request.getHeaders();
    Map<String, String> headers = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
    headers.put(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, ProtocolVersionUtil.extractProtocolVersion(requestHeaders).toString());
    headers.put(HeaderUtil.getErrorResponseHeaderName(requestHeaders), RestConstants.HEADER_VALUE_ERROR);
    RestLiServiceException restLiServiceException = RestLiServiceException.fromThrowable(throwable);
    ErrorResponse errorResponse = _errorResponseBuilder.buildErrorResponse(restLiServiceException);
    RestLiResponse restLiResponse = new RestLiResponse.Builder().status(restLiServiceException.getStatus()).entity(errorResponse).headers(headers).cookies(Collections.emptyList()).build();
    return new RestLiResponseException(throwable, restLiResponse);
}
Also used : RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) RestLiResponseException(com.linkedin.restli.internal.server.response.RestLiResponseException) TreeMap(java.util.TreeMap) ErrorResponse(com.linkedin.restli.common.ErrorResponse)

Example 25 with RestLiResponse

use of com.linkedin.restli.internal.server.response.RestLiResponse in project rest.li by linkedin.

the class TestRestLiServer method testHandleRequestWithRestLiResponseSuccess.

@Test(dataProvider = "restOrStream")
public void testHandleRequestWithRestLiResponseSuccess(final RestOrStream restOrStream) throws Exception {
    Status status = new Status();
    status.data().put("test", "this is a test");
    final StatusCollectionResource statusResource = getMockResource(StatusCollectionResource.class);
    EasyMock.expect(statusResource.get(eq(1L))).andReturn(status).once();
    replay(statusResource);
    Callback<RestLiResponse> restLiResponseCallback = new Callback<RestLiResponse>() {

        @Override
        public void onSuccess(RestLiResponse restLiResponse) {
            assertEquals(restLiResponse.getDataMap(), status.data());
            EasyMock.verify(statusResource);
            EasyMock.reset(statusResource);
        }

        @Override
        public void onError(Throwable e) {
            fail("We should not get an error here. The server should have returned a 200!");
        }
    };
    if (restOrStream == RestOrStream.REST) {
        RestRequest request = new RestRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build();
        _server.handleRequestWithRestLiResponse(request, new RequestContext(), restLiResponseCallback);
    } else {
        StreamRequest streamRequest = new StreamRequestBuilder(new URI("/statuses/1")).setHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION, AllProtocolVersions.BASELINE_PROTOCOL_VERSION.toString()).build(EntityStreams.emptyStream());
        _server.handleRequestWithRestLiResponse(streamRequest, new RequestContext(), restLiResponseCallback);
    }
}
Also used : Status(com.linkedin.restli.server.twitter.TwitterTestDataModels.Status) HttpStatus(com.linkedin.restli.common.HttpStatus) SinglePartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.SinglePartMIMEFullReaderCallback) Callback(com.linkedin.common.callback.Callback) MultiPartMIMEFullReaderCallback(com.linkedin.multipart.utils.MIMETestUtils.MultiPartMIMEFullReaderCallback) RestRequest(com.linkedin.r2.message.rest.RestRequest) RestLiResponse(com.linkedin.restli.internal.server.response.RestLiResponse) AsyncStatusCollectionResource(com.linkedin.restli.server.twitter.AsyncStatusCollectionResource) StatusCollectionResource(com.linkedin.restli.server.twitter.StatusCollectionResource) RestRequestBuilder(com.linkedin.r2.message.rest.RestRequestBuilder) FilterRequestContext(com.linkedin.restli.server.filter.FilterRequestContext) RequestContext(com.linkedin.r2.message.RequestContext) URI(java.net.URI) StreamRequestBuilder(com.linkedin.r2.message.stream.StreamRequestBuilder) StreamRequest(com.linkedin.r2.message.stream.StreamRequest) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

RestLiResponse (com.linkedin.restli.internal.server.response.RestLiResponse)27 Test (org.testng.annotations.Test)21 RestRequest (com.linkedin.r2.message.rest.RestRequest)18 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)14 DataMap (com.linkedin.data.DataMap)9 HttpStatus (com.linkedin.restli.common.HttpStatus)9 Status (com.linkedin.restli.server.twitter.TwitterTestDataModels.Status)9 RestLiServiceException (com.linkedin.restli.server.RestLiServiceException)8 ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)7 Callback (com.linkedin.common.callback.Callback)5 ByteString (com.linkedin.data.ByteString)5 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)5 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)5 URI (java.net.URI)5 RequestContext (com.linkedin.r2.message.RequestContext)4 RestLiResponseException (com.linkedin.restli.internal.server.response.RestLiResponseException)4 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)4 AfterTest (org.testng.annotations.AfterTest)4 BeforeTest (org.testng.annotations.BeforeTest)4 RestResponse (com.linkedin.r2.message.rest.RestResponse)3