use of com.nike.riposte.server.error.handler.ErrorResponseBody in project riposte by Nike-Inc.
the class ResponseSenderHandlerTest method sendResponse_calls_responseSender_sendErrorResponse_for_error_content.
@Test
public void sendResponse_calls_responseSender_sendErrorResponse_for_error_content() throws JsonProcessingException {
// given
RequestInfo<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
doReturn(requestInfo).when(stateMock).getRequestInfo();
ErrorResponseBody errorContentMock = mock(ErrorResponseBody.class);
Whitebox.setInternalState(responseInfo, "contentForFullResponse", errorContentMock);
// when
handlerSpy.sendResponse(ctxMock, null);
// then
verify(responseSenderMock).sendErrorResponse(ctxMock, requestInfo, (ResponseInfo<ErrorResponseBody>) responseInfo);
}
use of com.nike.riposte.server.error.handler.ErrorResponseBody in project riposte by Nike-Inc.
the class ExceptionHandlingHandlerTest method doExceptionCaught_should_setForceConnectionCloseAfterResponseSent_to_true_on_request_when_exception_matches_certain_types.
@UseDataProvider("exceptionsThatShouldForceCloseConnection")
@Test
public void doExceptionCaught_should_setForceConnectionCloseAfterResponseSent_to_true_on_request_when_exception_matches_certain_types(Throwable exThatShouldForceCloseConnection) throws Exception {
// given
ExceptionHandlingHandler handlerSpy = spy(handler);
ResponseInfo<ErrorResponseBody> errorResponseMock = mock(ResponseInfo.class);
doReturn(errorResponseMock).when(handlerSpy).processError(state, null, exThatShouldForceCloseConnection);
assertThat(state.getResponseInfo(), nullValue());
// when
PipelineContinuationBehavior result = handlerSpy.doExceptionCaught(ctxMock, exThatShouldForceCloseConnection);
// then
verify(errorResponseMock).setForceConnectionCloseAfterResponseSent(true);
verify(handlerSpy).getStateAndCreateIfNeeded(ctxMock, exThatShouldForceCloseConnection);
verify(handlerSpy).processError(state, null, exThatShouldForceCloseConnection);
assertThat(state.getResponseInfo(), is(errorResponseMock));
assertThat(result, is(PipelineContinuationBehavior.CONTINUE));
}
use of com.nike.riposte.server.error.handler.ErrorResponseBody in project riposte by Nike-Inc.
the class ExceptionHandlingHandlerTest method processError_returns_value_of_processUnhandledError_if_riposteErrorHandler_returns_null.
@Test
public void processError_returns_value_of_processUnhandledError_if_riposteErrorHandler_returns_null() throws UnexpectedMajorErrorHandlingError, JsonProcessingException {
// given
HttpProcessingState stateMock = mock(HttpProcessingState.class);
Object msg = new Object();
Throwable cause = new Exception();
ExceptionHandlingHandler handlerSpy = spy(handler);
ResponseInfo<ErrorResponseBody> responseInfoMockFromCatchallMethod = mock(ResponseInfo.class);
doReturn(null).when(riposteErrorHandlerMock).maybeHandleError(any(), any());
doReturn(responseInfoMockFromCatchallMethod).when(handlerSpy).processUnhandledError(stateMock, msg, cause);
// when
ResponseInfo<ErrorResponseBody> response = handlerSpy.processError(stateMock, msg, cause);
// then
verify(riposteErrorHandlerMock).maybeHandleError(any(), any());
assertThat(response, is(responseInfoMockFromCatchallMethod));
}
use of com.nike.riposte.server.error.handler.ErrorResponseBody in project riposte by Nike-Inc.
the class ExceptionHandlingHandlerTest method doChannelRead_should_call_processUnhandledError_and_set_response_on_state_and_return_CONTINUE_if_request_has_not_been_handled.
@Test
public void doChannelRead_should_call_processUnhandledError_and_set_response_on_state_and_return_CONTINUE_if_request_has_not_been_handled() throws Exception {
// given
ExceptionHandlingHandler handlerSpy = spy(handler);
ResponseInfo<ErrorResponseBody> errorResponseMock = mock(ResponseInfo.class);
Object msg = new Object();
doReturn(errorResponseMock).when(handlerSpy).processUnhandledError(eq(state), eq(msg), any(Throwable.class));
assertThat(state.isRequestHandled(), is(false));
// when
PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, msg);
// then
verify(handlerSpy).processUnhandledError(eq(state), eq(msg), any(Throwable.class));
assertThat(state.getResponseInfo(), is(errorResponseMock));
assertThat(result, is(PipelineContinuationBehavior.CONTINUE));
}
use of com.nike.riposte.server.error.handler.ErrorResponseBody in project riposte by Nike-Inc.
the class ExceptionHandlingHandlerTest method setupResponseInfoBasedOnErrorResponseInfo_sets_response_content_and_httpStatusCode_and_ignores_extra_headers_if_extra_headers_is_null.
@Test
public void setupResponseInfoBasedOnErrorResponseInfo_sets_response_content_and_httpStatusCode_and_ignores_extra_headers_if_extra_headers_is_null() {
// given
ResponseInfo<ErrorResponseBody> responseInfo = new FullResponseInfo<>();
ErrorResponseBody errorResponseBodyMock = mock(ErrorResponseBody.class);
int httpStatusCode = 42;
ErrorResponseInfo errorInfoMock = mock(ErrorResponseInfo.class);
doReturn(errorResponseBodyMock).when(errorInfoMock).getErrorResponseBody();
doReturn(httpStatusCode).when(errorInfoMock).getErrorHttpStatusCode();
doReturn(null).when(errorInfoMock).getExtraHeadersToAddToResponse();
// when
handler.setupResponseInfoBasedOnErrorResponseInfo(responseInfo, errorInfoMock);
// then
assertThat(responseInfo.getContentForFullResponse(), is(errorResponseBodyMock));
assertThat(responseInfo.getHttpStatusCode(), is(httpStatusCode));
assertThat(responseInfo.getHeaders().entries().size(), is(0));
}
Aggregations