use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.
the class HttpChannelInitializerTest method initChannel_adds_ExceptionHandlingHandler_immediately_before_ResponseSenderHandler_and_after_NonblockingEndpointExecutionHandler_and_uses_riposteErrorHandler_and_riposteUnhandledErrorHandler.
@Test
public void initChannel_adds_ExceptionHandlingHandler_immediately_before_ResponseSenderHandler_and_after_NonblockingEndpointExecutionHandler_and_uses_riposteErrorHandler_and_riposteUnhandledErrorHandler() {
// given
HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers();
RiposteErrorHandler expectedRiposteErrorHandler = extractField(hci, "riposteErrorHandler");
RiposteUnhandledErrorHandler expectedRiposteUnhandledErrorHandler = extractField(hci, "riposteUnhandledErrorHandler");
DistributedTracingConfig<Span> distributedTracingConfigMock = mock(DistributedTracingConfig.class);
ServerSpanNamingAndTaggingStrategy<Span> serverSpanNamingAndTaggingStrategyMock = mock(ServerSpanNamingAndTaggingStrategy.class);
Whitebox.setInternalState(hci, "distributedTracingConfig", distributedTracingConfigMock);
doReturn(serverSpanNamingAndTaggingStrategyMock).when(distributedTracingConfigMock).getServerSpanNamingAndTaggingStrategy();
// 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, ResponseSenderHandler> responseSenderHandler = findChannelHandler(handlers, ResponseSenderHandler.class);
Pair<Integer, NonblockingEndpointExecutionHandler> nonblockingEndpointExecutionHandler = findChannelHandler(handlers, NonblockingEndpointExecutionHandler.class);
Pair<Integer, ExceptionHandlingHandler> exceptionHandlingHandler = findChannelHandler(handlers, ExceptionHandlingHandler.class);
assertThat(responseSenderHandler, notNullValue());
assertThat(nonblockingEndpointExecutionHandler, notNullValue());
assertThat(exceptionHandlingHandler, notNullValue());
assertThat(exceptionHandlingHandler.getLeft(), is(responseSenderHandler.getLeft() - 1));
assertThat(exceptionHandlingHandler.getLeft(), is(greaterThan(nonblockingEndpointExecutionHandler.getLeft())));
assertThat(extractField(exceptionHandlingHandler.getRight(), "spanNamingAndTaggingStrategy"), is(serverSpanNamingAndTaggingStrategyMock));
// and then
RiposteErrorHandler actualRiposteErrorHandler = (RiposteErrorHandler) Whitebox.getInternalState(exceptionHandlingHandler.getRight(), "riposteErrorHandler");
RiposteUnhandledErrorHandler actualRiposteUnhandledErrorHandler = (RiposteUnhandledErrorHandler) Whitebox.getInternalState(exceptionHandlingHandler.getRight(), "riposteUnhandledErrorHandler");
assertThat(actualRiposteErrorHandler, is(expectedRiposteErrorHandler));
assertThat(actualRiposteUnhandledErrorHandler, is(expectedRiposteUnhandledErrorHandler));
}
use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.
the class HttpChannelInitializerTest method initChannel_adds_HttpServerCodec_with_config_values_coming_from_httpRequestDecoderConfig.
@Test
public void initChannel_adds_HttpServerCodec_with_config_values_coming_from_httpRequestDecoderConfig() {
// given
HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers();
HttpRequestDecoderConfig configWithCustomValues = new HttpRequestDecoderConfig() {
@Override
public int maxInitialLineLength() {
return 123;
}
@Override
public int maxHeaderSize() {
return 456;
}
@Override
public int maxChunkSize() {
return 789;
}
};
Whitebox.setInternalState(hci, "httpRequestDecoderConfig", configWithCustomValues);
// 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, HttpServerCodec> httpServerCodecHandlerPair = findChannelHandler(handlers, HttpServerCodec.class);
Assertions.assertThat(httpServerCodecHandlerPair).isNotNull();
HttpServerCodec httpServerCodecHandler = httpServerCodecHandlerPair.getValue();
HttpRequestDecoder decoderHandler = (HttpRequestDecoder) Whitebox.getInternalState(httpServerCodecHandler, "inboundHandler");
int actualMaxInitialLineLength = extractField(extractField(decoderHandler, "lineParser"), "maxLength");
int actualMaxHeaderSize = extractField(extractField(decoderHandler, "headerParser"), "maxLength");
int actualMaxChunkSize = extractField(decoderHandler, "maxChunkSize");
Assertions.assertThat(actualMaxInitialLineLength).isEqualTo(configWithCustomValues.maxInitialLineLength());
Assertions.assertThat(actualMaxHeaderSize).isEqualTo(configWithCustomValues.maxHeaderSize());
Assertions.assertThat(actualMaxChunkSize).isEqualTo(configWithCustomValues.maxChunkSize());
}
use of io.netty.channel.ChannelHandler in project riposte by Nike-Inc.
the class HttpChannelInitializerTest method initChannel_adds_RequestContentDeserializerHandler_after_RequestInfoSetterHandler_and_RoutingHandler_and_uses_requestContentDeserializer.
@Test
public void initChannel_adds_RequestContentDeserializerHandler_after_RequestInfoSetterHandler_and_RoutingHandler_and_uses_requestContentDeserializer() {
// given
HttpChannelInitializer hci = basicHttpChannelInitializerNoUtilityHandlers();
ObjectMapper expectedRequestContentDeserializer = mock(ObjectMapper.class);
Whitebox.setInternalState(hci, "requestContentDeserializer", expectedRequestContentDeserializer);
// 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, RequestInfoSetterHandler> requestInfoSetterHandler = findChannelHandler(handlers, RequestInfoSetterHandler.class);
Pair<Integer, RoutingHandler> routingHandler = findChannelHandler(handlers, RoutingHandler.class);
Pair<Integer, RequestContentDeserializerHandler> requestContentDeserializerHandler = findChannelHandler(handlers, RequestContentDeserializerHandler.class);
assertThat(requestInfoSetterHandler, notNullValue());
assertThat(routingHandler, notNullValue());
assertThat(requestContentDeserializerHandler, notNullValue());
assertThat(requestContentDeserializerHandler.getLeft(), is(greaterThan(requestInfoSetterHandler.getLeft())));
assertThat(requestContentDeserializerHandler.getLeft(), is(greaterThan(routingHandler.getLeft())));
// and then
ObjectMapper actualRequestContentDeserializer = (ObjectMapper) Whitebox.getInternalState(requestContentDeserializerHandler.getRight(), "defaultRequestContentDeserializer");
assertThat(actualRequestContentDeserializer, is(expectedRequestContentDeserializer));
}
use of io.netty.channel.ChannelHandler in project spring-framework by spring-projects.
the class RxNettyWebSocketSession method aggregateFrames.
/**
* Insert an {@link WebSocketFrameAggregator} after the
* {@code WebSocketFrameDecoder} for receiving full messages.
* @param channel the channel for the session
* @param frameDecoderName the name of the WebSocketFrame decoder
*/
public RxNettyWebSocketSession aggregateFrames(Channel channel, String frameDecoderName) {
ChannelPipeline pipeline = channel.pipeline();
if (pipeline.context(FRAME_AGGREGATOR_NAME) != null) {
return this;
}
ChannelHandlerContext frameDecoder = pipeline.context(frameDecoderName);
if (frameDecoder == null) {
throw new IllegalArgumentException("WebSocketFrameDecoder not found: " + frameDecoderName);
}
ChannelHandler frameAggregator = new WebSocketFrameAggregator(DEFAULT_FRAME_MAX_SIZE);
pipeline.addAfter(frameDecoder.name(), FRAME_AGGREGATOR_NAME, frameAggregator);
return this;
}
use of io.netty.channel.ChannelHandler in project netty by netty.
the class EmbeddedChannel method setup.
private void setup(final ChannelHandler... handlers) {
ObjectUtil.checkNotNull(handlers, "handlers");
ChannelPipeline p = pipeline();
p.addLast(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ChannelPipeline pipeline = ch.pipeline();
for (ChannelHandler h : handlers) {
if (h == null) {
break;
}
pipeline.addLast(h);
}
}
});
ChannelFuture future = loop.register(this);
assert future.isDone();
}
Aggregations