use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.
the class HttpRequestDecoderBenchmark method testDecodeWholeRequestInMultipleSteps.
private static void testDecodeWholeRequestInMultipleSteps(byte[] content, int fragmentSize) {
final EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
final int headerLength = content.length - CONTENT_LENGTH;
// split up the header
for (int a = 0; a < headerLength; ) {
int amount = fragmentSize;
if (a + amount > headerLength) {
amount = headerLength - a;
}
// if header is done it should produce a HttpRequest
channel.writeInbound(Unpooled.wrappedBuffer(content, a, amount));
a += amount;
}
for (int i = CONTENT_LENGTH; i > 0; i--) {
// Should produce HttpContent
channel.writeInbound(Unpooled.wrappedBuffer(content, content.length - i, 1));
}
}
use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.
the class WebSocketClientHandshakerTest method testHttpResponseAndFrameInSameBuffer.
private void testHttpResponseAndFrameInSameBuffer(boolean codec) {
String url = "ws://localhost:9999/ws";
final WebSocketClientHandshaker shaker = newHandshaker(URI.create(url));
final WebSocketClientHandshaker handshaker = new WebSocketClientHandshaker(shaker.uri(), shaker.version(), null, EmptyHttpHeaders.INSTANCE, Integer.MAX_VALUE) {
@Override
protected FullHttpRequest newHandshakeRequest() {
return shaker.newHandshakeRequest();
}
@Override
protected void verify(FullHttpResponse response) {
// Not do any verification, so we not need to care sending the correct headers etc in the test,
// which would just make things more complicated.
}
@Override
protected WebSocketFrameDecoder newWebsocketDecoder() {
return shaker.newWebsocketDecoder();
}
@Override
protected WebSocketFrameEncoder newWebSocketEncoder() {
return shaker.newWebSocketEncoder();
}
};
byte[] data = new byte[24];
PlatformDependent.threadLocalRandom().nextBytes(data);
// Create a EmbeddedChannel which we will use to encode a BinaryWebsocketFrame to bytes and so use these
// to test the actual handshaker.
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(url, null, false);
WebSocketServerHandshaker socketServerHandshaker = factory.newHandshaker(shaker.newHandshakeRequest());
EmbeddedChannel websocketChannel = new EmbeddedChannel(socketServerHandshaker.newWebSocketEncoder(), socketServerHandshaker.newWebsocketDecoder());
assertTrue(websocketChannel.writeOutbound(new BinaryWebSocketFrame(Unpooled.wrappedBuffer(data))));
byte[] bytes = "HTTP/1.1 101 Switching Protocols\r\nContent-Length: 0\r\n\r\n".getBytes(CharsetUtil.US_ASCII);
CompositeByteBuf compositeByteBuf = Unpooled.compositeBuffer();
compositeByteBuf.addComponent(true, Unpooled.wrappedBuffer(bytes));
for (; ; ) {
ByteBuf frameBytes = websocketChannel.readOutbound();
if (frameBytes == null) {
break;
}
compositeByteBuf.addComponent(true, frameBytes);
}
EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(Integer.MAX_VALUE), new SimpleChannelInboundHandler<FullHttpResponse>() {
@Override
protected void channelRead0(ChannelHandlerContext ctx, FullHttpResponse msg) throws Exception {
handshaker.finishHandshake(ctx.channel(), msg);
ctx.pipeline().remove(this);
}
});
if (codec) {
ch.pipeline().addFirst(new HttpClientCodec());
} else {
ch.pipeline().addFirst(new HttpRequestEncoder(), new HttpResponseDecoder());
}
// We need to first write the request as HttpClientCodec will fail if we receive a response before a request
// was written.
shaker.handshake(ch).syncUninterruptibly();
for (; ; ) {
// Just consume the bytes, we are not interested in these.
ByteBuf buf = ch.readOutbound();
if (buf == null) {
break;
}
buf.release();
}
assertTrue(ch.writeInbound(compositeByteBuf));
assertTrue(ch.finish());
BinaryWebSocketFrame frame = ch.readInbound();
ByteBuf expect = Unpooled.wrappedBuffer(data);
try {
assertEquals(expect, frame.content());
assertTrue(frame.isFinalFragment());
assertEquals(0, frame.rsv());
} finally {
expect.release();
frame.release();
}
}
use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.
the class WebSocketFrameAggregatorTest method textFrameTooBig.
@Test
public void textFrameTooBig() throws Exception {
EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(8));
channel.writeInbound(new BinaryWebSocketFrame(true, 1, Unpooled.wrappedBuffer(content1)));
channel.writeInbound(new BinaryWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content1)));
try {
channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
Assert.fail();
} catch (TooLongFrameException e) {
// expected
}
channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
channel.writeInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.wrappedBuffer(content2)));
channel.writeInbound(new BinaryWebSocketFrame(true, 1, Unpooled.wrappedBuffer(content1)));
channel.writeInbound(new BinaryWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content1)));
try {
channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
Assert.fail();
} catch (TooLongFrameException e) {
// expected
}
channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
channel.writeInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.wrappedBuffer(content2)));
for (; ; ) {
Object msg = channel.readInbound();
if (msg == null) {
break;
}
ReferenceCountUtil.release(msg);
}
channel.finish();
}
use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.
the class WebSocketFrameAggregatorTest method testAggregationText.
@Test
public void testAggregationText() {
EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(Integer.MAX_VALUE));
channel.writeInbound(new TextWebSocketFrame(true, 1, Unpooled.wrappedBuffer(content1)));
channel.writeInbound(new TextWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content1)));
channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
channel.writeInbound(new PingWebSocketFrame(Unpooled.wrappedBuffer(content1)));
channel.writeInbound(new PongWebSocketFrame(Unpooled.wrappedBuffer(content1)));
channel.writeInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.wrappedBuffer(content3)));
Assert.assertTrue(channel.finish());
TextWebSocketFrame frame = channel.readInbound();
Assert.assertTrue(frame.isFinalFragment());
Assert.assertEquals(1, frame.rsv());
Assert.assertArrayEquals(content1, toBytes(frame.content()));
PingWebSocketFrame frame2 = channel.readInbound();
Assert.assertTrue(frame2.isFinalFragment());
Assert.assertEquals(0, frame2.rsv());
Assert.assertArrayEquals(content1, toBytes(frame2.content()));
PongWebSocketFrame frame3 = channel.readInbound();
Assert.assertTrue(frame3.isFinalFragment());
Assert.assertEquals(0, frame3.rsv());
Assert.assertArrayEquals(content1, toBytes(frame3.content()));
TextWebSocketFrame frame4 = channel.readInbound();
Assert.assertTrue(frame4.isFinalFragment());
Assert.assertEquals(0, frame4.rsv());
Assert.assertArrayEquals(aggregatedContent, toBytes(frame4.content()));
Assert.assertNull(channel.readInbound());
}
use of io.netty.channel.embedded.EmbeddedChannel in project netty by netty.
the class WebSocketFrameAggregatorTest method testAggregationBinary.
@Test
public void testAggregationBinary() {
EmbeddedChannel channel = new EmbeddedChannel(new WebSocketFrameAggregator(Integer.MAX_VALUE));
channel.writeInbound(new BinaryWebSocketFrame(true, 1, Unpooled.wrappedBuffer(content1)));
channel.writeInbound(new BinaryWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content1)));
channel.writeInbound(new ContinuationWebSocketFrame(false, 0, Unpooled.wrappedBuffer(content2)));
channel.writeInbound(new PingWebSocketFrame(Unpooled.wrappedBuffer(content1)));
channel.writeInbound(new PongWebSocketFrame(Unpooled.wrappedBuffer(content1)));
channel.writeInbound(new ContinuationWebSocketFrame(true, 0, Unpooled.wrappedBuffer(content3)));
Assert.assertTrue(channel.finish());
BinaryWebSocketFrame frame = channel.readInbound();
Assert.assertTrue(frame.isFinalFragment());
Assert.assertEquals(1, frame.rsv());
Assert.assertArrayEquals(content1, toBytes(frame.content()));
PingWebSocketFrame frame2 = channel.readInbound();
Assert.assertTrue(frame2.isFinalFragment());
Assert.assertEquals(0, frame2.rsv());
Assert.assertArrayEquals(content1, toBytes(frame2.content()));
PongWebSocketFrame frame3 = channel.readInbound();
Assert.assertTrue(frame3.isFinalFragment());
Assert.assertEquals(0, frame3.rsv());
Assert.assertArrayEquals(content1, toBytes(frame3.content()));
BinaryWebSocketFrame frame4 = channel.readInbound();
Assert.assertTrue(frame4.isFinalFragment());
Assert.assertEquals(0, frame4.rsv());
Assert.assertArrayEquals(aggregatedContent, toBytes(frame4.content()));
Assert.assertNull(channel.readInbound());
}
Aggregations