Search in sources :

Example 1 with ResponseFilterHandler

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

the class HttpChannelInitializerTest method constructor_works_with_valid_args.

@Test
public void constructor_works_with_valid_args() {
    // given
    SslContext sslCtx = mock(SslContext.class);
    int maxRequestSizeInBytes = 42;
    Collection<Endpoint<?>> endpoints = Arrays.asList(getMockEndpoint("/some/path", HttpMethod.GET));
    RequestAndResponseFilter beforeSecurityRequestFilter = mock(RequestAndResponseFilter.class);
    doReturn(true).when(beforeSecurityRequestFilter).shouldExecuteBeforeSecurityValidation();
    RequestAndResponseFilter afterSecurityRequestFilter = mock(RequestAndResponseFilter.class);
    doReturn(false).when(afterSecurityRequestFilter).shouldExecuteBeforeSecurityValidation();
    List<RequestAndResponseFilter> reqResFilters = Arrays.asList(beforeSecurityRequestFilter, afterSecurityRequestFilter);
    Executor longRunningTaskExecutor = mock(Executor.class);
    RiposteErrorHandler riposteErrorHandler = mock(RiposteErrorHandler.class);
    RiposteUnhandledErrorHandler riposteUnhandledErrorHandler = mock(RiposteUnhandledErrorHandler.class);
    RequestValidator validationService = mock(RequestValidator.class);
    ObjectMapper requestContentDeserializer = mock(ObjectMapper.class);
    ResponseSender responseSender = mock(ResponseSender.class);
    @SuppressWarnings("unchecked") MetricsListener metricsListener = mock(MetricsListener.class);
    long defaultCompletableFutureTimeoutMillis = 4242L;
    AccessLogger accessLogger = mock(AccessLogger.class);
    List<PipelineCreateHook> pipelineCreateHooks = mock(List.class);
    RequestSecurityValidator requestSecurityValidator = mock(RequestSecurityValidator.class);
    long workerChannelIdleTimeoutMillis = 121000;
    long proxyRouterConnectTimeoutMillis = 4200;
    long incompleteHttpCallTimeoutMillis = 1234;
    int maxOpenChannelsThreshold = 1000;
    boolean debugChannelLifecycleLoggingEnabled = true;
    List<String> userIdHeaderKeys = mock(List.class);
    int responseCompressionThresholdBytes = 5678;
    HttpRequestDecoderConfig httpRequestDecoderConfig = new HttpRequestDecoderConfig() {
    };
    DistributedTracingConfig<Span> distributedTracingConfig = mock(DistributedTracingConfig.class);
    ProxyRouterSpanNamingAndTaggingStrategy<Span> proxySpanTaggingStrategyMock = mock(ProxyRouterSpanNamingAndTaggingStrategy.class);
    doReturn(proxySpanTaggingStrategyMock).when(distributedTracingConfig).getProxyRouterSpanNamingAndTaggingStrategy();
    // when
    HttpChannelInitializer hci = new HttpChannelInitializer(sslCtx, maxRequestSizeInBytes, endpoints, reqResFilters, longRunningTaskExecutor, riposteErrorHandler, riposteUnhandledErrorHandler, validationService, requestContentDeserializer, responseSender, metricsListener, defaultCompletableFutureTimeoutMillis, accessLogger, pipelineCreateHooks, requestSecurityValidator, workerChannelIdleTimeoutMillis, proxyRouterConnectTimeoutMillis, incompleteHttpCallTimeoutMillis, maxOpenChannelsThreshold, debugChannelLifecycleLoggingEnabled, userIdHeaderKeys, responseCompressionThresholdBytes, httpRequestDecoderConfig, distributedTracingConfig);
    // then
    assertThat(extractField(hci, "sslCtx"), is(sslCtx));
    assertThat(extractField(hci, "maxRequestSizeInBytes"), is(maxRequestSizeInBytes));
    assertThat(extractField(hci, "endpoints"), is(endpoints));
    assertThat(extractField(hci, "longRunningTaskExecutor"), is(longRunningTaskExecutor));
    assertThat(extractField(hci, "riposteErrorHandler"), is(riposteErrorHandler));
    assertThat(extractField(hci, "riposteUnhandledErrorHandler"), is(riposteUnhandledErrorHandler));
    assertThat(extractField(hci, "validationService"), is(validationService));
    assertThat(extractField(hci, "requestContentDeserializer"), is(requestContentDeserializer));
    assertThat(extractField(hci, "responseSender"), is(responseSender));
    assertThat(extractField(hci, "metricsListener"), is(metricsListener));
    assertThat(extractField(hci, "defaultCompletableFutureTimeoutMillis"), is(defaultCompletableFutureTimeoutMillis));
    assertThat(extractField(hci, "accessLogger"), is(accessLogger));
    assertThat(extractField(hci, "pipelineCreateHooks"), is(pipelineCreateHooks));
    assertThat(extractField(hci, "requestSecurityValidator"), is(requestSecurityValidator));
    assertThat(extractField(hci, "workerChannelIdleTimeoutMillis"), is(workerChannelIdleTimeoutMillis));
    assertThat(extractField(hci, "incompleteHttpCallTimeoutMillis"), is(incompleteHttpCallTimeoutMillis));
    assertThat(extractField(hci, "maxOpenChannelsThreshold"), is(maxOpenChannelsThreshold));
    assertThat(extractField(hci, "debugChannelLifecycleLoggingEnabled"), is(debugChannelLifecycleLoggingEnabled));
    assertThat(extractField(hci, "userIdHeaderKeys"), is(userIdHeaderKeys));
    assertThat(extractField(hci, "responseCompressionThresholdBytes"), is(responseCompressionThresholdBytes));
    assertThat(extractField(hci, "httpRequestDecoderConfig"), is(httpRequestDecoderConfig));
    assertThat(extractField(hci, "distributedTracingConfig"), is(distributedTracingConfig));
    StreamingAsyncHttpClient sahc = extractField(hci, "streamingAsyncHttpClientForProxyRouterEndpoints");
    assertThat(extractField(sahc, "idleChannelTimeoutMillis"), is(workerChannelIdleTimeoutMillis));
    assertThat(extractField(sahc, "downstreamConnectionTimeoutMillis"), is((int) proxyRouterConnectTimeoutMillis));
    assertThat(extractField(sahc, "debugChannelLifecycleLoggingEnabled"), is(debugChannelLifecycleLoggingEnabled));
    assertThat(extractField(sahc, "proxySpanTaggingStrategy"), is(proxySpanTaggingStrategyMock));
    RequestFilterHandler beforeSecReqFH = extractField(hci, "beforeSecurityRequestFilterHandler");
    assertThat(extractField(beforeSecReqFH, "filters"), is(Collections.singletonList(beforeSecurityRequestFilter)));
    RequestFilterHandler afterSecReqFH = extractField(hci, "afterSecurityRequestFilterHandler");
    assertThat(extractField(afterSecReqFH, "filters"), is(Collections.singletonList(afterSecurityRequestFilter)));
    ResponseFilterHandler resFH = extractField(hci, "cachedResponseFilterHandler");
    List<RequestAndResponseFilter> reversedFilters = new ArrayList<>(reqResFilters);
    Collections.reverse(reversedFilters);
    assertThat(extractField(resFH, "filtersInResponseProcessingOrder"), is(reversedFilters));
}
Also used : RiposteUnhandledErrorHandler(com.nike.riposte.server.error.handler.RiposteUnhandledErrorHandler) ResponseFilterHandler(com.nike.riposte.server.handler.ResponseFilterHandler) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) PipelineCreateHook(com.nike.riposte.server.hooks.PipelineCreateHook) Span(com.nike.wingtips.Span) RiposteErrorHandler(com.nike.riposte.server.error.handler.RiposteErrorHandler) ResponseSender(com.nike.riposte.server.http.ResponseSender) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) Executor(java.util.concurrent.Executor) Endpoint(com.nike.riposte.server.http.Endpoint) RequestValidator(com.nike.riposte.server.error.validation.RequestValidator) HttpRequestDecoderConfig(com.nike.riposte.server.config.ServerConfig.HttpRequestDecoderConfig) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) SslContext(io.netty.handler.ssl.SslContext) StreamingAsyncHttpClient(com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient) RequestAndResponseFilter(com.nike.riposte.server.http.filter.RequestAndResponseFilter) RequestSecurityValidator(com.nike.riposte.server.error.validation.RequestSecurityValidator) Endpoint(com.nike.riposte.server.http.Endpoint) RequestFilterHandler(com.nike.riposte.server.handler.RequestFilterHandler) MetricsListener(com.nike.riposte.metrics.MetricsListener) AccessLogger(com.nike.riposte.server.logging.AccessLogger) Test(org.junit.Test)

