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

Code before:

    @Test
    public void testChunkSizeOverflow2() {
        String requestStr = "PUT /some/path HTTP/1.1\r\n" +
                "Transfer-Encoding: chunked\r\n\r\n" +
                "bbbbbbbe;\n\r\n";
        EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
        assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)));
        HttpRequest request = channel.readInbound();
        assertTrue(request.decoderResult().isSuccess());
        HttpContent c = channel.readInbound();
        c.release();
        assertTrue(c.decoderResult().isFailure());
        assertInstanceOf(NumberFormatException.class, c.decoderResult().cause());
        assertFalse(channel.finish());
    }

    @ParameterizedTest
    // See https://www.unicode.org/charts/nameslist/n_0000.html
    @ValueSource(strings = { "\r", "\u000b", "\u000c" })
    public void testHeaderValueWithInvalidSuffix(String suffix) {
        testInvalidHeaders0("GET / HTTP/1.1\r\nHost: whatever\r\nTest-Key: test-value" + suffix + "\r\n\r\n");
    }

    private static void testInvalidHeaders0(String requestStr) {
        testInvalidHeaders0(Unpooled.copiedBuffer(requestStr, CharsetUtil.UTF_8));
    }

    private static void testInvalidHeaders0(ByteBuf requestBuffer) {
        EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
        assertTrue(channel.writeInbound(requestBuffer));
        HttpRequest request = channel.readInbound();
        assertThat(request.decoderResult().cause(), instanceOf(IllegalArgumentException.class));
        assertTrue(request.decoderResult().isFailure());
        assertFalse(channel.finish());
    }
}


Review comment (suggestion):
```suggestion
        testInvalidHeaders0(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII));
```

Code after:

    @Test
    public void testChunkSizeOverflow2() {
        String requestStr = "PUT /some/path HTTP/1.1\r\n" +
                "Transfer-Encoding: chunked\r\n\r\n" +
                "bbbbbbbe;\n\r\n";
        EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
        assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)));
        HttpRequest request = channel.readInbound();
        assertTrue(request.decoderResult().isSuccess());
        HttpContent c = channel.readInbound();
        c.release();
        assertTrue(c.decoderResult().isFailure());
        assertInstanceOf(NumberFormatException.class, c.decoderResult().cause());
        assertFalse(channel.finish());
    }

    @ParameterizedTest
    // See https://www.unicode.org/charts/nameslist/n_0000.html
    @ValueSource(strings = { "\r", "\u000b", "\u000c" })
    public void testHeaderValueWithInvalidSuffix(String suffix) {
        testInvalidHeaders0("GET / HTTP/1.1\r\nHost: whatever\r\nTest-Key: test-value" + suffix + "\r\n\r\n");
    }

    private static void testInvalidHeaders0(String requestStr) {
        testInvalidHeaders0(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII));
    }

    private static void testInvalidHeaders0(ByteBuf requestBuffer) {
        EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
        assertTrue(channel.writeInbound(requestBuffer));
        HttpRequest request = channel.readInbound();
        assertThat(request.decoderResult().cause(), instanceOf(IllegalArgumentException.class));
        assertTrue(request.decoderResult().isFailure());
        assertFalse(channel.finish());
    }
}