Search in sources :

Example 6 with ResponseInfo

use of com.nike.riposte.server.http.ResponseInfo in project riposte by Nike-Inc.

the class NonblockingEndpointExecutionHandlerTest method doChannelRead_does_nothing_to_timeout_check_if_timeout_check_is_already_completed_when_response_completes.

@Test
public void doChannelRead_does_nothing_to_timeout_check_if_timeout_check_is_already_completed_when_response_completes() throws Exception {
    // given
    ScheduledFuture timeoutCheckMock = mock(ScheduledFuture.class);
    doReturn(timeoutCheckMock).when(eventLoopMock).schedule(any(Runnable.class), any(Long.class), any(TimeUnit.class));
    handlerSpy.doChannelRead(ctxMock, msg);
    ArgumentCaptor<BiConsumer> timeoutCheckCancellationLogicArgumentCaptor = ArgumentCaptor.forClass(BiConsumer.class);
    // The 2nd whenComplete is for cancelling the timeout check if the response finishes before the timeout
    verify(futureThatWillBeAttachedToSpy, times(2)).whenComplete(timeoutCheckCancellationLogicArgumentCaptor.capture());
    BiConsumer<ResponseInfo<?>, Throwable> timeoutCheckCancellationLogic = timeoutCheckCancellationLogicArgumentCaptor.getAllValues().get(1);
    // when: the timeout check scheduled future is already done
    doReturn(true).when(timeoutCheckMock).isDone();
    timeoutCheckCancellationLogic.accept(mock(ResponseInfo.class), null);
    // then: nothing should be done
    verify(timeoutCheckMock).isDone();
    verify(timeoutCheckMock, times(0)).cancel(any(Boolean.class));
    verifyNoMoreInteractions(timeoutCheckMock);
}
Also used : ResponseInfo(com.nike.riposte.server.http.ResponseInfo) LastOutboundMessageSendFullResponseInfo(com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendFullResponseInfo) TimeUnit(java.util.concurrent.TimeUnit) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) BiConsumer(java.util.function.BiConsumer) Test(org.junit.Test)

Example 7 with ResponseInfo

use of com.nike.riposte.server.http.ResponseInfo in project riposte by Nike-Inc.

the class ExceptionHandlingHandlerTest method processError_gets_requestInfo_then_calls_riposteErrorHandler_then_converts_to_response_using_setupResponseInfoBasedOnErrorResponseInfo.

@Test
public void processError_gets_requestInfo_then_calls_riposteErrorHandler_then_converts_to_response_using_setupResponseInfoBasedOnErrorResponseInfo() throws UnexpectedMajorErrorHandlingError, JsonProcessingException {
    // given
    HttpProcessingState stateMock = mock(HttpProcessingState.class);
    Object msg = new Object();
    Throwable cause = new Exception();
    ExceptionHandlingHandler handlerSpy = spy(handler);
    RequestInfo<?> requestInfoMock = mock(RequestInfo.class);
    ErrorResponseInfo errorResponseInfoMock = mock(ErrorResponseInfo.class);
    RiposteErrorHandler riposteErrorHandlerMock = mock(RiposteErrorHandler.class);
    Whitebox.setInternalState(handlerSpy, "riposteErrorHandler", riposteErrorHandlerMock);
    doReturn(requestInfoMock).when(handlerSpy).getRequestInfo(stateMock, msg);
    doReturn(errorResponseInfoMock).when(riposteErrorHandlerMock).maybeHandleError(cause, requestInfoMock);
    // when
    ResponseInfo<ErrorResponseBody> response = handlerSpy.processError(stateMock, msg, cause);
    // then
    verify(handlerSpy).getRequestInfo(stateMock, msg);
    verify(riposteErrorHandlerMock).maybeHandleError(cause, requestInfoMock);
    ArgumentCaptor<ResponseInfo> responseInfoArgumentCaptor = ArgumentCaptor.forClass(ResponseInfo.class);
    verify(handlerSpy).setupResponseInfoBasedOnErrorResponseInfo(responseInfoArgumentCaptor.capture(), eq(errorResponseInfoMock));
    ResponseInfo<ErrorResponseBody> responseInfoPassedIntoSetupMethod = responseInfoArgumentCaptor.getValue();
    assertThat(response, is(responseInfoPassedIntoSetupMethod));
}
Also used : ResponseInfo(com.nike.riposte.server.http.ResponseInfo) ErrorResponseInfo(com.nike.riposte.server.error.handler.ErrorResponseInfo) FullResponseInfo(com.nike.riposte.server.http.impl.FullResponseInfo) ErrorResponseInfo(com.nike.riposte.server.error.handler.ErrorResponseInfo) 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) RiposteErrorHandler(com.nike.riposte.server.error.handler.RiposteErrorHandler) Test(org.junit.Test)