Example 2 with ResponseFilterHandler

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

the class HttpChannelInitializerTest method constructor_handles_empty_after_security_request_handlers.

@Test
public void constructor_handles_empty_after_security_request_handlers() {
    // given
    RequestAndResponseFilter beforeSecurityRequestFilter = mock(RequestAndResponseFilter.class);
    doReturn(true).when(beforeSecurityRequestFilter).shouldExecuteBeforeSecurityValidation();
    List<RequestAndResponseFilter> reqResFilters = Arrays.asList(beforeSecurityRequestFilter);
    // when
    HttpChannelInitializer hci = new HttpChannelInitializer(null, 42, Arrays.asList(getMockEndpoint("/some/path")), reqResFilters, null, mock(RiposteErrorHandler.class), mock(RiposteUnhandledErrorHandler.class), null, null, mock(ResponseSender.class), null, 4242L, null, null, null, 121, 42, 321, 100, false, null, 123, null, mock(DistributedTracingConfig.class));
    // then
    RequestFilterHandler beforeSecReqFH = extractField(hci, "beforeSecurityRequestFilterHandler");
    assertThat(extractField(beforeSecReqFH, "filters"), is(Collections.singletonList(beforeSecurityRequestFilter)));
    assertThat(extractField(hci, "afterSecurityRequestFilterHandler"), nullValue());
    ResponseFilterHandler responseFilterHandler = extractField(hci, "cachedResponseFilterHandler");
    assertThat(extractField(responseFilterHandler, "filtersInResponseProcessingOrder"), is(reqResFilters));
}
Also used : RiposteUnhandledErrorHandler(com.nike.riposte.server.error.handler.RiposteUnhandledErrorHandler) DistributedTracingConfig(com.nike.riposte.server.config.distributedtracing.DistributedTracingConfig) RequestFilterHandler(com.nike.riposte.server.handler.RequestFilterHandler) RequestAndResponseFilter(com.nike.riposte.server.http.filter.RequestAndResponseFilter) ResponseFilterHandler(com.nike.riposte.server.handler.ResponseFilterHandler) RiposteErrorHandler(com.nike.riposte.server.error.handler.RiposteErrorHandler) ResponseSender(com.nike.riposte.server.http.ResponseSender) Test(org.junit.Test)

