@DataProvider(value = { "true", "false" }) @Test public void doExceptionCaught_should_do_nothing_and_return_DO_NOT_FIRE_CONTINUE_EVENT_if_response_sending_already_started( boolean causeIsNullPointerException ) { // given ExceptionHandlingHandler handlerSpy = spy(handler); Throwable cause = (causeIsNullPointerException) ? new NullPointerException("intentional NPE") : new Exception("intentional test exception"); ResponseInfo<?> responseInfoMock = mock(ResponseInfo.class); doReturn(true).when(responseInfoMock).isResponseSendingStarted(); state.setResponseInfo(responseInfoMock, cause); // when PipelineContinuationBehavior result = handlerSpy.doExceptionCaught(ctxMock, cause); // then verify(handlerSpy, never()).processError(any(HttpProcessingState.class), any(Object.class), any(Throwable.class)); Assertions.assertThat(result).isEqualTo(DO_NOT_FIRE_CONTINUE_EVENT); }
@Test public void doExceptionCaught_should_cancel_proxy_router_processing_if_endpoint_is_ProxyRouterEndpoint() { // given ExceptionHandlingHandler handlerSpy = spy(handler); Throwable cause = new Exception("intentional test exception"); ResponseInfo<ErrorResponseBody> errorResponseMock = mock(ResponseInfo.class); doReturn(errorResponseMock).when(handlerSpy).processError(state, null, cause); assertThat(state.getResponseInfo(), nullValue()); // when PipelineContinuationBehavior result = handlerSpy.doExceptionCaught(ctxMock, cause); // then verify(handlerSpy).getStateAndCreateIfNeeded(ctxMock, cause); verify(handlerSpy).processError(state, null, cause); assertThat(state.getResponseInfo(), is(errorResponseMock)); Assertions.assertThat(state.getErrorThatTriggeredThisResponse()).isSameAs(cause); verify(handlerSpy).addErrorAnnotationToOverallRequestSpan(state, errorResponseMock, cause); assertThat(result, is(PipelineContinuationBehavior.CONTINUE)); }
@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)); }
PipelineContinuationBehavior result = handlerSpy.doExceptionCaught(ctxMock, cause);