Search in sources :

Example 76 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project vert.x by eclipse.

the class Http2ClientTest method testConnectionDecodeError.

@Test
public void testConnectionDecodeError() throws Exception {
    waitFor(3);
    ServerBootstrap bootstrap = createH2Server((dec, enc) -> new Http2EventAdapter() {

        @Override
        public void onHeadersRead(ChannelHandlerContext ctx, int streamId, Http2Headers headers, int streamDependency, short weight, boolean exclusive, int padding, boolean endStream) throws Http2Exception {
            enc.writeHeaders(ctx, streamId, new DefaultHttp2Headers().status("200"), 0, false, ctx.newPromise());
            enc.frameWriter().writeRstStream(ctx, 10, 0, ctx.newPromise());
            ctx.flush();
        }
    });
    ChannelFuture s = bootstrap.bind(DEFAULT_HTTPS_HOST, DEFAULT_HTTPS_PORT).sync();
    try {
        Context ctx = vertx.getOrCreateContext();
        client.close();
        ctx.runOnContext(v -> {
            client = vertx.createHttpClient(createBaseClientOptions());
            client.connectionHandler(conn -> {
                conn.exceptionHandler(err -> {
                    assertSame(ctx, Vertx.currentContext());
                    if (err instanceof Http2Exception) {
                        complete();
                    }
                });
            });
            client.request(new RequestOptions().setMethod(HttpMethod.PUT).setHost(DEFAULT_HTTPS_HOST).setPort(DEFAULT_HTTPS_PORT).setURI(DEFAULT_TEST_URI)).onComplete(onSuccess(req -> {
                req.response(onSuccess(resp -> {
                    resp.exceptionHandler(err -> {
                        assertOnIOContext(ctx);
                        if (err instanceof Http2Exception) {
                            complete();
                        }
                    });
                })).exceptionHandler(err -> {
                    assertOnIOContext(ctx);
                    if (err instanceof Http2Exception) {
                        complete();
                    }
                }).sendHead();
            }));
        });
        await();
    } finally {
        s.channel().close().sync();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Context(io.vertx.core.Context) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Arrays(java.util.Arrays) io.netty.handler.codec.http2(io.netty.handler.codec.http2) BiFunction(java.util.function.BiFunction) MultiMap(io.vertx.core.MultiMap) AsciiString(io.netty.util.AsciiString) Context(io.vertx.core.Context) HttpClientConnection(io.vertx.core.http.impl.HttpClientConnection) TestUtils(io.vertx.test.core.TestUtils) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ApplicationProtocolNegotiationHandler(io.netty.handler.ssl.ApplicationProtocolNegotiationHandler) ChannelInitializer(io.netty.channel.ChannelInitializer) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Set(java.util.Set) ChannelPipeline(io.netty.channel.ChannelPipeline) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) SslHandler(io.netty.handler.ssl.SslHandler) Cert(io.vertx.test.tls.Cert) GZIPOutputStream(java.util.zip.GZIPOutputStream) NetSocket(io.vertx.core.net.NetSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ByteBuf(io.netty.buffer.ByteBuf) Assume(org.junit.Assume) ConnectException(java.net.ConnectException) AsyncResult(io.vertx.core.AsyncResult) SocketAddress(io.vertx.core.net.SocketAddress) VertxInternal(io.vertx.core.impl.VertxInternal) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) ApplicationProtocolNames(io.netty.handler.ssl.ApplicationProtocolNames) Test(org.junit.Test) SSLHelper(io.vertx.core.net.impl.SSLHelper) ChannelFuture(io.netty.channel.ChannelFuture) Channel(io.netty.channel.Channel) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) AtomicLong(java.util.concurrent.atomic.AtomicLong) Ignore(org.junit.Ignore) AsyncTestBase(io.vertx.test.core.AsyncTestBase) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) HttpHeaderNames(io.netty.handler.codec.http.HttpHeaderNames) Handler(io.vertx.core.Handler) Collections(java.util.Collections) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) Test(org.junit.Test)

Example 77 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.

the class NettyClientHandlerTest method bdpPingAllowedAfterSendingData.

@Test
public void bdpPingAllowedAfterSendingData() throws Exception {
    // gRPC servers limit PINGs based on what they _send_. Some servers limit PINGs based on what is
    // _received_.
    flowControlWindow = 64 * 1024;
    manualSetUp();
    createStream();
    handler().setAutoTuneFlowControl(true);
    ByteBuf content = Unpooled.buffer(64 * 1024 + 1024);
    content.writerIndex(content.capacity());
    ChannelFuture future = enqueue(new SendGrpcFrameCommand(streamTransportState, content, false));
    // flow control limits send
    assertFalse(future.isDone());
    Http2Headers headers = new DefaultHttp2Headers().status(STATUS_OK).set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC);
    channelRead(headersFrame(3, headers));
    channelRead(dataFrame(3, false, content()));
    verifyWrite().writePing(eq(ctx()), eq(false), eq(1234L), any(ChannelPromise.class));
    channelRead(pingFrame(true, 1234));
    channelRead(dataFrame(3, false, content()));
    verifyWrite(times(2)).writePing(eq(ctx()), eq(false), eq(1234L), any(ChannelPromise.class));
    channelRead(pingFrame(true, 1234));
    channelRead(dataFrame(3, false, content()));
    // No ping was sent
    verifyWrite(times(2)).writePing(eq(ctx()), eq(false), eq(1234L), any(ChannelPromise.class));
    channelRead(windowUpdate(0, 2024));
    channelRead(windowUpdate(3, 2024));
    assertTrue(future.isDone());
    assertTrue(future.isSuccess());
    // But now one is sent
    channelRead(dataFrame(3, false, content()));
    verifyWrite(times(3)).writePing(eq(ctx()), eq(false), eq(1234L), any(ChannelPromise.class));
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) ChannelPromise(io.netty.channel.ChannelPromise) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 78 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.