Example 3 with ResponseFilterHandler

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

the class HttpChannelInitializerTest method initChannel_adds_ResponseFilterHandler_after_ExceptionHandlingHandler_and_before_ResponseSenderHandler_and_uses_cached_handler.

@Test
public void initChannel_adds_ResponseFilterHandler_after_ExceptionHandlingHandler_and_before_ResponseSenderHandler_and_uses_cached_handler() {
    // given
    HttpChannelInitializer hci = basicHttpChannelInitializer(null, 0, 42, false, null, createRequestAndResponseFilterMock());
    // when
    hci.initChannel(socketChannelMock);
    // then
    ArgumentCaptor<ChannelHandler> channelHandlerArgumentCaptor = ArgumentCaptor.forClass(ChannelHandler.class);
    verify(channelPipelineMock, atLeastOnce()).addLast(anyString(), channelHandlerArgumentCaptor.capture());
    List<ChannelHandler> handlers = channelHandlerArgumentCaptor.getAllValues();
    Pair<Integer, ExceptionHandlingHandler> exceptionHandler = findChannelHandler(handlers, ExceptionHandlingHandler.class);
    Pair<Integer, ResponseFilterHandler> responseFilterHandler = findChannelHandler(handlers, ResponseFilterHandler.class);
    Pair<Integer, ResponseSenderHandler> responseSenderHandler = findChannelHandler(handlers, ResponseSenderHandler.class);
    assertThat(exceptionHandler, notNullValue());
    assertThat(responseFilterHandler, notNullValue());
    assertThat(responseSenderHandler, notNullValue());
    Assertions.assertThat(responseFilterHandler.getLeft()).isGreaterThan(exceptionHandler.getLeft());
    Assertions.assertThat(responseFilterHandler.getLeft()).isLessThan(responseSenderHandler.getLeft());
    // and then
    ResponseFilterHandler cachedHandler = extractField(hci, "cachedResponseFilterHandler");
    Assertions.assertThat(responseFilterHandler.getRight()).isSameAs(cachedHandler);
}
Also used : ExceptionHandlingHandler(com.nike.riposte.server.handler.ExceptionHandlingHandler) ResponseFilterHandler(com.nike.riposte.server.handler.ResponseFilterHandler) ChannelHandler(io.netty.channel.ChannelHandler) ResponseSenderHandler(com.nike.riposte.server.handler.ResponseSenderHandler) Test(org.junit.Test)

