Search in sources :

Example 16 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class HttpToHttp2ConnectionHandlerTest method testChunkedRequestWithBodyAndTrailingHeaders.

@Test
public void testChunkedRequestWithBodyAndTrailingHeaders() throws Exception {
    final String text = "foooooo";
    final String text2 = "goooo";
    final List<String> receivedBuffers = Collections.synchronizedList(new ArrayList<String>());
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock in) throws Throwable {
            receivedBuffers.add(((ByteBuf) in.getArguments()[2]).toString(UTF_8));
            return null;
        }
    }).when(serverListener).onDataRead(any(ChannelHandlerContext.class), eq(3), any(ByteBuf.class), eq(0), eq(false));
    bootstrapEnv(4, 1, 1);
    final HttpRequest request = new DefaultHttpRequest(HTTP_1_1, POST, "http://your_user-name123@www.example.org:5555/example");
    final HttpHeaders httpHeaders = request.headers();
    httpHeaders.set(HttpHeaderNames.HOST, "www.example.org:5555");
    httpHeaders.add(HttpHeaderNames.TRANSFER_ENCODING, "chunked");
    httpHeaders.add(of("foo"), of("goo"));
    httpHeaders.add(of("foo"), of("goo2"));
    httpHeaders.add(of("foo2"), of("goo2"));
    final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("POST")).path(new AsciiString("/example")).authority(new AsciiString("www.example.org:5555")).scheme(new AsciiString("http")).add(new AsciiString("foo"), new AsciiString("goo")).add(new AsciiString("foo"), new AsciiString("goo2")).add(new AsciiString("foo2"), new AsciiString("goo2"));
    final DefaultHttpContent httpContent = new DefaultHttpContent(Unpooled.copiedBuffer(text, UTF_8));
    final LastHttpContent lastHttpContent = new DefaultLastHttpContent(Unpooled.copiedBuffer(text2, UTF_8));
    lastHttpContent.trailingHeaders().add(of("trailing"), of("bar"));
    final Http2Headers http2TrailingHeaders = new DefaultHttp2Headers().add(new AsciiString("trailing"), new AsciiString("bar"));
    ChannelPromise writePromise = newPromise();
    ChannelFuture writeFuture = clientChannel.write(request, writePromise);
    ChannelPromise contentPromise = newPromise();
    ChannelFuture contentFuture = clientChannel.write(httpContent, contentPromise);
    ChannelPromise lastContentPromise = newPromise();
    ChannelFuture lastContentFuture = clientChannel.write(lastHttpContent, lastContentPromise);
    clientChannel.flush();
    assertTrue(writePromise.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(writePromise.isSuccess());
    assertTrue(writeFuture.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(writeFuture.isSuccess());
    assertTrue(contentPromise.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(contentPromise.isSuccess());
    assertTrue(contentFuture.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(contentFuture.isSuccess());
    assertTrue(lastContentPromise.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(lastContentPromise.isSuccess());
    assertTrue(lastContentFuture.awaitUninterruptibly(WAIT_TIME_SECONDS, SECONDS));
    assertTrue(lastContentFuture.isSuccess());
    awaitRequests();
    verify(serverListener).onHeadersRead(any(ChannelHandlerContext.class), eq(3), eq(http2Headers), eq(0), anyShort(), anyBoolean(), eq(0), eq(false));
    verify(serverListener).onDataRead(any(ChannelHandlerContext.class), eq(3), any(ByteBuf.class), eq(0), eq(false));
    verify(serverListener).onHeadersRead(any(ChannelHandlerContext.class), eq(3), eq(http2TrailingHeaders), eq(0), anyShort(), anyBoolean(), eq(0), eq(true));
    assertEquals(1, receivedBuffers.size());
    assertEquals(text + text2, receivedBuffers.get(0));
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) HttpRequest(io.netty.handler.codec.http.HttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) ChannelFuture(io.netty.channel.ChannelFuture) HttpHeaders(io.netty.handler.codec.http.HttpHeaders) AsciiString(io.netty.util.AsciiString) DefaultHttpContent(io.netty.handler.codec.http.DefaultHttpContent) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) AsciiString(io.netty.util.AsciiString) ByteBuf(io.netty.buffer.ByteBuf) LastHttpContent(io.netty.handler.codec.http.LastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) DefaultLastHttpContent(io.netty.handler.codec.http.DefaultLastHttpContent) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultHttpRequest(io.netty.handler.codec.http.DefaultHttpRequest) Test(org.junit.Test)

Example 17 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class HttpToHttp2ConnectionHandlerTest method testAbsoluteFormRequestTargetHandledFromHeaders.

@Test
public void testAbsoluteFormRequestTargetHandledFromHeaders() throws Exception {
    bootstrapEnv(2, 1, 0);
    final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/pub/WWW/TheProject.html");
    final HttpHeaders httpHeaders = request.headers();
    httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 5);
    httpHeaders.set(HttpHeaderNames.HOST, "foouser@www.example.org:5555");
    httpHeaders.set(HttpConversionUtil.ExtensionHeaderNames.PATH.text(), "ignored_path");
    httpHeaders.set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), "https");
    final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")).path(new AsciiString("/pub/WWW/TheProject.html")).authority(new AsciiString("www.example.org:5555")).scheme(new AsciiString("https"));
    ChannelPromise writePromise = newPromise();
    verifyHeadersOnly(http2Headers, writePromise, clientChannel.writeAndFlush(request, writePromise));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) AsciiString(io.netty.util.AsciiString) ChannelPromise(io.netty.channel.ChannelPromise) Test(org.junit.Test)

