Search in sources :

Example 91 with ChannelHandler

use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.

the class ChannelPipelineFinalizerHandlerTest method finalizeChannelPipeline_should_add_idle_channel_timeout_handler_first_in_pipeline_if_workerChannelIdleTimeoutMillis_is_greater_than_0.

@Test
public void finalizeChannelPipeline_should_add_idle_channel_timeout_handler_first_in_pipeline_if_workerChannelIdleTimeoutMillis_is_greater_than_0() throws JsonProcessingException {
    // given
    LastOutboundMessage msg = mock(LastOutboundMessage.class);
    // when
    handler.finalizeChannelPipeline(ctxMock, msg, state, null);
    // then
    ArgumentCaptor<ChannelHandler> idleHandlerArgCaptor = ArgumentCaptor.forClass(ChannelHandler.class);
    verify(pipelineMock).addFirst(eq(IDLE_CHANNEL_TIMEOUT_HANDLER_NAME), idleHandlerArgCaptor.capture());
    ChannelHandler handlerRegistered = idleHandlerArgCaptor.getValue();
    assertThat(handlerRegistered, instanceOf(IdleChannelTimeoutHandler.class));
    IdleChannelTimeoutHandler idleHandler = (IdleChannelTimeoutHandler) handlerRegistered;
    long idleValue = (long) Whitebox.getInternalState(idleHandler, "idleTimeoutMillis");
    assertThat(idleValue, is(workerChannelIdleTimeoutMillis));
}
Also used : LastOutboundMessage(com.nike.riposte.server.channelpipeline.message.LastOutboundMessage) ChannelHandler(io.netty.channel.ChannelHandler) Test(org.junit.Test)

Example 92 with ChannelHandler

use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.

the class HttpChannelInitializerTest method initChannel_adds_ResponseSenderHandler_after_NonblockingEndpointExecutionHandler_and_uses_responseSender.

@Test
public void initChannel_adds_ResponseSenderHandler_after_NonblockingEndpointExecutionHandler_and_uses_responseSender() {
    // given
    HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers();
    ResponseSender expectedResponseSender = extractField(hci, "responseSender");
    // 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, NonblockingEndpointExecutionHandler> nonblockingEndpointExecutionHandler = findChannelHandler(handlers, NonblockingEndpointExecutionHandler.class);
    Pair<Integer, ResponseSenderHandler> responseSenderHandler = findChannelHandler(handlers, ResponseSenderHandler.class);
    assertThat(nonblockingEndpointExecutionHandler, notNullValue());
    assertThat(responseSenderHandler, notNullValue());
    assertThat(responseSenderHandler.getLeft(), is(greaterThan(nonblockingEndpointExecutionHandler.getLeft())));
    // and then
    ResponseSender actualResponseSender = (ResponseSender) Whitebox.getInternalState(responseSenderHandler.getRight(), "responseSender");
    assertThat(actualResponseSender, is(expectedResponseSender));
}
Also used : ChannelHandler(io.netty.channel.ChannelHandler) NonblockingEndpointExecutionHandler(com.nike.riposte.server.handler.NonblockingEndpointExecutionHandler) ResponseSender(com.nike.riposte.server.http.ResponseSender) ResponseSenderHandler(com.nike.riposte.server.handler.ResponseSenderHandler) Test(org.junit.Test)

Example 93 with ChannelHandler

use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.

the class HttpChannelInitializerTest method initChannel_adds_before_and_after_RequestFilterHandler_appropriately_before_and_after_security_filter.