Example 8 with ResponseInfo

use of com.nike.riposte.server.http.ResponseInfo in project riposte by Nike-Inc.

the class NonblockingEndpointExecutionHandlerTest method doChannelRead_cancels_timeout_check_if_response_finishes_before_timeout_check_occurs.

@Test
public void doChannelRead_cancels_timeout_check_if_response_finishes_before_timeout_check_occurs() throws Exception {
    // given
    ScheduledFuture timeoutCheckMock = mock(ScheduledFuture.class);
    doReturn(timeoutCheckMock).when(eventLoopMock).schedule(any(Runnable.class), any(Long.class), any(TimeUnit.class));
    handlerSpy.doChannelRead(ctxMock, msg);
    ArgumentCaptor<BiConsumer> timeoutCheckCancellationLogicArgumentCaptor = ArgumentCaptor.forClass(BiConsumer.class);
    // The 2nd whenComplete is for cancelling the timeout check if the response finishes before the timeout
    verify(futureThatWillBeAttachedToSpy, times(2)).whenComplete(timeoutCheckCancellationLogicArgumentCaptor.capture());
    BiConsumer<ResponseInfo<?>, Throwable> timeoutCheckCancellationLogic = timeoutCheckCancellationLogicArgumentCaptor.getAllValues().get(1);
    // when: the timeout check scheduled future is not yet complete when the response finishes
    doReturn(false).when(timeoutCheckMock).isDone();
    timeoutCheckCancellationLogic.accept(mock(ResponseInfo.class), null);
    // then: timeout check scheduled future should be cancelled
    verify(timeoutCheckMock).cancel(false);
}
Also used : ResponseInfo(com.nike.riposte.server.http.ResponseInfo) LastOutboundMessageSendFullResponseInfo(com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendFullResponseInfo) TimeUnit(java.util.concurrent.TimeUnit) ScheduledFuture(io.netty.util.concurrent.ScheduledFuture) BiConsumer(java.util.function.BiConsumer) Test(org.junit.Test)

Example 9 with ResponseInfo

use of com.nike.riposte.server.http.ResponseInfo in project riposte by Nike-Inc.

the class RequestAndResponseFilterTest method default_method_implementations_behave_as_expected.

@Test
public void default_method_implementations_behave_as_expected() throws CertificateException, SSLException {
    // given
    RequestAndResponseFilter defaultImpl = new RequestAndResponseFilter() {

        @Override
        public <T> RequestInfo<T> filterRequestFirstChunkNoPayload(RequestInfo<T> currentRequestInfo, ChannelHandlerContext ctx) {
            return null;
        }

        @Override
        public <T> RequestInfo<T> filterRequestLastChunkWithFullPayload(RequestInfo<T> currentRequestInfo, ChannelHandlerContext ctx) {
            return null;
        }

        @Override
        public <T> ResponseInfo<T> filterResponse(ResponseInfo<T> currentResponseInfo, RequestInfo<?> requestInfo, ChannelHandlerContext ctx) {
            return null;
        }
    };
    // expect
    assertThat(defaultImpl.isShortCircuitRequestFilter()).isFalse();
    Throwable firstChunkShortCircuitEx = catchThrowable(() -> defaultImpl.filterRequestFirstChunkWithOptionalShortCircuitResponse(mock(RequestInfo.class), mock(ChannelHandlerContext.class)));
    assertThat(firstChunkShortCircuitEx).isNotNull().isInstanceOf(UnsupportedOperationException.class);
    Throwable lastChunkShortCircuitEx = catchThrowable(() -> defaultImpl.filterRequestLastChunkWithOptionalShortCircuitResponse(mock(RequestInfo.class), mock(ChannelHandlerContext.class)));
    assertThat(lastChunkShortCircuitEx).isNotNull().isInstanceOf(UnsupportedOperationException.class);
    assertThat(defaultImpl.shouldExecuteBeforeSecurityValidation()).isTrue();
}
Also used : ResponseInfo(com.nike.riposte.server.http.ResponseInfo) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) RequestInfo(com.nike.riposte.server.http.RequestInfo) Test(org.junit.Test)

