use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class TestRestBuilders method testHeaderWithNullListElem.
@Test
public void testHeaderWithNullListElem() {
final String headerName = "key";
final String headerVal1 = "value1";
final String headerVal2 = "value2";
final String headerValue = headerVal1 + ", ," + headerVal2;
final MessageHeaders msg = new RestResponseBuilder().addHeaderValue(headerName, headerValue).build();
Assert.assertEquals(headerValue, msg.getHeader(headerName));
Assert.assertEquals(Arrays.asList(headerVal1, headerVal2), msg.getHeaderValues(headerName));
}
use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class RAPResponseHandler method channelRead0.
@Override
protected void channelRead0(ChannelHandlerContext ctx, RestResponse response) throws Exception {
final Map<String, String> headers = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
final Map<String, String> wireAttrs = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
headers.putAll(response.getHeaders());
wireAttrs.putAll(WireAttributeHelper.removeWireAttributes(headers));
final RestResponse newResponse = new RestResponseBuilder(response).unsafeSetHeaders(headers).build();
// In general there should always be a callback to handle a received message,
// but it could have been removed due to a previous exception or closure on the
// channel
TransportCallback<RestResponse> callback = ctx.channel().attr(CALLBACK_ATTR_KEY).getAndRemove();
if (callback != null) {
LOG.debug("{}: handling a response", ctx.channel().remoteAddress());
callback.onResponse(TransportResponseImpl.success(newResponse, wireAttrs));
} else {
LOG.debug("{}: dropped a response", ctx.channel().remoteAddress());
}
ctx.fireChannelRead(response);
}
use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class TestChannelPoolHandler method testConnectionClose.
@Test(dataProvider = "connectionClose")
public void testConnectionClose(String headerName, String headerValue) {
EmbeddedChannel ch = new EmbeddedChannel(new ChannelPoolHandler());
FakePool pool = new FakePool();
ch.attr(ChannelPoolHandler.CHANNEL_POOL_ATTR_KEY).set(pool);
RestResponse response = new RestResponseBuilder().setHeader(headerName, headerValue).build();
ch.writeInbound(response);
Assert.assertTrue(pool.isDisposeCalled());
Assert.assertFalse(pool.isPutCalled());
}
use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class RestClientTest method testEmptyErrorResponse.
@Test
public void testEmptyErrorResponse() {
RestResponse response = new RestResponseBuilder().setStatus(200).build();
RestLiResponseException e = new RestLiResponseException(response, null, new ErrorResponse());
Assert.assertNull(e.getServiceErrorMessage());
Assert.assertNull(e.getErrorDetails());
Assert.assertNull(e.getErrorSource());
Assert.assertFalse(e.hasServiceErrorCode());
Assert.assertNull(e.getServiceErrorStackTrace());
Assert.assertNull(e.getServiceExceptionClass());
}
use of com.linkedin.r2.message.rest.RestResponseBuilder in project rest.li by linkedin.
the class TestRestLiCallback method testOnErrorWithFiltersSuccessfulyHandlingAppEx.
@SuppressWarnings("unchecked")
@Test
public void testOnErrorWithFiltersSuccessfulyHandlingAppEx() throws Exception {
Exception exFromApp = new RuntimeException("Runtime exception from app");
RestLiServiceException appException = new RestLiServiceException(HttpStatus.S_404_NOT_FOUND);
RequestExecutionReport executionReport = new RequestExecutionReportBuilder().build();
RestLiResponseAttachments responseAttachments = new RestLiResponseAttachments.Builder().build();
final Map<String, String> headersFromApp = Maps.newHashMap();
headersFromApp.put("Key", "Input");
final RecordTemplate entityFromFilter = Foo.createFoo("Key", "Two");
final Map<String, String> headersFromFilter = Maps.newHashMap();
headersFromFilter.put("Key", "Output");
RestLiResponseDataImpl responseData = new RestLiResponseDataImpl(appException, headersFromApp, Collections.<HttpCookie>emptyList());
responseData.setResponseEnvelope(new CreateResponseEnvelope(new EmptyRecord(), responseData));
PartialRestResponse partialResponse = new PartialRestResponse.Builder().build();
ArgumentCaptor<RestLiServiceException> exCapture = ArgumentCaptor.forClass(RestLiServiceException.class);
when(_requestExecutionReportBuilder.build()).thenReturn(executionReport);
when(_responseHandler.buildExceptionResponseData(eq(_restRequest), eq(_routingResult), exCapture.capture(), anyMap(), anyList())).thenReturn(responseData);
when(_responseHandler.buildPartialResponse(_routingResult, responseData)).thenReturn(partialResponse);
// Mock the behavior of the first filter.
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
Throwable t = (Throwable) args[0];
FilterRequestContext requestContext = (FilterRequestContext) args[1];
FilterResponseContext responseContext = (FilterResponseContext) args[2];
// Verify incoming data.
assertEquals(HttpStatus.S_404_NOT_FOUND, responseContext.getResponseData().getStatus());
assertEquals(headersFromApp, responseContext.getResponseData().getHeaders());
assertNull(responseContext.getResponseData().getRecordResponseEnvelope().getRecord());
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_400_BAD_REQUEST);
responseContext.getResponseData().getHeaders().clear();
return CompletableFuture.completedFuture(null);
}
}).when(_filter).onError(any(Throwable.class), eq(_filterRequestContext), any(FilterResponseContext.class));
doAnswer(new Answer<Object>() {
// Mock the behavior of the second filter.
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
Object[] args = invocation.getArguments();
FilterRequestContext requestContext = (FilterRequestContext) args[0];
FilterResponseContext responseContext = (FilterResponseContext) args[1];
// Verify incoming data.
assertEquals(HttpStatus.S_400_BAD_REQUEST, responseContext.getResponseData().getStatus());
assertTrue(responseContext.getResponseData().getHeaders().isEmpty());
assertNull(responseContext.getResponseData().getRecordResponseEnvelope().getRecord());
// Modify data in filter.
setStatus(responseContext, HttpStatus.S_403_FORBIDDEN);
responseContext.getResponseData().getRecordResponseEnvelope().setRecord(entityFromFilter, HttpStatus.S_403_FORBIDDEN);
responseContext.getResponseData().getHeaders().putAll(headersFromFilter);
return CompletableFuture.completedFuture(null);
}
}).when(_filter).onResponse(eq(_filterRequestContext), any(FilterResponseContext.class));
RestResponse restResponse = new RestResponseBuilder().build();
when(_responseHandler.buildResponse(_routingResult, partialResponse)).thenReturn(restResponse);
// invoke request filters so cursor is in correct place
when(_filter.onRequest(any(FilterRequestContext.class))).thenReturn(CompletableFuture.completedFuture(null));
_twoFilterChain.onRequest(_filterRequestContext, _filterResponseContextFactory);
// Invoke.
_twoFilterRestLiCallback.onError(exFromApp, executionReport, _requestAttachmentReader, responseAttachments);
// Verify.
assertNotNull(responseData);
assertEquals(HttpStatus.S_403_FORBIDDEN, responseData.getStatus());
assertEquals(entityFromFilter, responseData.getRecordResponseEnvelope().getRecord());
assertEquals(headersFromFilter, responseData.getHeaders());
verify(_responseHandler).buildExceptionResponseData(eq(_restRequest), eq(_routingResult), exCapture.capture(), anyMap(), anyList());
verify(_responseHandler).buildPartialResponse(_routingResult, responseData);
verify(_responseHandler).buildResponse(_routingResult, partialResponse);
verify(_callback).onSuccess(restResponse, executionReport, responseAttachments);
verify(_restRequest, times(1)).getHeaders();
verifyZeroInteractions(_routingResult);
verifyNoMoreInteractions(_restRequest, _responseHandler, _callback);
RestLiServiceException restliEx = exCapture.getValue();
assertNotNull(restliEx);
assertEquals(HttpStatus.S_500_INTERNAL_SERVER_ERROR, restliEx.getStatus());
assertEquals(exFromApp.getMessage(), restliEx.getMessage());
assertEquals(exFromApp, restliEx.getCause());
}
Aggregations