@Test public void getStateAndCreateIfNeeded_uses_state_from_ctx_if_available() { // expect assertThat(handler.getStateAndCreateIfNeeded(ctxMock, null), is(state)); }
@Test public void getStateAndCreateIfNeeded_creates_new_state_if_ctx_state_is_null() { // given doReturn(null).when(stateAttributeMock).get(); // when HttpProcessingState result = handler.getStateAndCreateIfNeeded(ctxMock, null); // then assertThat(result, notNullValue()); assertThat(result, not(state)); verify(stateAttributeMock).set(result); }
@Override public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) { // We expect to be here for normal message processing, but only as a pass-through. If the state indicates that // the request was not handled then that's a pipeline misconfiguration and we need to throw an error. HttpProcessingState state = getStateAndCreateIfNeeded(ctx, null); if (!state.isRequestHandled()) { runnableWithTracingAndMdc(() -> { String errorMsg = "In ExceptionHandlingHandler's channelRead method, but the request has not yet been " + "handled. This should not be possible and indicates the pipeline is not set up " + "properly or some unknown and unexpected error state was triggered. Sending " + "unhandled error response"; logger.error(errorMsg); Exception ex = new InvalidRipostePipelineException(errorMsg); ResponseInfo<ErrorResponseBody> responseInfo = processUnhandledError(state, msg, ex); state.setResponseInfo(responseInfo, ex); addErrorAnnotationToOverallRequestSpan(state, responseInfo, ex); }, ctx).run(); } return PipelineContinuationBehavior.CONTINUE; }
@Override public PipelineContinuationBehavior doChannelRead(ChannelHandlerContext ctx, Object msg) { // We expect to be here for normal message processing, but only as a pass-through. If the state indicates that // the request was not handled then that's a pipeline misconfiguration and we need to throw an error. HttpProcessingState state = getStateAndCreateIfNeeded(ctx, null); if (!state.isRequestHandled()) { runnableWithTracingAndMdc(() -> { String errorMsg = "In ExceptionHandlingHandler's channelRead method, but the request has not yet been " + "handled. This should not be possible and indicates the pipeline is not set up " + "properly or some unknown and unexpected error state was triggered. Sending " + "unhandled error response"; logger.error(errorMsg); Exception ex = new InvalidRipostePipelineException(errorMsg); ResponseInfo<ErrorResponseBody> responseInfo = processUnhandledError(state, msg, ex); state.setResponseInfo(responseInfo, ex); addErrorAnnotationToOverallRequestSpan(state, responseInfo, ex); }, ctx).run(); } return PipelineContinuationBehavior.CONTINUE; }
HttpProcessingState state = getStateAndCreateIfNeeded(ctx, cause); if (state.isResponseSendingStarted()) { String infoMessage =
@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)); }
HttpProcessingState state = getStateAndCreateIfNeeded(ctx, cause); if (state.isResponseSendingStarted()) { String infoMessage =
@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)); }
verify(handlerSpy).getStateAndCreateIfNeeded(ctxMock, cause); verify(handlerSpy).processError(state, null, cause); assertThat(state.getResponseInfo(), is(errorResponseMock));