Search in sources :

Example 21 with ResourceContextImpl

use of com.linkedin.restli.internal.server.ResourceContextImpl 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.findNamedMethod("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, context);
    RoutingResult routingResult = new RoutingResult(context, methodDescriptor);
    // this is a valid response
    RestResponse response = _responseHandler.buildResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), routingResult, buildStatusList(1));
    List<HttpCookie> cookies = Arrays.asList(new HttpCookie("cook1", "value1"), new HttpCookie("cook2", "value2"));
    Assert.assertEquals(CookieUtil.decodeSetCookies(response.getCookies()), cookies);
    response = _responseHandler.buildResponse(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(CookieUtil.decodeSetCookies(response.getCookies()), cookies);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestLiServiceException(com.linkedin.restli.server.RestLiServiceException) RestResponse(com.linkedin.r2.message.rest.RestResponse) PartialRestResponse(com.linkedin.restli.internal.server.response.PartialRestResponse) 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) ByteString(com.linkedin.data.ByteString) HttpCookie(java.net.HttpCookie) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test)

Example 22 with ResourceContextImpl

use of com.linkedin.restli.internal.server.ResourceContextImpl 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.findNamedMethod("search");
    ResourceContextImpl context = new ResourceContextImpl();
    context.setResponseHeader(testHeaderName, testHeaderValue);
    RestUtils.validateRequestHeadersAndUpdateResourceContext(acceptTypeData.acceptHeaders, context);
    RoutingResult routingResult = new RoutingResult(context, methodDescriptor);
    RestResponse response;
    response = _responseHandler.buildResponse(buildRequest(acceptTypeData.acceptHeaders, protocolVersion), routingResult, buildStatusList(3));
    Assert.assertEquals(response.getHeader(testHeaderName), testHeaderValue);
}
Also used : RoutingResult(com.linkedin.restli.internal.server.RoutingResult) RestResponse(com.linkedin.r2.message.rest.RestResponse) PartialRestResponse(com.linkedin.restli.internal.server.response.PartialRestResponse) 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) ByteString(com.linkedin.data.ByteString) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) Test(org.testng.annotations.Test)

Example 23 with ResourceContextImpl

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

the class TestRestUtils method testValidateRequestHeadersWithValidAcceptHeaderAndMatch.

@Test()
public void testValidateRequestHeadersWithValidAcceptHeaderAndMatch() throws Exception {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Accept", "application/json");
    ServerResourceContext resourceContext = new ResourceContextImpl();
    RestUtils.validateRequestHeadersAndUpdateResourceContext(headers, resourceContext);
    Assert.assertEquals(resourceContext.getResponseMimeType(), "application/json");
}
Also used : HashMap(java.util.HashMap) ServerResourceContext(com.linkedin.restli.internal.server.ServerResourceContext) ResourceContextImpl(com.linkedin.restli.internal.server.ResourceContextImpl) UnionTest(com.linkedin.pegasus.generator.test.UnionTest) Test(org.testng.annotations.Test) TyperefTest(com.linkedin.pegasus.generator.test.TyperefTest)

Aggregations

ResourceContextImpl (com.linkedin.restli.internal.server.ResourceContextImpl)23 ServerResourceContext (com.linkedin.restli.internal.server.ServerResourceContext)17 RequestContext (com.linkedin.r2.message.RequestContext)16 Test (org.testng.annotations.Test)16 RoutingResult (com.linkedin.restli.internal.server.RoutingResult)15 ByteString (com.linkedin.data.ByteString)12 URI (java.net.URI)12 PathKeysImpl (com.linkedin.restli.internal.server.PathKeysImpl)11 RestRequest (com.linkedin.r2.message.rest.RestRequest)9 ResourceMethodDescriptor (com.linkedin.restli.internal.server.model.ResourceMethodDescriptor)9 ResourceModel (com.linkedin.restli.internal.server.model.ResourceModel)9 RestLiTestHelper.buildResourceModel (com.linkedin.restli.server.test.RestLiTestHelper.buildResourceModel)9 RestRequestBuilder (com.linkedin.r2.message.rest.RestRequestBuilder)8 RestResponse (com.linkedin.r2.message.rest.RestResponse)7 RoutingException (com.linkedin.restli.server.RoutingException)7 DataMap (com.linkedin.data.DataMap)6 RestLiSyntaxException (com.linkedin.restli.internal.server.util.RestLiSyntaxException)6 RestLiResponseAttachments (com.linkedin.restli.server.RestLiResponseAttachments)6 RestLiAttachmentReader (com.linkedin.restli.common.attachments.RestLiAttachmentReader)5 RequestExecutionReport (com.linkedin.restli.server.RequestExecutionReport)5