From 23e8ff76c7302c4505da5f6f28af7f117e8269ea Mon Sep 17 00:00:00 2001 From: Scott Mitchell Date: Fri, 16 Dec 2016 11:55:32 -0800 Subject: [PATCH] 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. --- .../codec/http/HttpRequestDecoderTest.java | 39 ++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java b/codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java index 9943167e2e..66dacce134 100644 --- a/codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java +++ b/codec-http/src/test/java/io/netty/handler/codec/http/HttpRequestDecoderTest.java @@ -23,7 +23,12 @@ import org.junit.Test; import java.util.List; -import static org.junit.Assert.*; +import static io.netty.handler.codec.http.HttpHeaders.Names.HOST; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class HttpRequestDecoderTest { private static final byte[] CONTENT_CRLF_DELIMITERS = createContent("\r\n"); @@ -175,4 +180,36 @@ public class HttpRequestDecoderTest { HttpRequest req = (HttpRequest) channel.readInbound(); assertEquals("", req.headers().get("EmptyHeader")); } + + @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 = (HttpRequest) channel.readInbound(); + assertEquals(HttpVersion.HTTP_1_1, req.getProtocolVersion()); + assertEquals("/some/path", req.getUri()); + assertFalse(req.headers().isEmpty()); + assertTrue("localhost1".equalsIgnoreCase(req.headers().get(HOST))); + LastHttpContent cnt = (LastHttpContent) channel.readInbound(); + cnt.release(); + + channel.writeInbound(Unpooled.copiedBuffer(str2, CharsetUtil.US_ASCII)); + req = (HttpRequest) channel.readInbound(); + assertEquals(HttpVersion.HTTP_1_0, req.getProtocolVersion()); + assertEquals("/some/other/path", req.getUri()); + assertFalse(req.headers().isEmpty()); + + assertTrue("localhost2".equalsIgnoreCase(req.headers().get(HOST))); + assertTrue("0".equalsIgnoreCase(req.headers().get(HttpHeaders.Names.CONTENT_LENGTH))); + cnt = (LastHttpContent) channel.readInbound(); + cnt.release(); + assertFalse(channel.finishAndReleaseAll()); + } }