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

Code before:

        assertFalse(channel.writeInbound(Unpooled.wrappedBuffer(headers.getBytes(CharsetUtil.US_ASCII))));

        int offset = 0;
        while (offset < SAMPLE_BZ_BYTES.length) {
            int len = Math.min(1500, SAMPLE_BZ_BYTES.length - offset);
            boolean available = channel.writeInbound(Unpooled.wrappedBuffer(SAMPLE_BZ_BYTES, offset, len));
            offset += 1500;
            if (offset < SAMPLE_BZ_BYTES.length) {
                assertFalse(available);
            } else {
                assertTrue(available);
            }
        }

        Object o = channel.readInbound();
        assertThat(o, is(instanceOf(FullHttpResponse.class)));
        FullHttpResponse resp = (FullHttpResponse) o;
        assertEquals(SAMPLE_STRING, resp.content().toString(CharsetUtil.UTF_8),
          "Response body should match uncompressed string");
        resp.release();

        assertHasInboundMessages(channel, false);
        assertHasOutboundMessages(channel, false);
        assertFalse(channel.finish()); // assert that no messages are left in channel
    }

    @Test
    public void testExpectContinueResponse1() {
        // request with header "Expect: 100-continue" must be replied with one "100 Continue" response
        // case 1: no ContentDecoder in chain at all (baseline test)
        HttpRequestDecoder decoder = new HttpRequestDecoder();
        HttpObjectAggregator aggregator = new HttpObjectAggregator(1024);
        EmbeddedChannel channel = new EmbeddedChannel(decoder, aggregator);
        String req = "POST / HTTP/1.1\r\n" +
                     "Content-Length: " + GZ_HELLO_WORLD.length + "\r\n" +
                     "Expect: 100-continue\r\n" +
                     "\r\n";
        // note: the following writeInbound() returns false as there is no message is inbound buffer
        // until HttpObjectAggregator caches composes a complete message.
        // however, http response "100 continue" must be sent as soon as headers are received
        assertFalse(channel.writeInbound(Unpooled.wrappedBuffer(req.getBytes())));

        Object o = channel.readOutbound();
        assertThat(o, is(instanceOf(FullHttpResponse.class)));
        FullHttpResponse r = (FullHttpResponse) o;
        assertEquals(100, r.status().code());
        assertTrue(channel.writeInbound(Unpooled.copiedBuffer(GZ_HELLO_WORLD)));
        r.release();

        assertHasInboundMessages(channel, true);

Review comment (bug):
In a try/finally block to not leak memory in the event of a test failure?

Code after:

        String headers = "HTTP/1.1 200 OK\r\n" +
          "Content-Length: " + SAMPLE_BZ_BYTES.length + "\r\n" +
          "Content-Encoding: br\r\n" +
          "\r\n";

        assertFalse(channel.writeInbound(Unpooled.wrappedBuffer(headers.getBytes(CharsetUtil.US_ASCII))));

        int offset = 0;
        while (offset < SAMPLE_BZ_BYTES.length) {
            int len = Math.min(1500, SAMPLE_BZ_BYTES.length - offset);
            boolean available = channel.writeInbound(Unpooled.wrappedBuffer(SAMPLE_BZ_BYTES, offset, len));
            offset += 1500;
            if (offset < SAMPLE_BZ_BYTES.length) {
                assertFalse(available);
            } else {
                assertTrue(available);
            }
        }

        Object o = channel.readInbound();
        assertThat(o, is(instanceOf(FullHttpResponse.class)));
        FullHttpResponse resp = (FullHttpResponse) o;
        assertEquals(SAMPLE_STRING, resp.content().toString(CharsetUtil.UTF_8),
                "Response body should match uncompressed string");
        resp.release();

        assertHasInboundMessages(channel, false);
        assertHasOutboundMessages(channel, false);
        assertFalse(channel.finish()); // assert that no messages are left in channel
    }

    @Test
    public void testResponseZstdDecompression() throws Throwable {
        HttpResponseDecoder decoder = new HttpResponseDecoder();
        HttpContentDecoder decompressor = new HttpContentDecompressor();
        HttpObjectAggregator aggregator = new HttpObjectAggregator(Integer.MAX_VALUE);
        EmbeddedChannel channel = new EmbeddedChannel(decoder, decompressor, aggregator);

        String headers = "HTTP/1.1 200 OK\r\n" +
                "Content-Length: " + SAMPLE_ZSTD_BYTES.length + "\r\n" +
                "Content-Encoding: zstd\r\n" +
                "\r\n";
        ByteBuf buf = Unpooled.wrappedBuffer(headers.getBytes(CharsetUtil.US_ASCII), SAMPLE_ZSTD_BYTES);
        assertTrue(channel.writeInbound(buf));

        Object o = channel.readInbound();
        assertThat(o, is(instanceOf(FullHttpResponse.class)));
        FullHttpResponse resp = (FullHttpResponse) o;
        assertNull(resp.headers().get(HttpHeaderNames.CONTENT_ENCODING), "Content-Encoding header should be removed");
        assertEquals(SAMPLE_STRING, resp.content().toString(CharsetUtil.UTF_8),