Home / Class/ HttpServerUpgradeHandlerTest Class — netty Architecture

HttpServerUpgradeHandlerTest Class — netty Architecture

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

Entity Profile

Dependency Diagram

graph TD
  b1c607f7_af7c_9e8d_7626_d168a967f7a0["HttpServerUpgradeHandlerTest"]
  b53eb5bf_0aa1_db1b_34b5_27f222bc806d["HttpServerUpgradeHandlerTest.java"]
  b1c607f7_af7c_9e8d_7626_d168a967f7a0 -->|defined in| b53eb5bf_0aa1_db1b_34b5_27f222bc806d
  26606c94_ea67_e56d_6d9c_4abd98565bb9["upgradesPipelineInSameMethodInvocation()"]
  b1c607f7_af7c_9e8d_7626_d168a967f7a0 -->|method| 26606c94_ea67_e56d_6d9c_4abd98565bb9
  daa470a4_a88a_73e1_62fa_34817a90580b["skippedUpgrade()"]
  b1c607f7_af7c_9e8d_7626_d168a967f7a0 -->|method| daa470a4_a88a_73e1_62fa_34817a90580b
  e2819c86_e2aa_8b96_1eb8_a383fa267d8b["upgradeFail()"]
  b1c607f7_af7c_9e8d_7626_d168a967f7a0 -->|method| e2819c86_e2aa_8b96_1eb8_a383fa267d8b
  5bdf641b_33aa_9377_7274_b133552ee57a["upgradeExpect()"]
  b1c607f7_af7c_9e8d_7626_d168a967f7a0 -->|method| 5bdf641b_33aa_9377_7274_b133552ee57a
  0824e92b_1ed5_dc76_661b_7e4ea15d000e["upgradePrematureClose()"]
  b1c607f7_af7c_9e8d_7626_d168a967f7a0 -->|method| 0824e92b_1ed5_dc76_661b_7e4ea15d000e

Relationship Graph

Source Code

codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java lines 45–286

public class HttpServerUpgradeHandlerTest {

    private static class TestUpgradeCodec implements UpgradeCodec {
        @Override
        public Collection<CharSequence> requiredUpgradeHeaders() {
            return Collections.<CharSequence>emptyList();
        }

        @Override
        public boolean prepareUpgradeResponse(ChannelHandlerContext ctx, FullHttpRequest upgradeRequest,
                                              HttpHeaders upgradeHeaders) {
            return true;
        }

        @Override
        public void upgradeTo(ChannelHandlerContext ctx, FullHttpRequest upgradeRequest) {
            // Ensure that the HttpServerUpgradeHandler is still installed when this is called
            assertEquals(ctx.pipeline().context(HttpServerUpgradeHandler.class), ctx);
            assertNotNull(ctx.pipeline().get(HttpServerUpgradeHandler.class));

            // Add a marker handler to signal that the upgrade has happened
            ctx.pipeline().addAfter(ctx.name(), "marker", new ChannelInboundHandlerAdapter());
          }
    }

    @Test
    public void upgradesPipelineInSameMethodInvocation() {
        final HttpServerCodec httpServerCodec = new HttpServerCodec();
        final UpgradeCodecFactory factory = new UpgradeCodecFactory() {
            @Override
            public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
                return new TestUpgradeCodec();
            }
        };

        ChannelHandler testInStackFrame = new ChannelDuplexHandler() {
            // marker boolean to signal that we're in the `channelRead` method
            private boolean inReadCall;
            private boolean writeUpgradeMessage;
            private boolean writeFlushed;

            @Override
            public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
                assertFalse(inReadCall);
                assertFalse(writeUpgradeMessage);

                inReadCall = true;
                try {
                    super.channelRead(ctx, msg);
                    // All in the same call stack, the upgrade codec should receive the message,
                    // written the upgrade response, and upgraded the pipeline.
                    assertTrue(writeUpgradeMessage);
                    assertFalse(writeFlushed);
                    assertNull(ctx.pipeline().get(HttpServerCodec.class));
                    assertNotNull(ctx.pipeline().get("marker"));
                } finally {
                    inReadCall = false;
                }
            }

            @Override
            public void write(final ChannelHandlerContext ctx, final Object msg, final ChannelPromise promise) {
                // We ensure that we're in the read call and defer the write so we can
                // make sure the pipeline was reformed irrespective of the flush completing.
                assertTrue(inReadCall);
                writeUpgradeMessage = true;
                ctx.channel().eventLoop().execute(new Runnable() {
                    @Override
                    public void run() {
                        ctx.write(msg, promise);
                    }
                });
                promise.addListener(future -> writeFlushed = true);
            }
        };

        HttpServerUpgradeHandler upgradeHandler = new HttpServerUpgradeHandler(httpServerCodec, factory);

        EmbeddedChannel channel = new EmbeddedChannel(testInStackFrame, httpServerCodec, upgradeHandler);

        String upgradeString = "GET / HTTP/1.1\r\n" +

Frequently Asked Questions

What is the HttpServerUpgradeHandlerTest class?
HttpServerUpgradeHandlerTest is a class in the netty codebase, defined in codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java.
Where is HttpServerUpgradeHandlerTest defined?
HttpServerUpgradeHandlerTest is defined in codec-http/src/test/java/io/netty/handler/codec/http/HttpServerUpgradeHandlerTest.java at line 45.

Analyze Your Own Codebase

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

Try Supermodel Free