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);
}
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);
}
Aggregations