the class NettyClientHandlerTest method setUp.

/**
 * Set up for test.
 */
@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    doAnswer(new Answer<Void>() {

        @Override
        public Void answer(InvocationOnMock invocation) throws Throwable {
            StreamListener.MessageProducer producer = (StreamListener.MessageProducer) invocation.getArguments()[0];
            InputStream message;
            while ((message = producer.next()) != null) {
                streamListenerMessageQueue.add(message);
            }
            return null;
        }
    }).when(streamListener).messagesAvailable(ArgumentMatchers.<StreamListener.MessageProducer>any());
    lifecycleManager = new ClientTransportLifecycleManager(listener);
    // it'll be null which will be testing if we behave correctly when it's not present.
    if (setKeepaliveManagerFor.contains(testNameRule.getMethodName())) {
        mockKeepAliveManager = mock(KeepAliveManager.class);
    }
    initChannel(new GrpcHttp2ClientHeadersDecoder(GrpcUtil.DEFAULT_MAX_HEADER_LIST_SIZE));
    streamTransportState = new TransportStateImpl(handler(), channel().eventLoop(), DEFAULT_MAX_MESSAGE_SIZE, transportTracer);
    streamTransportState.setListener(streamListener);
    grpcHeaders = new DefaultHttp2Headers().scheme(HTTPS).authority(as("www.fake.com")).path(as("/fakemethod")).method(HTTP_METHOD).add(as("auth"), as("sometoken")).add(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC).add(TE_HEADER, TE_TRAILERS);
    // Simulate receipt of initial remote settings.
    ByteBuf serializedSettings = serializeSettings(new Http2Settings());
    channelRead(serializedSettings);
}
Also used : InputStream(java.io.InputStream) ByteBuf(io.netty.buffer.ByteBuf) InvocationOnMock(org.mockito.invocation.InvocationOnMock) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) GrpcHttp2ClientHeadersDecoder(io.grpc.netty.GrpcHttp2HeadersUtils.GrpcHttp2ClientHeadersDecoder) Http2Settings(io.netty.handler.codec.http2.Http2Settings) KeepAliveManager(io.grpc.internal.KeepAliveManager) StreamListener(io.grpc.internal.StreamListener) ClientStreamListener(io.grpc.internal.ClientStreamListener) Before(org.junit.Before)

Example 79 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.

the class NettyClientHandlerTest method makeStream.

@Override
protected void makeStream() throws Exception {
    createStream();
    // The tests in NettyServerHandlerTest expect the header to already be read, since they work on
    // both client- and server-side.
    Http2Headers headers = new DefaultHttp2Headers().status(STATUS_OK).set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC);
    ByteBuf headersFrame = headersFrame(3, headers);
    channelRead(headersFrame);
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) ByteBuf(io.netty.buffer.ByteBuf)

Example 80 with DefaultHttp2Headers

use of io.netty.handler.codec.http2.DefaultHttp2Headers in project grpc-java by grpc.

the class NettyServerHandlerTest method createStream.

private void createStream() throws Exception {
    Http2Headers headers = new DefaultHttp2Headers().method(HTTP_METHOD).set(CONTENT_TYPE_HEADER, CONTENT_TYPE_GRPC).set(TE_HEADER, TE_TRAILERS).path(new AsciiString("/foo/bar"));
    ByteBuf headersFrame = headersFrame(STREAM_ID, headers);
    channelRead(headersFrame);
    ArgumentCaptor<NettyServerStream> streamCaptor = ArgumentCaptor.forClass(NettyServerStream.class);
    ArgumentCaptor<String> methodCaptor = ArgumentCaptor.forClass(String.class);
    verify(transportListener).streamCreated(streamCaptor.capture(), methodCaptor.capture(), any(Metadata.class));
    stream = streamCaptor.getValue();
}
Also used : Http2Headers(io.netty.handler.codec.http2.Http2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) DefaultHttp2Headers(io.netty.handler.codec.http2.DefaultHttp2Headers) AsciiString(io.netty.util.AsciiString) Metadata(io.grpc.Metadata) AsciiString(io.netty.util.AsciiString) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ByteBuf(io.netty.buffer.ByteBuf)

Aggregations

DefaultHttp2Headers (io.netty.handler.codec.http2.DefaultHttp2Headers)72 Http2Headers (io.netty.handler.codec.http2.Http2Headers)58 AsciiString (io.netty.util.AsciiString)56 ByteBuf (io.netty.buffer.ByteBuf)54 Test (org.junit.Test)49 Test (org.junit.jupiter.api.Test)32 ChannelFuture (io.netty.channel.ChannelFuture)27 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)19 ChannelPromise (io.netty.channel.ChannelPromise)17 Channel (io.netty.channel.Channel)16 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)16 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)15 List (java.util.List)15 ChannelInitializer (io.netty.channel.ChannelInitializer)14 ChannelPipeline (io.netty.channel.ChannelPipeline)14 HttpHeaderNames (io.netty.handler.codec.http.HttpHeaderNames)14 HttpServerCodec (io.netty.handler.codec.http.HttpServerCodec)14 Http2Runnable (io.netty.handler.codec.http2.Http2TestUtil.Http2Runnable)14 ApplicationProtocolNames (io.netty.handler.ssl.ApplicationProtocolNames)14 ApplicationProtocolNegotiationHandler (io.netty.handler.ssl.ApplicationProtocolNegotiationHandler)14