Repository: netty/netty
File: codec-http/src/test/java/io/netty/handler/codec/http/cors/CorsHandlerTest.java (Java)

Code before:
        assertFalse(ch.writeInbound(req));

        LastHttpContent last = LastHttpContent.EMPTY_LAST_CONTENT;
        assertFalse(ch.writeInbound(last));

        CaptureHandler cap = ch.pipeline().get(CaptureHandler.class);
        assertEquals(2, cap.messages.size());

        assertFalse(ch.finish());
    }

    @Test
    public void testContentForwardedWhenNoDiscard() {
        CorsConfig config = forOrigin("http://allowed").build();
        EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config), new CaptureHandler());

        // Normal request
        FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, GET, "/test");
        req.headers().set(ORIGIN, "http://allowed");

        ch.writeInbound(req);

        HttpContent content = new DefaultHttpContent(Unpooled.copiedBuffer("test message", CharsetUtil.UTF_8));
        assertFalse(ch.writeInbound(content));

        CaptureHandler cap = ch.pipeline().get(CaptureHandler.class);
        assertEquals(2, cap.messages.size());

        assertFalse(ch.finish());
    }

    private static HttpResponse simpleRequest(final CorsConfig config, final String origin) {
        return simpleRequest(config, origin, null);
    }

    private static HttpResponse simpleRequest(final CorsConfig config,
                                              final String origin,
                                              final String requestHeaders) {
        return simpleRequest(config, origin, requestHeaders, GET);
    }

    private static HttpResponse simpleRequest(final CorsConfig config,
                                              final String origin,
                                              final String requestHeaders,
                                              final HttpMethod method) {
        final EmbeddedChannel channel = new EmbeddedChannel(new CorsHandler(config), new EchoHandler());
        final FullHttpRequest httpRequest = createHttpRequest(method);
        if (origin != null) {
            httpRequest.headers().set(ORIGIN, origin);
        }
        if (requestHeaders != null) {

Review comment (suggestion):
remove the whole `CaptureHandler` stuff and use `ch.readInbound()`. This way you can also verify its the correct type and can also release it

Code after:
        assertFalse(ch.finish());
    }

    @Test
    public void preflightNonEmptyLastForwarded() {
        CorsConfig config = forOrigin("http://allowed").build();
        EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config));

        FullHttpRequest preflight = new DefaultFullHttpRequest(HTTP_1_1, OPTIONS, "/x");
        preflight.headers().set(ORIGIN, "http://allowed");
        preflight.headers().set(ACCESS_CONTROL_REQUEST_METHOD, "GET");

        assertFalse(ch.writeInbound(preflight));
        Object outbound = ch.releaseOutbound();
        assertNotNull(outbound);

        LastHttpContent nonEmpty = new DefaultLastHttpContent(Unpooled.copiedBuffer("x", CharsetUtil.UTF_8));
        assertFalse(ch.writeInbound(nonEmpty));

        Object inbound = ch.readInbound();
        assertNull(inbound);

        assertFalse(ch.finish());
    }

    @Test
    public void testNormalRequestForwarded() {
        CorsConfig config = forOrigin("http://allowed").build();
        EmbeddedChannel ch = new EmbeddedChannel(new CorsHandler(config));

        FullHttpRequest req = new DefaultFullHttpRequest(HTTP_1_1, GET, "/test");
        req.headers().set(ORIGIN, "http://allowed");

        assertTrue(ch.writeInbound(req));

        LastHttpContent last = LastHttpContent.EMPTY_LAST_CONTENT;
        assertTrue(ch.writeInbound(last));

        Object firstInbound = ch.readInbound();
        Object secondInbound = ch.readInbound();

        assertNotNull(firstInbound);
        assertNotNull(secondInbound);

        assertNull(ch.readInbound());
        assertFalse(ch.finish());
    }

    @Test
    public void preflightEmptyLastDiscardedThenNewRequestForwarded() {
        CorsConfig config = forOrigin("http://allowed").build();