use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestChunkDecoder method testBasicDecoding.
@Test
public void testBasicDecoding() throws Exception {
final String s = "5\r\n01234\r\n5\r\n56789\r\n6\r\nabcdef\r\n0\r\n\r\n";
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
final ByteBuffer dst = ByteBuffer.allocate(1024);
int bytesRead = decoder.read(dst);
Assertions.assertEquals(16, bytesRead);
Assertions.assertEquals("0123456789abcdef", CodecTestUtils.convert(dst));
final List<? extends Header> trailers = decoder.getTrailers();
Assertions.assertNull(trailers);
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(-1, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals("[chunk-coded; completed: true]", decoder.toString());
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestChunkDecoder method testMalformedChunkSizeDecoding.
@Test
public void testMalformedChunkSizeDecoding() throws Exception {
final String s = "5\r\n01234\r\n5zz\r\n56789\r\n6\r\nabcdef\r\n0\r\n\r\n";
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
final ByteBuffer dst = ByteBuffer.allocate(1024);
Assertions.assertThrows(MalformedChunkCodingException.class, () -> decoder.read(dst));
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestChunkDecoder method testMissingClosingChunk.
@Test
public void testMissingClosingChunk() throws Exception {
final String s = "10\r\n1234567890123456\r\n" + "5\r\n12345\r\n5\r\n12345\r\n";
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
final ByteBuffer dst = ByteBuffer.allocate(1024);
Assertions.assertThrows(ConnectionClosedException.class, () -> {
long bytesRead = 0;
try {
while (dst.hasRemaining() && !decoder.isCompleted()) {
final int i = decoder.read(dst);
if (i > 0) {
bytesRead += i;
}
}
} catch (final MalformedChunkCodingException ex) {
Assertions.assertEquals(26L, bytesRead);
Assertions.assertEquals("12345678901234561234512345", CodecTestUtils.convert(dst));
Assertions.assertTrue(decoder.isCompleted());
throw ex;
}
});
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestChunkDecoder method testMalformedChunk.
@Test
public void testMalformedChunk() throws Exception {
final String s = "5\r\n01234----------------------------------------------------------" + "-----------------------------------------------------------------------------" + "-----------------------------------------------------------------------------";
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(32, 32, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
final ByteBuffer dst = ByteBuffer.allocate(1024);
Assertions.assertThrows(MalformedChunkCodingException.class, () -> decoder.read(dst));
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestChunkDecoder method testMalformedChunkTruncatedChunk.
@Test
public void testMalformedChunkTruncatedChunk() throws Exception {
final String s = "3\r\n12";
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
final ByteBuffer dst = ByteBuffer.allocate(1024);
Assertions.assertEquals(2, decoder.read(dst));
Assertions.assertThrows(TruncatedChunkException.class, () -> decoder.read(dst));
}
Aggregations