From 918fde82f8efa64a6f9d3abb8e108c773f2ccf46 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Sun, 6 Aug 2017 15:27:52 +0200 Subject: [PATCH] Add testcases to prove HttpResponseEncoder correctly handles empty content Motivation: Issue #6695 states that there is an issue when writing empty content via HttpResponseEncoder. Modifications: Add two test-cases. Result: Verified that all works as expected. --- .../codec/http/HttpResponseEncoderTest.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseEncoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseEncoderTest.java index 199ff7779b1f..03117febbdff 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseEncoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpResponseEncoderTest.java @@ -150,4 +150,49 @@ public void testEmptyBufferBypass() throws Exception { assertFalse(channel.finish()); } + + @Test + public void testEmptyContentChunked() throws Exception { + testEmptyContent(true); + } + + @Test + public void testEmptyContentNotChunked() throws Exception { + testEmptyContent(false); + } + + private static void testEmptyContent(boolean chunked) throws Exception { + String content = "netty rocks"; + ByteBuf contentBuffer = Unpooled.copiedBuffer(content, CharsetUtil.US_ASCII); + int length = contentBuffer.readableBytes(); + + EmbeddedChannel channel = new EmbeddedChannel(new HttpResponseEncoder()); + HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); + if (!chunked) { + HttpUtil.setContentLength(response, length); + } + assertTrue(channel.writeOutbound(response)); + assertTrue(channel.writeOutbound(new DefaultHttpContent(Unpooled.EMPTY_BUFFER))); + assertTrue(channel.writeOutbound(new DefaultLastHttpContent(contentBuffer))); + + ByteBuf buffer = channel.readOutbound(); + if (!chunked) { + assertEquals("HTTP/1.1 200 OK\r\ncontent-length: " + length + "\r\n\r\n", + buffer.toString(CharsetUtil.US_ASCII)); + } else { + assertEquals("HTTP/1.1 200 OK\r\n\r\n", buffer.toString(CharsetUtil.US_ASCII)); + } + buffer.release(); + + // Test writing an empty buffer works when the encoder is not at ST_INIT. + buffer = channel.readOutbound(); + assertEquals(0, buffer.readableBytes()); + buffer.release(); + + buffer = channel.readOutbound(); + assertEquals(length, buffer.readableBytes()); + buffer.release(); + + assertFalse(channel.finish()); + } }