Example 10 with ResponseInfo

use of com.nike.riposte.server.http.ResponseInfo in project riposte by Nike-Inc.

the class ShortCircuitingRequestAndResponseFilterTest method default_method_implementations_behave_as_expected.

@Test
public void default_method_implementations_behave_as_expected() throws CertificateException, SSLException {
    // given
    ShortCircuitingRequestAndResponseFilter defaultImpl = new ShortCircuitingRequestAndResponseFilter() {

        @Override
        public <T> ResponseInfo<T> filterResponse(ResponseInfo<T> currentResponseInfo, RequestInfo<?> requestInfo, ChannelHandlerContext ctx) {
            return null;
        }

        @Override
        public <T> Pair<RequestInfo<T>, Optional<ResponseInfo<?>>> filterRequestFirstChunkWithOptionalShortCircuitResponse(RequestInfo<T> currentRequestInfo, ChannelHandlerContext ctx) {
            return null;
        }

        @Override
        public <T> Pair<RequestInfo<T>, Optional<ResponseInfo<?>>> filterRequestLastChunkWithOptionalShortCircuitResponse(RequestInfo<T> currentRequestInfo, ChannelHandlerContext ctx) {
            return null;
        }
    };
    // expect
    assertThat(defaultImpl.isShortCircuitRequestFilter()).isTrue();
    Throwable firstChunkEx = catchThrowable(() -> defaultImpl.filterRequestFirstChunkNoPayload(mock(RequestInfo.class), mock(ChannelHandlerContext.class)));
    assertThat(firstChunkEx).isNotNull().isInstanceOf(UnsupportedOperationException.class);
    Throwable lastChunkEx = catchThrowable(() -> defaultImpl.filterRequestLastChunkWithFullPayload(mock(RequestInfo.class), mock(ChannelHandlerContext.class)));
    assertThat(lastChunkEx).isNotNull().isInstanceOf(UnsupportedOperationException.class);
}
Also used : ResponseInfo(com.nike.riposte.server.http.ResponseInfo) Optional(java.util.Optional) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) RequestInfo(com.nike.riposte.server.http.RequestInfo) Test(org.junit.Test)

Aggregations

ResponseInfo (com.nike.riposte.server.http.ResponseInfo)13 RequestInfo (com.nike.riposte.server.http.RequestInfo)9 Test (org.junit.Test)9 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)7 LastOutboundMessageSendFullResponseInfo (com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendFullResponseInfo)5 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)5 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)4 ErrorResponseBody (com.nike.riposte.server.error.handler.ErrorResponseBody)4 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)4 Pair (com.nike.internal.util.Pair)3 ChannelAttributes (com.nike.riposte.server.channelpipeline.ChannelAttributes)3 IncompleteHttpCallTimeoutException (com.nike.riposte.server.error.exception.IncompleteHttpCallTimeoutException)3 TooManyOpenChannelsException (com.nike.riposte.server.error.exception.TooManyOpenChannelsException)3 ErrorResponseInfo (com.nike.riposte.server.error.handler.ErrorResponseInfo)3 FullResponseInfo (com.nike.riposte.server.http.impl.FullResponseInfo)3 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)3 ScheduledFuture (io.netty.util.concurrent.ScheduledFuture)3 TimeUnit (java.util.concurrent.TimeUnit)3 RiposteErrorHandler (com.nike.riposte.server.error.handler.RiposteErrorHandler)2 BaseInboundHandlerWithTracingAndMdcSupport (com.nike.riposte.server.handler.base.BaseInboundHandlerWithTracingAndMdcSupport)2