use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class TestRestLiResponseHandler method testWrapperResults.
@Test(dataProvider = TestConstants.RESTLI_PROTOCOL_1_2_PREFIX + "statusesData")
public void testWrapperResults(AcceptTypeData acceptTypeData, String expectedStatus, String expectedActionStatus, ProtocolVersion protocolVersion, String errorResponseHeaderName) throws Exception {
RestLiResponse response;
final Status status = buildStatusRecord();
final GetResult<Status> getResult = new GetResult<>(status, HttpStatus.S_500_INTERNAL_SERVER_ERROR);
response = invokeResponseHandler("/test", getResult, ResourceMethod.GET, acceptTypeData.acceptHeaders, protocolVersion);
checkResponse(response, HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode(), 1, true, errorResponseHeaderName);
if (acceptTypeData != AcceptTypeData.PSON) {
assertEquals(DataMapUtils.mapToByteString(response.getDataMap(), response.getHeaders()).asAvroString(), expectedStatus);
}
RestRequest req = buildRequest("/test", acceptTypeData.acceptHeaders, protocolVersion);
RoutingResult routing = buildRoutingResult(ResourceMethod.GET, req, acceptTypeData.acceptHeaders);
RestResponse restResponse = ResponseUtils.buildResponse(routing, response);
assertEquals(restResponse.getEntity().asAvroString(), expectedStatus);
final RestRequest request = buildRequest(acceptTypeData.acceptHeaders, protocolVersion);
final ActionResult<Status> actionResult = new ActionResult<>(status, HttpStatus.S_500_INTERNAL_SERVER_ERROR);
routing = buildRoutingResultAction(Status.class, request, acceptTypeData.acceptHeaders);
response = buildPartialRestResponse(request, routing, actionResult);
checkResponse(response, HttpStatus.S_500_INTERNAL_SERVER_ERROR.getCode(), 1, true, errorResponseHeaderName);
if (acceptTypeData != AcceptTypeData.PSON) {
assertEquals(DataMapUtils.mapToByteString(response.getDataMap(), response.getHeaders()).asAvroString(), expectedActionStatus);
}
restResponse = ResponseUtils.buildResponse(routing, response);
assertEquals(restResponse.getEntity().asAvroString(), expectedActionStatus);
}
use of com.linkedin.restli.internal.server.RoutingResult 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);
}
use of com.linkedin.restli.internal.server.RoutingResult 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);
}
use of com.linkedin.restli.internal.server.RoutingResult 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);
}
use of com.linkedin.restli.internal.server.RoutingResult in project rest.li by linkedin.
the class BaseRestLiServer method getRoutingResult.
protected RoutingResult getRoutingResult(Request request, RequestContext requestContext) {
ensureRequestUsesValidRestliProtocol(request, requestContext);
try {
ServerResourceContext context = new ResourceContextImpl(new PathKeysImpl(), request, requestContext);
RestUtils.validateRequestHeadersAndUpdateResourceContext(request.getHeaders(), _supportedAcceptTypes, _customContentTypes, context, requestContext);
ResourceMethodDescriptor method = _router.process(context);
ResourceMethodConfig methodConfig = _methodConfigProvider.apply(method);
context.setAlwaysProjectedFields(methodConfig.getAlwaysProjectedFields().getValue());
context.setFillInDefaultValues(_fillInDefaultValueConfigured);
return new RoutingResult(context, method, methodConfig);
} catch (RestLiSyntaxException e) {
throw new RoutingException(e.getMessage(), HttpStatus.S_400_BAD_REQUEST.getCode());
}
}
Aggregations