@Test
public void initChannel_adds_before_and_after_RequestFilterHandler_appropriately_before_and_after_security_filter() {
    // given
    RequestAndResponseFilter beforeSecurityRequestFilter = mock(RequestAndResponseFilter.class);
    doReturn(true).when(beforeSecurityRequestFilter).shouldExecuteBeforeSecurityValidation();
    RequestAndResponseFilter afterSecurityRequestFilter = mock(RequestAndResponseFilter.class);
    doReturn(false).when(afterSecurityRequestFilter).shouldExecuteBeforeSecurityValidation();
    List<RequestAndResponseFilter> requestAndResponseFilters = Arrays.asList(beforeSecurityRequestFilter, afterSecurityRequestFilter);
    HttpChannelInitializer hci = basicHttpChannelInitializer(null, 0, 42, false, null, requestAndResponseFilters);
    // 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, AccessLogStartHandler> accessLogStartHandler = findChannelHandler(handlers, AccessLogStartHandler.class);
    Pair<Integer, RequestFilterHandler> beforeSecurityRequestFilterHandler = findChannelHandler(handlers, RequestFilterHandler.class);
    Pair<Integer, RequestFilterHandler> afterSecurityRequestFilterHandler = findChannelHandler(handlers, RequestFilterHandler.class, true);
    Pair<Integer, RoutingHandler> routingHandler = findChannelHandler(handlers, RoutingHandler.class);
    Pair<Integer, SecurityValidationHandler> securityValidationHandler = findChannelHandler(handlers, SecurityValidationHandler.class);
    Pair<Integer, RequestContentDeserializerHandler> requestContentDeserializerHandler = findChannelHandler(handlers, RequestContentDeserializerHandler.class);
    assertThat(accessLogStartHandler, notNullValue());
    assertThat(beforeSecurityRequestFilterHandler, notNullValue());
    assertThat(routingHandler, notNullValue());
    assertThat(afterSecurityRequestFilterHandler, notNullValue());
    assertThat(securityValidationHandler, notNullValue());
    assertThat(requestContentDeserializerHandler, notNullValue());
    Assertions.assertThat(beforeSecurityRequestFilterHandler.getLeft()).isGreaterThan(accessLogStartHandler.getLeft());
    Assertions.assertThat(beforeSecurityRequestFilterHandler.getLeft()).isLessThan(securityValidationHandler.getLeft());
    // beforeSecurityRequestFilterHandler must come before RoutingHandler as well (so that it is executed before any 404 gets thrown)
    Assertions.assertThat(beforeSecurityRequestFilterHandler.getLeft()).isLessThan(routingHandler.getLeft());
    Assertions.assertThat(afterSecurityRequestFilterHandler.getLeft()).isGreaterThan(securityValidationHandler.getLeft());
    Assertions.assertThat(afterSecurityRequestFilterHandler.getLeft()).isLessThan(requestContentDeserializerHandler.getLeft());
    // and then
    RequestFilterHandler beforeSecurityCachedHandler = extractField(hci, "beforeSecurityRequestFilterHandler");
    Assertions.assertThat(beforeSecurityRequestFilterHandler.getRight()).isSameAs(beforeSecurityCachedHandler);
    RequestFilterHandler afterSecurityCachedHandler = extractField(hci, "afterSecurityRequestFilterHandler");
    Assertions.assertThat(afterSecurityRequestFilterHandler.getRight()).isSameAs(afterSecurityCachedHandler);
}
Also used : AccessLogStartHandler(com.nike.riposte.server.handler.AccessLogStartHandler) RequestAndResponseFilter(com.nike.riposte.server.http.filter.RequestAndResponseFilter) RequestContentDeserializerHandler(com.nike.riposte.server.handler.RequestContentDeserializerHandler) ChannelHandler(io.netty.channel.ChannelHandler) RequestFilterHandler(com.nike.riposte.server.handler.RequestFilterHandler) RoutingHandler(com.nike.riposte.server.handler.RoutingHandler) SecurityValidationHandler(com.nike.riposte.server.handler.SecurityValidationHandler) Test(org.junit.Test)

Example 94 with ChannelHandler

use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.

the class HttpChannelInitializerTest method initChannel_adds_sslCtx_handler_first_if_available_and_no_utility_handlers.

@Test
public void initChannel_adds_sslCtx_handler_first_if_available_and_no_utility_handlers() throws SSLException {
    // given
    SslContext sslCtx = SslContextBuilder.forClient().build();
    HttpChannelInitializer hci = basicHttpChannelInitializer(sslCtx, 0, 100, false, mock(RequestValidator.class), 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();
    assertThat(handlers.get(0), instanceOf(SslHandler.class));
}
Also used : RequestValidator(com.nike.riposte.server.error.validation.RequestValidator) ChannelHandler(io.netty.channel.ChannelHandler) SslHandler(io.netty.handler.ssl.SslHandler) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.Test)

Example 95 with ChannelHandler

use of io.netty.channel.ChannelHandler 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)

Aggregations

ChannelHandler (io.netty.channel.ChannelHandler)186 Test (org.junit.Test)88 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)44 Channel (io.netty.channel.Channel)26 ChannelPipeline (io.netty.channel.ChannelPipeline)25 SslHandler (io.netty.handler.ssl.SslHandler)25 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)22 FilterChainMatchingHandler (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler)20 ChannelFuture (io.netty.channel.ChannelFuture)20 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)20 FilterChainSelector (io.grpc.xds.FilterChainMatchingProtocolNegotiators.FilterChainMatchingHandler.FilterChainSelector)19 ChannelHandlerAdapter (io.netty.channel.ChannelHandlerAdapter)18 DownstreamTlsContext (io.grpc.xds.EnvoyServerProtoData.DownstreamTlsContext)17 FilterChain (io.grpc.xds.EnvoyServerProtoData.FilterChain)17 InetSocketAddress (java.net.InetSocketAddress)16 Test (org.junit.jupiter.api.Test)16 LineBasedFrameDecoder (io.netty.handler.codec.LineBasedFrameDecoder)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)12 Bootstrap (io.netty.bootstrap.Bootstrap)11 ArrayList (java.util.ArrayList)11