Example 18 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class Http2FrameRoundtripTest method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    when(ctx.alloc()).thenReturn(alloc);
    when(ctx.executor()).thenReturn(executor);
    when(ctx.channel()).thenReturn(channel);
    doAnswer(new Answer<ByteBuf>() {

        @Override
        public ByteBuf answer(InvocationOnMock in) throws Throwable {
            return Unpooled.buffer();
        }
    }).when(alloc).buffer();
    doAnswer(new Answer<ByteBuf>() {

        @Override
        public ByteBuf answer(InvocationOnMock in) throws Throwable {
            return Unpooled.buffer((Integer) in.getArguments()[0]);
        }
    }).when(alloc).buffer(anyInt());
    doAnswer(new Answer<ChannelPromise>() {

        @Override
        public ChannelPromise answer(InvocationOnMock invocation) throws Throwable {
            return new DefaultChannelPromise(channel, GlobalEventExecutor.INSTANCE);
        }
    }).when(ctx).newPromise();
    writer = new DefaultHttp2FrameWriter(new DefaultHttp2HeadersEncoder(NEVER_SENSITIVE, newTestEncoder()));
    reader = new DefaultHttp2FrameReader(new DefaultHttp2HeadersDecoder(false, newTestDecoder()));
}
Also used : ChannelPromise(io.netty.channel.ChannelPromise) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) CompositeByteBuf(io.netty.buffer.CompositeByteBuf) EmptyByteBuf(io.netty.buffer.EmptyByteBuf) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultChannelPromise(io.netty.channel.DefaultChannelPromise) Before(org.junit.Before)

Example 19 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class Http2MultiplexCodecTest method failedOutboundStreamCreationThrowsAndClosesChannel.

/**
     * Test failing the promise of the first headers frame of an outbound stream. In practice this error case would most
     * likely happen due to the max concurrent streams limit being hit or the channel running out of stream identifiers.
     */
@Test(expected = Http2NoMoreStreamIdsException.class)
public void failedOutboundStreamCreationThrowsAndClosesChannel() throws Exception {
    parentChannel.pipeline().addFirst(new ChannelOutboundHandlerAdapter() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            promise.tryFailure(new Http2NoMoreStreamIdsException());
        }
    });
    LastInboundHandler inboundHandler = new LastInboundHandler();
    childChannelInitializer.handler = inboundHandler;
    Http2StreamChannelBootstrap b = new Http2StreamChannelBootstrap();
    Channel childChannel = b.parentChannel(parentChannel).handler(childChannelInitializer).connect().channel();
    assertTrue(childChannel.isActive());
    childChannel.writeAndFlush(new DefaultHttp2HeadersFrame(new DefaultHttp2Headers()));
    parentChannel.flush();
    assertFalse(childChannel.isActive());
    assertFalse(childChannel.isOpen());
    inboundHandler.checkException();
}
Also used : EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelPromise(io.netty.channel.ChannelPromise) Test(org.junit.Test)

Example 20 with ChannelPromise

use of io.netty.channel.ChannelPromise in project netty by netty.

the class HttpToHttp2ConnectionHandlerTest method testHostIPv6FormRequestTargetHandled.

@Test
public void testHostIPv6FormRequestTargetHandled() throws Exception {
    // Valid according to
    // https://tools.ietf.org/html/rfc7230#section-2.7.1 -> https://tools.ietf.org/html/rfc3986#section-3.2.2
    bootstrapEnv(2, 1, 0);
    final FullHttpRequest request = new DefaultFullHttpRequest(HTTP_1_1, GET, "/");
    final HttpHeaders httpHeaders = request.headers();
    httpHeaders.setInt(HttpConversionUtil.ExtensionHeaderNames.STREAM_ID.text(), 5);
    httpHeaders.set(HttpHeaderNames.HOST, "[::1]:80");
    httpHeaders.set(HttpConversionUtil.ExtensionHeaderNames.SCHEME.text(), "http");
    final Http2Headers http2Headers = new DefaultHttp2Headers().method(new AsciiString("GET")).path(new AsciiString("/")).scheme(new AsciiString("http")).authority(new AsciiString("[::1]:80"));
    ChannelPromise writePromise = newPromise();
    verifyHeadersOnly(http2Headers, writePromise, clientChannel.writeAndFlush(request, writePromise));
}
Also used : HttpHeaders(io.netty.handler.codec.http.HttpHeaders) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) AsciiString(io.netty.util.AsciiString) ChannelPromise(io.netty.channel.ChannelPromise) Test(org.junit.Test)

Aggregations

ChannelPromise (io.netty.channel.ChannelPromise)97 Test (org.junit.Test)56 DefaultChannelPromise (io.netty.channel.DefaultChannelPromise)34 ByteBuf (io.netty.buffer.ByteBuf)29 ChannelFuture (io.netty.channel.ChannelFuture)23 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)20 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)17 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)16 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)15 AsciiString (io.netty.util.AsciiString)14 Channel (io.netty.channel.Channel)10 ClosedChannelException (java.nio.channels.ClosedChannelException)10 InvocationOnMock (org.mockito.invocation.InvocationOnMock)10 ChannelFutureListener (io.netty.channel.ChannelFutureListener)9 Bootstrap (io.netty.bootstrap.Bootstrap)7 ConnectException (java.net.ConnectException)7 CountDownLatch (java.util.concurrent.CountDownLatch)7 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)6 AbstractChannel (io.netty.channel.AbstractChannel)6 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)6