Search in sources :

Example 31 with PipelineContinuationBehavior

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

the class RequestFilterHandlerTest method doChannelRead_delegates_to_handleFilterLogic_with_last_chunk_method_references_when_msg_is_LastHttpContent.

@DataProvider(value = { "CONTINUE", "DO_NOT_FIRE_CONTINUE_EVENT" }, splitBy = "\\|")
@Test
public void doChannelRead_delegates_to_handleFilterLogic_with_last_chunk_method_references_when_msg_is_LastHttpContent(PipelineContinuationBehavior expectedPipelineContinuationBehavior) throws Exception {
    // given
    doReturn(expectedPipelineContinuationBehavior).when(handlerSpy).handleFilterLogic(any(), any(), any(), any(), any());
    // when
    PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, lastChunkMsgMock);
    // then
    assertThat(result).isEqualTo(expectedPipelineContinuationBehavior);
    ArgumentCaptor<BiFunction> normalFilterCallCaptor = ArgumentCaptor.forClass(BiFunction.class);
    ArgumentCaptor<BiFunction> shortCircuitFilterCallCaptor = ArgumentCaptor.forClass(BiFunction.class);
    verify(handlerSpy).handleFilterLogic(eq(ctxMock), eq(lastChunkMsgMock), eq(state), normalFilterCallCaptor.capture(), shortCircuitFilterCallCaptor.capture());
    BiFunction<RequestAndResponseFilter, RequestInfo, RequestInfo> normalFilterCall = normalFilterCallCaptor.getValue();
    BiFunction<RequestAndResponseFilter, RequestInfo, Pair<RequestInfo, Optional<ResponseInfo<?>>>> shortCircuitFilterCall = shortCircuitFilterCallCaptor.getValue();
    RequestAndResponseFilter filterForNormalCallMock = mock(RequestAndResponseFilter.class);
    normalFilterCall.apply(filterForNormalCallMock, requestInfoMock);
    verify(filterForNormalCallMock).filterRequestLastChunkWithFullPayload(requestInfoMock, ctxMock);
    RequestAndResponseFilter filterForShortCircuitCallMock = mock(RequestAndResponseFilter.class);
    shortCircuitFilterCall.apply(filterForShortCircuitCallMock, requestInfoMock);
    verify(filterForShortCircuitCallMock).filterRequestLastChunkWithOptionalShortCircuitResponse(requestInfoMock, ctxMock);
}
Also used : ResponseInfo(com.nike.riposte.server.http.ResponseInfo) LastOutboundMessageSendFullResponseInfo(com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendFullResponseInfo) PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) BiFunction(java.util.function.BiFunction) RequestAndResponseFilter(com.nike.riposte.server.http.filter.RequestAndResponseFilter) RequestInfo(com.nike.riposte.server.http.RequestInfo) Pair(com.nike.internal.util.Pair) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 32 with PipelineContinuationBehavior

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

the class RequestFilterHandlerTest method handleFilterLogic_short_circuits_as_expected_if_filter_returns_valid_response.

@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_short_circuits_as_expected_if_filter_returns_valid_response(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<?> returnedResponseInfoMock = mock(ResponseInfo.class);
    doReturn(Pair.of(returnedRequestInfo, Optional.of(returnedResponseInfoMock))).when(shortCircuitingFilter).filterRequestFirstChunkWithOptionalShortCircuitResponse(any(), any());
    doReturn(Pair.of(returnedRequestInfo, Optional.of(returnedResponseInfoMock))).when(shortCircuitingFilter).filterRequestLastChunkWithOptionalShortCircuitResponse(any(), any());
    // when
    PipelineContinuationBehavior result = handlerSpy.handleFilterLogic(ctxMock, args.msg, args.httpState, args.normalFilterCall, args.shortCircuitFilterCall);
    // then
    // Pipeline stops for the given msg event.
    assertThat(result).isEqualTo(DO_NOT_FIRE_CONTINUE_EVENT);
    // 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 depending on what the filter returned.
    if (filterReturnsModifiedRequestInfo)
        assertThat(state.getRequestInfo()).isSameAs(modifiedRequestInfoMock);
    else
        assertThat(state.getRequestInfo()).isSameAs(requestInfoMock);
    // The state is updated with the ResponseInfo returned by the filter.
    assertThat(state.getResponseInfo()).isSameAs(returnedResponseInfoMock);
    // The short circuiting "we're all done, return the response to the caller" event is fired down the pipeline.
    verify(ctxMock).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)

Example 33 with PipelineContinuationBehavior

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

the class RoutingHandlerTest method doChannelRead_does_nothing_if_msg_is_not_HttpRequest.

@Test
public void doChannelRead_does_nothing_if_msg_is_not_HttpRequest() {
    // given
    String pathTemplate = "/some/path/with/{id}";
    Collection<String> pathTemplates = new ArrayList<String>() {

        {
            add(pathTemplate);
        }
    };
    doReturn(pathTemplates).when(matcherMock).matchingPathTemplates();
    HttpObject msg = mock(HttpObject.class);
    // when
    PipelineContinuationBehavior result = handlerSpy.doChannelRead(ctxMock, msg);
    // then
    verify(handlerSpy).doChannelRead(ctxMock, msg);
    verifyNoMoreInteractions(handlerSpy);
    verifyNoMoreInteractions(requestInfoMock);
    verifyNoMoreInteractions(stateMock);
    assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) HttpObject(io.netty.handler.codec.http.HttpObject) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.Test)

Example 34 with PipelineContinuationBehavior

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

the class RequestContentValidationHandlerTest method doChannelRead_does_nothing_if_endpoint_does_not_want_to_validate.

@Test
public void doChannelRead_does_nothing_if_endpoint_does_not_want_to_validate() throws Exception {
    // given
    doReturn(false).when(endpointMock).isValidateRequestContent(any());
    // when
    PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, msg);
    // then
    verifyNoMoreInteractions(requestValidatorMock);
    assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) Test(org.junit.Test)

Example 35 with PipelineContinuationBehavior

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

the class RequestContentValidationHandlerTest method doChannelRead_calls_request_getContent_method_if_endpoint_wants_validation.

@Test
public void doChannelRead_calls_request_getContent_method_if_endpoint_wants_validation() throws Exception {
    // when
    PipelineContinuationBehavior result = handler.doChannelRead(ctxMock, msg);
    // then
    verify(requestInfoMock).getContent();
    assertThat(result).isEqualTo(PipelineContinuationBehavior.CONTINUE);
}
Also used : PipelineContinuationBehavior(com.nike.riposte.server.handler.base.PipelineContinuationBehavior) 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