Search in sources :

Example 16 with ErrorResponseBody

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);
}
Also used : ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) Test(org.junit.Test)

Example 17 with ErrorResponseBody

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));
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 18 with ErrorResponseBody

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));
}
Also used : HttpProcessingState(com.nike.riposte.server.http.HttpProcessingState) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) IncompleteHttpCallTimeoutException(com.nike.riposte.server.error.exception.IncompleteHttpCallTimeoutException) TooManyOpenChannelsException(com.nike.riposte.server.error.exception.TooManyOpenChannelsException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Test(org.junit.Test)

Example 19 with ErrorResponseBody

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));
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) Test(org.junit.Test)

Example 20 with ErrorResponseBody

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));
}
Also used : ErrorResponseInfo(com.nike.riposte.server.error.handler.ErrorResponseInfo) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) FullResponseInfo(com.nike.riposte.server.http.impl.FullResponseInfo) Test(org.junit.Test)

Aggregations

ErrorResponseBody (com.nike.riposte.server.error.handler.ErrorResponseBody)24 Test (org.junit.Test)18 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 IncompleteHttpCallTimeoutException (com.nike.riposte.server.error.exception.IncompleteHttpCallTimeoutException)6 TooManyOpenChannelsException (com.nike.riposte.server.error.exception.TooManyOpenChannelsException)6 ErrorResponseInfo (com.nike.riposte.server.error.handler.ErrorResponseInfo)6 FullResponseInfo (com.nike.riposte.server.http.impl.FullResponseInfo)6 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)5 RequestInfo (com.nike.riposte.server.http.RequestInfo)5 List (java.util.List)5 ErrorResponseInfo (com.nike.backstopper.handler.ErrorResponseInfo)4 ResponseInfo (com.nike.riposte.server.http.ResponseInfo)4 RequestInfoForLogging (com.nike.backstopper.handler.RequestInfoForLogging)3 DefaultErrorContractDTO (com.nike.backstopper.model.DefaultErrorContractDTO)2 ErrorResponseBodyImpl (com.nike.backstopper.model.riposte.ErrorResponseBodyImpl)2 ErrorResponseInfoImpl (com.nike.backstopper.model.riposte.ErrorResponseInfoImpl)2 ChannelAttributes (com.nike.riposte.server.channelpipeline.ChannelAttributes)2 RiposteErrorHandler (com.nike.riposte.server.error.handler.RiposteErrorHandler)2 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)2