Add unit test for HttpObjectDecoder with message split on buffer boundaries

Motivation:
We should have a unit test which explicitly tests a HTTP message being split between multiple ByteBuf objects.

Modifications:
- Add a unit test to HttpRequestDecoderTest which splits a request between 2 ByteBuf objects

Result:
More unit test coverage for HttpObjectDecoder.
This commit is contained in:
Scott Mitchell 2016-12-16 11:55:32 -08:00
parent 7c630feefc
commit 3f82b53bae

View File

@ -17,11 +17,13 @@ package io.netty.handler.codec.http;
import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.AsciiString;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import java.util.List;
import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static io.netty.handler.codec.http.HttpHeadersTestUtils.of;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
@ -237,4 +239,35 @@ public class HttpRequestDecoderTest {
assertThat(channel.finish(), is(false));
}
@Test
public void testMessagesSplitBetweenMultipleBuffers() {
EmbeddedChannel channel = new EmbeddedChannel(new HttpRequestDecoder());
String crlf = "\r\n";
String str1 = "GET /some/path HTTP/1.1" + crlf +
"Host: localhost1" + crlf + crlf +
"GET /some/other/path HTTP/1.0" + crlf +
"Hos";
String str2 = "t: localhost2" + crlf +
"content-length: 0" + crlf + crlf;
channel.writeInbound(Unpooled.copiedBuffer(str1, CharsetUtil.US_ASCII));
HttpRequest req = channel.readInbound();
assertEquals(HttpVersion.HTTP_1_1, req.protocolVersion());
assertEquals("/some/path", req.uri());
assertEquals(1, req.headers().size());
assertTrue(AsciiString.contentEqualsIgnoreCase("localhost1", req.headers().get(HOST)));
LastHttpContent cnt = channel.readInbound();
cnt.release();
channel.writeInbound(Unpooled.copiedBuffer(str2, CharsetUtil.US_ASCII));
req = channel.readInbound();
assertEquals(HttpVersion.HTTP_1_0, req.protocolVersion());
assertEquals("/some/other/path", req.uri());
assertEquals(2, req.headers().size());
assertTrue(AsciiString.contentEqualsIgnoreCase("localhost2", req.headers().get(HOST)));
assertTrue(AsciiString.contentEqualsIgnoreCase("0", req.headers().get(HttpHeaderNames.CONTENT_LENGTH)));
cnt = channel.readInbound();
cnt.release();
assertFalse(channel.finishAndReleaseAll());
}
}