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