Search in sources :

Example 91 with PipelineContinuationBehavior

use of com.nike.riposte.server.handler.base.PipelineContinuationBehavior in project riposte by Nike-Inc.

the class RequestFilterHandlerTest method handleFilterLogic_executes_all_filters_and_uses_requestInfo_returned_by_short_circuiting_filters_when_they_do_not_short_circuit.

@DataProvider(value = { "true", "false" }, splitBy = "\\|")
@Test
public void handleFilterLogic_executes_all_filters_and_uses_requestInfo_returned_by_short_circuiting_filters_when_they_do_not_short_circuit(boolean isFirstChunk) {
    // given
    HandleFilterLogicMethodCallArgs args = new HandleFilterLogicMethodCallArgs(isFirstChunk);
    filtersList.forEach(filter -> doReturn(true).when(filter).isShortCircuitRequestFilter());
    RequestInfo<?> firstFilterResult = mock(RequestInfo.class);
    RequestInfo<?> secondFilterResult = mock(RequestInfo.class);
    // Do a mix of empty Optional vs null for the response to hit branch coverage (both indicate no short circuiting response)
    doReturn(Pair.of(firstFilterResult, Optional.empty())).when(filter1Mock).filterRequestFirstChunkWithOptionalShortCircuitResponse(any(), any());
    doReturn(Pair.of(firstFilterResult, null)).when(filter1Mock).filterRequestLastChunkWithOptionalShortCircuitResponse(any(), any());
    doReturn(Pair.of(secondFilterResult, null)).when(filter2Mock).filterRequestFirstChunkWithOptionalShortCircuitResponse(any(), any());
    doReturn(Pair.of(secondFilterResult, Optional.empty())).when(filter2Mock).filterRequestLastChunkWithOptionalShortCircuitResponse(any(), any());
    // when
    PipelineContinuationBehavior result = handlerSpy.handleFilterLogic(ctxMock, args.msg, args.httpState, args.normalFilterCall, args.shortCircuitFilterCall);
    // then
    assertThat(result).isEqualTo(CONTINUE);
    // First filter should have been passed the original request.
    if (isFirstChunk)
        verify(filter1Mock).filterRequestFirstChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
    else
        verify(filter1Mock).filterRequestLastChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
    // Second filter should have been passed the result of the first filter.
    if (isFirstChunk)
        verify(filter2Mock).filterRequestFirstChunkWithOptionalShortCircuitResponse(firstFilterResult, ctxMock);
    else
        verify(filter2Mock).filterRequestLastChunkWithOptionalShortCircuitResponse(firstFilterResult, ctxMock);
    // The state should have been updated with the result of the second filter.
    assertThat(state.getRequestInfo()).isSameAs(secondFilterResult);
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 92 with PipelineContinuationBehavior

use of com.nike.riposte.server.handler.base.PipelineContinuationBehavior in project riposte by Nike-Inc.

the class RequestFilterHandlerTest method handleFilterLogic_does_not_short_circuit_if_responseInfo_is_chunked.

@DataProvider(value = { "true   |   0   |   true", "true   |   0   |   false", "true   |   1   |   true", "true   |   1   |   false", "false  |   0   |   true", "false  |   0   |   false", "false  |   1   |   true", "false  |   1   |   false" }, splitBy = "\\|")
@Test
public void handleFilterLogic_does_not_short_circuit_if_responseInfo_is_chunked(boolean isFirstChunk, int shortCircuitingFilterIndex, boolean filterReturnsModifiedRequestInfo) {
    // given
    HandleFilterLogicMethodCallArgs args = new HandleFilterLogicMethodCallArgs(isFirstChunk);
    RequestAndResponseFilter shortCircuitingFilter = filtersList.get(shortCircuitingFilterIndex);
    doReturn(true).when(shortCircuitingFilter).isShortCircuitRequestFilter();
    RequestInfo<?> modifiedRequestInfoMock = mock(RequestInfo.class);
    RequestInfo<?> returnedRequestInfo = (filterReturnsModifiedRequestInfo) ? modifiedRequestInfoMock : null;
    ResponseInfo<?> chunkedResponseInfoMock = mock(ResponseInfo.class);
    doReturn(true).when(chunkedResponseInfoMock).isChunkedResponse();
    doReturn(Pair.of(returnedRequestInfo, Optional.of(chunkedResponseInfoMock))).when(shortCircuitingFilter).filterRequestFirstChunkWithOptionalShortCircuitResponse(any(), any());
    doReturn(Pair.of(returnedRequestInfo, Optional.of(chunkedResponseInfoMock))).when(shortCircuitingFilter).filterRequestLastChunkWithOptionalShortCircuitResponse(any(), any());
    // when
    PipelineContinuationBehavior result = handlerSpy.handleFilterLogic(ctxMock, args.msg, args.httpState, args.normalFilterCall, args.shortCircuitFilterCall);
    // then
    // Pipeline continues - no short circuit.
    assertThat(result).isEqualTo(CONTINUE);
    // The filter's short-circuit-capable method was called.
    if (isFirstChunk)
        verify(shortCircuitingFilter).filterRequestFirstChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
    else
        verify(shortCircuitingFilter).filterRequestLastChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
    // The state is updated with the correct RequestInfo
    if (filterReturnsModifiedRequestInfo)
        assertThat(state.getRequestInfo()).isSameAs(modifiedRequestInfoMock);
    else
        assertThat(state.getRequestInfo()).isSameAs(requestInfoMock);
    // The state is NOT updated with the ResponseInfo returned by the filter.
    assertThat(state.getResponseInfo()).isNull();
    // The short circuiting "we're all done, return the response to the caller" event is NOT fired down the pipeline.
    verify(ctxMock, never()).fireChannelRead(LastOutboundMessageSendFullResponseInfo.INSTANCE);
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) RequestAndResponseFilter(com.nike.riposte.server.http.filter.RequestAndResponseFilter) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Aggregations

PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)92 Test (org.junit.Test)90 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)26 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)11 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)9 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)8 RequestInfo (com.nike.riposte.server.http.RequestInfo)7 Span (com.nike.wingtips.Span)6 IncompleteHttpCallTimeoutException (com.nike.riposte.server.error.exception.IncompleteHttpCallTimeoutException)5 TooManyOpenChannelsException (com.nike.riposte.server.error.exception.TooManyOpenChannelsException)5 ErrorResponseBody (com.nike.riposte.server.error.handler.ErrorResponseBody)5 RequestAndResponseFilter (com.nike.riposte.server.http.filter.RequestAndResponseFilter)5 HttpRequest (io.netty.handler.codec.http.HttpRequest)5 Pair (com.nike.internal.util.Pair)4 LastOutboundMessageSendFullResponseInfo (com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendFullResponseInfo)4 ResponseInfo (com.nike.riposte.server.http.ResponseInfo)4 ChannelFuture (io.netty.channel.ChannelFuture)4 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)4 InvalidRipostePipelineException (com.nike.riposte.server.error.exception.InvalidRipostePipelineException)3 UseDataProvider (com.tngtech.java.junit.dataprovider.UseDataProvider)3