Search in sources :

Example 11 with RequestInfo

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

the class AllowAllTheThingsCORSFilterTest method filterRequestFirstChunkWithOptionalShortCircuitResponse_short_circuits_on_CORS_preflight_OPTIONS_request.

@DataProvider(value = { "OPTIONS    |   true", "GET        |   false", "POST       |   false", "PUT        |   false" }, splitBy = "\\|")
@Test
public void filterRequestFirstChunkWithOptionalShortCircuitResponse_short_circuits_on_CORS_preflight_OPTIONS_request(String httpMethodString, boolean expectShortCircuit) {
    // given
    HttpMethod method = HttpMethod.valueOf(httpMethodString);
    doReturn(method).when(requestMock).getMethod();
    // when
    Pair<RequestInfo<?>, Optional<ResponseInfo<?>>> result = filter.filterRequestFirstChunkWithOptionalShortCircuitResponse(requestMock, ctxMock);
    // then
    if (expectShortCircuit) {
        assertThat(result).isNotNull();
        assertThat(result.getLeft()).isSameAs(requestMock);
        assertThat(result.getRight()).isPresent();
        assertThat(result.getRight().get().getHttpStatusCode()).isEqualTo(200);
    } else
        assertThat(result).isNull();
}
Also used : Optional(java.util.Optional) RequestInfo(com.nike.riposte.server.http.RequestInfo) HttpMethod(io.netty.handler.codec.http.HttpMethod) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 12 with RequestInfo

use of com.nike.riposte.server.http.RequestInfo 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 13 with RequestInfo

use of com.nike.riposte.server.http.RequestInfo 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)

Example 14 with RequestInfo

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

the class RequestInfoImplTest method setupContentDeserializer_and_getContent_sets_content_to_null_if_raw_content_is_null.

@Test
public void setupContentDeserializer_and_getContent_sets_content_to_null_if_raw_content_is_null() throws IOException {
    // given
    RequestInfo<TestContentObject> requestInfo = (RequestInfo<TestContentObject>) RequestInfoImpl.dummyInstanceForUnknownRequests();
    assertThat(requestInfo.getRawContent(), nullValue());
    // when
    requestInfo.setupContentDeserializer(new ObjectMapper(), new TypeReference<TestContentObject>() {
    });
    // then
    assertThat(requestInfo.getContent(), nullValue());
}
Also used : RequestInfo(com.nike.riposte.server.http.RequestInfo) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 15 with RequestInfo

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

the class RiposteUnhandledExceptionHandlerTest method handleErrorFromNettyInterfaceWrapsRequestInfoWithAdapterBeforeContinuing.

@Test
public void handleErrorFromNettyInterfaceWrapsRequestInfoWithAdapterBeforeContinuing() {
    com.nike.backstopper.handler.ErrorResponseInfo<ErrorResponseBody> backstopperResponse = new ErrorResponseInfo<>(42, mock(ErrorResponseBody.class), Collections.emptyMap());
    doReturn(backstopperResponse).when(adapterSpy).handleException(any(Throwable.class), any(RequestInfoForLogging.class));
    RequestInfo requestInfoMock = mock(RequestInfo.class);
    adapterSpy.handleError(new Exception(), requestInfoMock);
    ArgumentCaptor<RequestInfoForLogging> requestInfoForLoggingArgumentCaptor = ArgumentCaptor.forClass(RequestInfoForLogging.class);
    verify(adapterSpy).handleException(any(Throwable.class), requestInfoForLoggingArgumentCaptor.capture());
    RequestInfoForLogging passedArg = requestInfoForLoggingArgumentCaptor.getValue();
    assertThat(passedArg, instanceOf(RequestInfoForLoggingRiposteAdapter.class));
    RequestInfo embeddedRequestInfoInWrapper = (RequestInfo) Whitebox.getInternalState(passedArg, "request");
    assertThat(embeddedRequestInfoInWrapper, sameInstance(requestInfoMock));
}
Also used : ErrorResponseInfo(com.nike.backstopper.handler.ErrorResponseInfo) RequestInfoForLogging(com.nike.backstopper.handler.RequestInfoForLogging) ErrorResponseBody(com.nike.riposte.server.error.handler.ErrorResponseBody) RequestInfo(com.nike.riposte.server.http.RequestInfo) Test(org.junit.Test)

Aggregations

RequestInfo (com.nike.riposte.server.http.RequestInfo)39 Test (org.junit.Test)30 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)13 HttpProcessingState (com.nike.riposte.server.http.HttpProcessingState)10 ResponseInfo (com.nike.riposte.server.http.ResponseInfo)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 PipelineContinuationBehavior (com.nike.riposte.server.handler.base.PipelineContinuationBehavior)6 DataProvider (com.tngtech.java.junit.dataprovider.DataProvider)6 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)6 Pair (com.nike.internal.util.Pair)5 RequestInfoForLogging (com.nike.backstopper.handler.RequestInfoForLogging)4 Endpoint (com.nike.riposte.server.http.Endpoint)4 HttpMethod (io.netty.handler.codec.http.HttpMethod)4 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)4 Optional (java.util.Optional)4 Assertions.catchThrowable (org.assertj.core.api.Assertions.catchThrowable)4 LastOutboundMessageSendFullResponseInfo (com.nike.riposte.server.channelpipeline.message.LastOutboundMessageSendFullResponseInfo)3 ChannelAttributes (com.nike.riposte.server.channelpipeline.ChannelAttributes)2 BaseInboundHandlerWithTracingAndMdcSupport (com.nike.riposte.server.handler.base.BaseInboundHandlerWithTracingAndMdcSupport)2 ProxyRouterProcessingState (com.nike.riposte.server.http.ProxyRouterProcessingState)2