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 b91426ff33
commit 23e8ff76c7

View File

@ -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());
}
}