Skip to content

Commit

Permalink
Add testcases to prove HttpResponseEncoder correctly handles empty co…
Browse files Browse the repository at this point in the history
…ntent

Motivation:

Issue netty#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.
  • Loading branch information
normanmaurer committed Aug 7, 2017
1 parent bd02950 commit 918fde8
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}

0 comments on commit 918fde8

Please sign in to comment.