use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class TestRAPClientCodec method testResponseDecoder.
@Test(dataProvider = "responseData")
public void testResponseDecoder(int status, String entity, HttpHeaders headers, String[] cookies) {
final EmbeddedChannel ch = new EmbeddedChannel(new RAPClientCodec());
ByteBuf content = Unpooled.copiedBuffer(entity, CHARSET);
FullHttpResponse nettyResponse = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.valueOf(status), content);
nettyResponse.headers().set(headers);
for (String cookie : cookies) {
nettyResponse.headers().add(HttpHeaderNames.SET_COOKIE, cookie);
}
ch.writeInbound(nettyResponse);
RestResponse response = (RestResponse) ch.readInbound();
Assert.assertEquals(response.getStatus(), status);
Assert.assertEquals(response.getEntity().asString(CHARSET), entity);
assertList(response.getCookies(), nettyResponse.headers().getAll(HttpConstants.RESPONSE_COOKIE_HEADER_NAME));
for (Map.Entry<String, String> header : nettyResponse.headers()) {
if (!header.getKey().equalsIgnoreCase(HttpConstants.RESPONSE_COOKIE_HEADER_NAME)) {
List<String> values = response.getHeaderValues(header.getKey());
Assert.assertNotNull(values);
Assert.assertTrue(values.contains(header.getValue()));
}
}
// make sure the incoming ByteBuf is released
Assert.assertEquals(content.refCnt(), 0);
ch.finish();
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class TestMockFailedResponseFutureBuilder method testBuildWithErrorResponseTreatServerErrorAsSuccess.
@Test
public void testBuildWithErrorResponseTreatServerErrorAsSuccess() {
ResponseFuture<Greeting> future = buildWithErrorResponse(ErrorHandlingBehavior.TREAT_SERVER_ERROR_AS_SUCCESS);
try {
Response<Greeting> response = future.getResponse();
Assert.assertNull(response.getEntity());
Assert.assertEquals(response.getStatus(), 404);
RestLiResponseException restLiResponseException = response.getError();
Assert.assertEquals(restLiResponseException.getStatus(), 404);
Assert.assertEquals(restLiResponseException.getServiceErrorMessage(), "foo");
} catch (Exception e) {
Assert.fail("No exception should have been thrown!");
}
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class TestRestClientRequestBuilder method clientGeneratedStreamRequest.
//This is similar to clientGeneratedRestRequest above except that it will generate a StreamRequest instead
//of a RestRequest. Note that this will ONLY happen if either acceptResponseAttachments below is 'true' OR
//streamingAttachmentDataSources below is non-null with a size greater then 0. If both of these do not hold,
//then a StreamRequest will not be generated by the RestClient.
@SuppressWarnings({ "unchecked", "rawtypes", "deprecation" })
private <T extends Request> StreamRequest clientGeneratedStreamRequest(Class<T> requestClass, ResourceMethod method, DataMap entityBody, RestClient.ContentType contentType, List<RestClient.AcceptType> acceptTypes, boolean acceptContentTypePerClient, List<Object> streamingAttachmentDataSources, boolean acceptResponseAttachments) throws URISyntaxException {
// massive setup...
Client mockClient = EasyMock.createMock(Client.class);
@SuppressWarnings({ "rawtypes" }) Request<?> mockRequest = EasyMock.createMock(requestClass);
RecordTemplate mockRecordTemplate = EasyMock.createMock(RecordTemplate.class);
@SuppressWarnings({ "rawtypes" }) RestResponseDecoder mockResponseDecoder = EasyMock.createMock(RestResponseDecoder.class);
RestliRequestOptions requestOptions = RestliRequestOptions.DEFAULT_OPTIONS;
//If there is a desire to receive response attachments, then we must use request options.
if (!acceptContentTypePerClient || acceptResponseAttachments) {
requestOptions = new RestliRequestOptions(ProtocolVersionOption.USE_LATEST_IF_AVAILABLE, null, null, contentType, acceptTypes, acceptResponseAttachments);
}
setCommonExpectations(mockRequest, method, mockResponseDecoder, requestOptions);
if (streamingAttachmentDataSources != null && streamingAttachmentDataSources.size() > 0) {
EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(streamingAttachmentDataSources).times(2);
} else {
EasyMock.expect(mockRequest.getStreamingAttachments()).andReturn(null).times(2);
}
setResourceMethodExpectations(method, mockRequest, mockRecordTemplate, entityBody);
Capture<StreamRequest> streamRequestCapture = new Capture<StreamRequest>();
EasyMock.expect(mockClient.getMetadata(new URI(HOST + SERVICE_NAME))).andReturn(Collections.<String, Object>emptyMap()).once();
mockClient.streamRequest(EasyMock.capture(streamRequestCapture), (RequestContext) EasyMock.anyObject(), (Callback<StreamResponse>) EasyMock.anyObject());
EasyMock.expectLastCall().once();
EasyMock.replay(mockClient, mockRequest, mockRecordTemplate);
// do work!
RestClient restClient;
if (acceptContentTypePerClient) {
// configuration per client
restClient = new RestClient(mockClient, HOST, contentType, acceptTypes);
} else {
// configuration per request
restClient = new RestClient(mockClient, HOST);
}
restClient.sendRequest(mockRequest);
return streamRequestCapture.getValue();
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class TestGreetingsClientProtocolVersionHeader method testNoProtocolVersionHeaderSuccess.
@Test
public void testNoProtocolVersionHeaderSuccess() throws InterruptedException, ExecutionException {
final TransportClientAdapter client = new TransportClientAdapter(CLIENT_FACTORY.getClient(Collections.<String, String>emptyMap()));
final RestRequestBuilder requestBuilder = new RestRequestBuilder(URI.create(URI_PREFIX + "greetings/1"));
final RestRequest request = requestBuilder.build();
Assert.assertTrue(request.getHeaders().isEmpty());
final RestResponse response = client.restRequest(request).get();
Assert.assertEquals(response.getStatus(), HttpStatus.S_200_OK.getCode());
Assert.assertEquals(response.getHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION), AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion().toString());
}
use of com.linkedin.r2.message.Response in project rest.li by linkedin.
the class TestGreetingsClientProtocolVersionHeader method testNoProtocolVersionHeaderFail.
@Test
public void testNoProtocolVersionHeaderFail() throws InterruptedException {
final TransportClientAdapter client = new TransportClientAdapter(CLIENT_FACTORY.getClient(Collections.<String, String>emptyMap()));
final RestRequestBuilder requestBuilder = new RestRequestBuilder(URI.create(URI_PREFIX));
final RestRequest request = requestBuilder.build();
Assert.assertTrue(request.getHeaders().isEmpty());
try {
client.restRequest(request).get();
} catch (ExecutionException e) {
final RestException exception = (RestException) e.getCause();
final RestResponse response = exception.getResponse();
Assert.assertEquals(response.getStatus(), HttpStatus.S_404_NOT_FOUND.getCode());
Assert.assertEquals(response.getHeader(RestConstants.HEADER_RESTLI_PROTOCOL_VERSION), AllProtocolVersions.RESTLI_PROTOCOL_1_0_0.getProtocolVersion().toString());
final DataMap exceptionDetail = DataMapUtils.readMap(response.getEntity().asInputStream());
Assert.assertEquals(exceptionDetail.getString("exceptionClass"), RestLiServiceException.class.getName());
}
}
Aggregations