Add also tests for HttpClientCodec that tests handling chunked responses. See #256 amd #259

This commit is contained in:
Norman Maurer 2012-04-15 20:58:40 +02:00
parent bc141f695b
commit e02225a80f

View File

@ -31,6 +31,10 @@ public class HttpClientCodecTest {
private static final String RESPONSE = "HTTP/1.0 200 OK\r\n" + "Date: Fri, 31 Dec 1999 23:59:59 GMT\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 28\r\n" + "\r\n"
+ "<html><body></body></html>\r\n";
private static final String INCOMPLETE_CHUNKED_RESPONSE = "HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Transfer-Encoding: chunked\r\n" + "\r\n"
+"5\r\n" + "first\r\n" + "6\r\n" + "second\r\n" + "0\r\n";
private static final String CHUNKED_RESPONSE = INCOMPLETE_CHUNKED_RESPONSE + "\r\n";
@Test
public void testFailsNotOnRequestResponse() {
@ -46,6 +50,20 @@ public class HttpClientCodecTest {
}
@Test
public void testFailsNotOnRequestResponseChunked() {
HttpClientCodec codec = new HttpClientCodec();
DecoderEmbedder<ChannelBuffer> decoder = new DecoderEmbedder<ChannelBuffer>(codec);
EncoderEmbedder<ChannelBuffer> encoder = new EncoderEmbedder<ChannelBuffer>(codec);
encoder.offer(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/"));
decoder.offer(ChannelBuffers.copiedBuffer(CHUNKED_RESPONSE, CharsetUtil.ISO_8859_1));
encoder.finish();
decoder.finish();
}
@Test
public void testFailsOnMissingResponse() {
HttpClientCodec codec = new HttpClientCodec();
@ -61,4 +79,24 @@ public class HttpClientCodecTest {
}
}
@Test
public void testFailsOnIncompleteChunkedResponse() {
HttpClientCodec codec = new HttpClientCodec();
DecoderEmbedder<ChannelBuffer> decoder = new DecoderEmbedder<ChannelBuffer>(codec);
EncoderEmbedder<ChannelBuffer> encoder = new EncoderEmbedder<ChannelBuffer>(codec);
encoder.offer(new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/"));
decoder.offer(ChannelBuffers.copiedBuffer(INCOMPLETE_CHUNKED_RESPONSE, CharsetUtil.ISO_8859_1));
try {
encoder.finish();
decoder.finish();
fail();
} catch (CodecEmbedderException e) {
assertTrue(e.getCause() instanceof PrematureChannelClosureException);
}
}
}