Home / Class/ CodecHttp2Test Class — netty Architecture

CodecHttp2Test Class — netty Architecture

Architecture documentation for the CodecHttp2Test class in CodecHttp2Test.java from the netty codebase.

Entity Profile

Dependency Diagram

graph TD
  9cde399d_fb6a_abb8_d6b0_5ac4951f70a0["CodecHttp2Test"]
  e8b87cf2_9513_97a0_b37e_46d2361e5982["CodecHttp2Test.java"]
  9cde399d_fb6a_abb8_d6b0_5ac4951f70a0 -->|defined in| e8b87cf2_9513_97a0_b37e_46d2361e5982
  3bfc87d3_a3cc_9da7_8bf6_c48e7c666fe3["smokeTest()"]
  9cde399d_fb6a_abb8_d6b0_5ac4951f70a0 -->|method| 3bfc87d3_a3cc_9da7_8bf6_c48e7c666fe3

Relationship Graph

Source Code

testsuite-jpms/src/test/java/io/netty/testsuite_jpms/test/CodecHttp2Test.java lines 63–165

public class CodecHttp2Test {

    @Test
    public void smokeTest() throws Exception {

        ServerBootstrap serverBootstrap = new ServerBootstrap();
        serverBootstrap.channel(NioServerSocketChannel.class);
        MultiThreadIoEventLoopGroup eventLoopGroup = new MultiThreadIoEventLoopGroup(NioIoHandler.newFactory());
        serverBootstrap.group(eventLoopGroup);
        serverBootstrap.childHandler(new ChannelInitializer<>() {
            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(Http2FrameCodecBuilder.forServer().build(), new ChannelDuplexHandler() {
                    @Override
                    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                        if (msg instanceof Http2HeadersFrame) {
                            Http2HeadersFrame streamFrame = (Http2HeadersFrame) msg;
                            if (streamFrame.isEndStream()) {
                                ByteBuf body = ctx.alloc().buffer();
                                body.writeCharSequence("Hello World", StandardCharsets.UTF_8);
                                Http2Headers headers = new DefaultHttp2Headers().status(OK.codeAsText());
                                Http2FrameStream stream = streamFrame.stream();
                                ctx.write(new DefaultHttp2HeadersFrame(headers).stream(stream));
                                ctx.write(new DefaultHttp2DataFrame(body, true).stream(stream));
                            }
                        } else {
                            super.channelRead(ctx, msg);
                        }
                    }
                });
            }
        });
        ChannelFuture server = serverBootstrap.bind("localhost", 8080).sync();

        try {
            Bootstrap clientBootstrap = new Bootstrap()
                    .group(eventLoopGroup)
                    .channel(NioSocketChannel.class)
                    .handler(new ChannelInitializer<>() {
                @Override
                protected void initChannel(Channel ch) {
                    Http2FrameCodec http2FrameCodec = Http2FrameCodecBuilder.forClient().build();
                    ch.pipeline().addLast(http2FrameCodec);
                    ch.pipeline().addLast(new Http2MultiplexHandler(new SimpleChannelInboundHandler<>() {
                        @Override
                        protected void channelRead0(ChannelHandlerContext ctx, Object msg) {
                        }
                    }));
                }
            });

            CompletableFuture<String> responseFut = new CompletableFuture<>();

            Channel client = clientBootstrap.handler(new ChannelInitializer<SocketChannel>() {
                Http2Connection connection;
                Http2ConnectionHandler connectionHandler;
                @Override
                protected void initChannel(SocketChannel ch) {
                    connection = new DefaultHttp2Connection(false);
                    Http2EventAdapter http2EventHandler = new Http2EventAdapter() {
                        @Override
                        public void onSettingsRead(ChannelHandlerContext ctx, Http2Settings settings)
                                throws Http2Exception {
                            Http2Connection.Endpoint<Http2LocalFlowController> endpoint = connection.local();
                            Http2Stream stream = endpoint.createStream(1, false);
                            Http2Headers headers = new DefaultHttp2Headers()
                                    .method("GET")
                                    .path("/")
                                    .scheme("http");
                            ChannelPromise promise = ctx.newPromise();
                            Http2ConnectionEncoder encoder = connectionHandler.encoder();
                            encoder.writeHeaders(ctx, 1, headers, 0, true, promise);
                        }
                        @Override
                        public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data,
                                              int padding, boolean endOfStream) throws Http2Exception {
                            if (endOfStream) {
                                responseFut.complete(data.toString(StandardCharsets.UTF_8));
                            }
                            return super.onDataRead(ctx, streamId, data, padding, endOfStream);
                        }

Frequently Asked Questions

What is the CodecHttp2Test class?
CodecHttp2Test is a class in the netty codebase, defined in testsuite-jpms/src/test/java/io/netty/testsuite_jpms/test/CodecHttp2Test.java.
Where is CodecHttp2Test defined?
CodecHttp2Test is defined in testsuite-jpms/src/test/java/io/netty/testsuite_jpms/test/CodecHttp2Test.java at line 63.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free