Add testcase for writing empty last content with headers.

Motivation:

https://github.com/netty/netty/issues/7418 reported an issue with writing a LastHttpContent with trailers set.

Modifications:

Add unit test to ensure this issue is fixed in latest netty release.

Result:

Ensure code is correct.
This commit is contained in:
Norman Maurer 2017-11-19 09:19:07 +01:00
parent a3e41ba6eb
commit e5e4c18c1b
2 changed files with 71 additions and 6 deletions

View File

@ -156,16 +156,26 @@ public class HttpRequestEncoderTest {
}
@Test
public void testEmptyContentChunked() throws Exception {
testEmptyContent(true);
public void testEmptyContentsChunked() throws Exception {
testEmptyContents(true, false);
}
@Test
public void testEmptyContentNotChunked() throws Exception {
testEmptyContent(false);
public void testEmptyContentsChunkedWithTrailers() throws Exception {
testEmptyContents(true, true);
}
private void testEmptyContent(boolean chunked) throws Exception {
@Test
public void testEmptyContentsNotChunked() throws Exception {
testEmptyContents(false, false);
}
@Test
public void testEmptyContentNotsChunkedWithTrailers() throws Exception {
testEmptyContents(false, true);
}
private void testEmptyContents(boolean chunked, boolean trailers) throws Exception {
HttpRequestEncoder encoder = new HttpRequestEncoder();
EmbeddedChannel channel = new EmbeddedChannel(encoder);
HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
@ -178,7 +188,11 @@ public class HttpRequestEncoderTest {
assertTrue(channel.writeOutbound(new DefaultHttpContent(contentBuffer)));
ByteBuf lastContentBuffer = Unpooled.buffer();
assertTrue(channel.writeOutbound(new DefaultHttpContent(lastContentBuffer)));
LastHttpContent last = new DefaultLastHttpContent(lastContentBuffer);
if (trailers) {
last.trailingHeaders().set("X-Netty-Test", "true");
}
assertTrue(channel.writeOutbound(last));
// Ensure we only produce ByteBuf instances.
ByteBuf head = channel.readOutbound();

View File

@ -302,4 +302,55 @@ public class HttpResponseEncoderTest {
buffer = channel.readOutbound();
buffer.release();
}
@Test
public void testEmptyContentsChunked() throws Exception {
testEmptyContents(true, false);
}
@Test
public void testEmptyContentsChunkedWithTrailers() throws Exception {
testEmptyContents(true, true);
}
@Test
public void testEmptyContentsNotChunked() throws Exception {
testEmptyContents(false, false);
}
@Test
public void testEmptyContentNotsChunkedWithTrailers() throws Exception {
testEmptyContents(false, true);
}
private void testEmptyContents(boolean chunked, boolean trailers) throws Exception {
HttpResponseEncoder encoder = new HttpResponseEncoder();
EmbeddedChannel channel = new EmbeddedChannel(encoder);
HttpResponse request = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
if (chunked) {
HttpUtil.setTransferEncodingChunked(request, true);
}
assertTrue(channel.writeOutbound(request));
ByteBuf contentBuffer = Unpooled.buffer();
assertTrue(channel.writeOutbound(new DefaultHttpContent(contentBuffer)));
ByteBuf lastContentBuffer = Unpooled.buffer();
LastHttpContent last = new DefaultLastHttpContent(lastContentBuffer);
if (trailers) {
last.trailingHeaders().set("X-Netty-Test", "true");
}
assertTrue(channel.writeOutbound(last));
// Ensure we only produce ByteBuf instances.
ByteBuf head = channel.readOutbound();
assertTrue(head.release());
ByteBuf content = channel.readOutbound();
content.release();
ByteBuf lastContent = channel.readOutbound();
lastContent.release();
assertFalse(channel.finish());
}
}