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);
}
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);
}
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);
}
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);
}
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);
}
}
Aggregations