Example 4 with ResponseFilterHandler

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

the class HttpChannelInitializerTest method constructor_handles_empty_before_security_request_handlers.

@Test
public void constructor_handles_empty_before_security_request_handlers() {
    // given
    RequestAndResponseFilter afterSecurityRequestFilter = mock(RequestAndResponseFilter.class);
    doReturn(false).when(afterSecurityRequestFilter).shouldExecuteBeforeSecurityValidation();
    List<RequestAndResponseFilter> reqResFilters = Arrays.asList(afterSecurityRequestFilter);
    // when
    HttpChannelInitializer hci = new HttpChannelInitializer(null, 42, Arrays.asList(getMockEndpoint("/some/path")), reqResFilters, null, mock(RiposteErrorHandler.class), mock(RiposteUnhandledErrorHandler.class), null, null, mock(ResponseSender.class), null, 4242L, null, null, null, 121, 42, 321, 100, false, null, 123, null, mock(DistributedTracingConfig.class));
    // then
    RequestFilterHandler beforeSecReqFH = extractField(hci, "afterSecurityRequestFilterHandler");
    assertThat(extractField(beforeSecReqFH, "filters"), is(Collections.singletonList(afterSecurityRequestFilter)));
    assertThat(extractField(hci, "beforeSecurityRequestFilterHandler"), nullValue());
    ResponseFilterHandler responseFilterHandler = extractField(hci, "cachedResponseFilterHandler");
    assertThat(extractField(responseFilterHandler, "filtersInResponseProcessingOrder"), is(reqResFilters));
}
Also used : RiposteUnhandledErrorHandler(com.nike.riposte.server.error.handler.RiposteUnhandledErrorHandler) DistributedTracingConfig(com.nike.riposte.server.config.distributedtracing.DistributedTracingConfig) RequestFilterHandler(com.nike.riposte.server.handler.RequestFilterHandler) RequestAndResponseFilter(com.nike.riposte.server.http.filter.RequestAndResponseFilter) ResponseFilterHandler(com.nike.riposte.server.handler.ResponseFilterHandler) RiposteErrorHandler(com.nike.riposte.server.error.handler.RiposteErrorHandler) ResponseSender(com.nike.riposte.server.http.ResponseSender) Test(org.junit.Test)

Aggregations

ResponseFilterHandler (com.nike.riposte.server.handler.ResponseFilterHandler)4 Test (org.junit.Test)4 RiposteErrorHandler (com.nike.riposte.server.error.handler.RiposteErrorHandler)3 RiposteUnhandledErrorHandler (com.nike.riposte.server.error.handler.RiposteUnhandledErrorHandler)3 RequestFilterHandler (com.nike.riposte.server.handler.RequestFilterHandler)3 ResponseSender (com.nike.riposte.server.http.ResponseSender)3 RequestAndResponseFilter (com.nike.riposte.server.http.filter.RequestAndResponseFilter)3 DistributedTracingConfig (com.nike.riposte.server.config.distributedtracing.DistributedTracingConfig)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 StreamingAsyncHttpClient (com.nike.riposte.client.asynchttp.netty.StreamingAsyncHttpClient)1 MetricsListener (com.nike.riposte.metrics.MetricsListener)1 HttpRequestDecoderConfig (com.nike.riposte.server.config.ServerConfig.HttpRequestDecoderConfig)1 RequestSecurityValidator (com.nike.riposte.server.error.validation.RequestSecurityValidator)1 RequestValidator (com.nike.riposte.server.error.validation.RequestValidator)1 ExceptionHandlingHandler (com.nike.riposte.server.handler.ExceptionHandlingHandler)1 ResponseSenderHandler (com.nike.riposte.server.handler.ResponseSenderHandler)1 PipelineCreateHook (com.nike.riposte.server.hooks.PipelineCreateHook)1 Endpoint (com.nike.riposte.server.http.Endpoint)1 AccessLogger (com.nike.riposte.server.logging.AccessLogger)1 Span (com.nike.wingtips.Span)1