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

Code before:
        HttpRequest request = channel.readInbound();
        assertTrue(request.decoderResult().isSuccess());
        HttpHeaders headers = request.headers();
        assertEquals("example.com", headers.get("Host"));
        assertEquals("x0", headers.get("X-0-Header"));
        assertEquals("x1", headers.get("X-1-Header"));
        assertEquals("x2", headers.get("X-2-Header"));
        assertEquals("x3", headers.get("X-3-Header"));
        LastHttpContent last = channel.readInbound();
        assertEquals(LastHttpContent.EMPTY_LAST_CONTENT, last);
        last.release();
        assertFalse(channel.finish());
    }

    @Test
    public void testChunkSizeOverflow() {
        String requestStr = "PUT /some/path HTTP/1.1\r\n" +
                "Transfer-Encoding: chunked\r\n\r\n" +
                "8ccccccc\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();
        assertFalse(channel.finish());
    }

    @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();
        assertFalse(channel.finish());
    }

    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));

Review comment (suggestion):
1. In both tests, consider asserting that `c.decoderResult().cause()` is `NumberFormatException`.
2. Consider adding similar tests for `HttpResponseDecoderTest`.

Code after:
        assertTrue(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)));
        HttpRequest request = channel.readInbound();
        assertTrue(request.decoderResult().isSuccess());
        HttpHeaders headers = request.headers();
        assertEquals("example.com", headers.get("Host"));
        assertEquals("x0", headers.get("X-0-Header"));
        assertEquals("x1", headers.get("X-1-Header"));
        assertEquals("x2", headers.get("X-2-Header"));
        assertEquals("x3", headers.get("X-3-Header"));
        LastHttpContent last = channel.readInbound();
        assertEquals(LastHttpContent.EMPTY_LAST_CONTENT, last);
        last.release();
        assertFalse(channel.finish());
    }

    @Test
    public void testChunkSizeOverflow() {
        String requestStr = "PUT /some/path HTTP/1.1\r\n" +
                "Transfer-Encoding: chunked\r\n\r\n" +
                "8ccccccc\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());
    }

    @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());
    }

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