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

Code before:
        assertFalse(ch.writeInbound(lastHttpContent));
        ReferenceCountUtil.release(lastHttpContent); // safe release

        // Nothing should have been forwarded
        assertNull(ch.readInbound());

        assertFalse(ch.finish());
    }

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

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

        assertFalse(ch.writeInbound(preflight));
        ReferenceCountUtil.release(ch.readOutbound());

        LastHttpContent first = LastHttpContent.EMPTY_LAST_CONTENT;
        LastHttpContent second = LastHttpContent.EMPTY_LAST_CONTENT;

        assertFalse(ch.writeInbound(first));
        ReferenceCountUtil.release(first);

        assertFalse(ch.writeInbound(second));
        ReferenceCountUtil.release(second);

        assertNull(ch.readInbound());
        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);
        ReferenceCountUtil.release(outbound);

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


Review comment (suggestion):
This should be removed... The handler is responsible to release.


```suggestion
```

Code after:

        // Nothing should have been forwarded
        assertNull(ch.readInbound());

        assertFalse(ch.finish());
    }

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

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

        assertFalse(ch.writeInbound(preflight));
        ReferenceCountUtil.release(ch.readOutbound());

        LastHttpContent first = LastHttpContent.EMPTY_LAST_CONTENT;
        LastHttpContent second = LastHttpContent.EMPTY_LAST_CONTENT;

        assertFalse(ch.writeInbound(first));

        assertFalse(ch.writeInbound(second));

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

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

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

        assertFalse(ch.writeInbound(preflight));
        ReferenceCountUtil.release(ch.readOutbound());

        LastHttpContent first = LastHttpContent.EMPTY_LAST_CONTENT;
        LastHttpContent second = new DefaultLastHttpContent(
                Unpooled.copiedBuffer("test message", CharsetUtil.UTF_8));

        assertFalse(ch.writeInbound(first));
        assertFalse(ch.writeInbound(second));
        assertNull(ch.readInbound());
        assertFalse(ch.finish());
    }