use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testCodingBeyondContentLimitFile.
@Test
public void testCodingBeyondContentLimitFile() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff;", "more stuff; and a lot more stuff" }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final LengthDelimitedDecoder decoder = new LengthDelimitedDecoder(channel, inbuf, metrics, 16);
createTempFile();
try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
final FileChannel fchannel = testfile.getChannel();
long bytesRead = decoder.transfer(fchannel, 0, 6);
Assertions.assertEquals(6, bytesRead);
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(6, metrics.getBytesTransferred());
bytesRead = decoder.transfer(fchannel, 0, 10);
Assertions.assertEquals(10, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(16, metrics.getBytesTransferred());
bytesRead = decoder.transfer(fchannel, 0, 1);
Assertions.assertEquals(-1, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(16, metrics.getBytesTransferred());
}
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testTruncatedContent.
@Test
public void testTruncatedContent() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "1234567890" }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final LengthDelimitedDecoder decoder = new LengthDelimitedDecoder(channel, inbuf, metrics, 20);
final ByteBuffer dst = ByteBuffer.allocate(1024);
final int bytesRead = decoder.read(dst);
Assertions.assertEquals(10, bytesRead);
Assertions.assertThrows(ConnectionClosedException.class, () -> decoder.read(dst));
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestChunkDecoder method testFoldedFooters.
@Test
public void testFoldedFooters() throws Exception {
final String s = "10;key=\"value\"\r\n1234567890123456\r\n" + "5\r\n12345\r\n5\r\n12345\r\n0\r\nFooter1: abcde\r\n \r\n fghij\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);
final int bytesRead = decoder.read(dst);
Assertions.assertEquals(26, bytesRead);
Assertions.assertEquals("12345678901234561234512345", CodecTestUtils.convert(dst));
final List<? extends Header> trailers = decoder.getTrailers();
Assertions.assertEquals(1, trailers.size());
Assertions.assertEquals("Footer1", trailers.get(0).getName());
Assertions.assertEquals("abcde fghij", trailers.get(0).getValue());
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestChunkDecoder method testHugeChunk.
@Test
public void testHugeChunk() throws Exception {
final String s = "1234567890abcdef\r\n0123456789abcdef";
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(4);
int bytesRead = decoder.read(dst);
Assertions.assertEquals(4, bytesRead);
Assertions.assertEquals("0123", CodecTestUtils.convert(dst));
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(4, bytesRead);
Assertions.assertEquals("4567", CodecTestUtils.convert(dst));
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestChunkDecoder method testTooManyFooters.
@Test
public void testTooManyFooters() throws Exception {
final String s = "10\r\n1234567890123456\r\n" + "0\r\nFooter1: blah\r\nFooter2: blah\r\nFooter3: blah\r\nFooter4: blah\r\n\r\n";
final ReadableByteChannel channel1 = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf1 = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics1 = new BasicHttpTransportMetrics();
final ChunkDecoder decoder1 = new ChunkDecoder(channel1, inbuf1, metrics1);
final ByteBuffer dst = ByteBuffer.allocate(1024);
final int bytesRead = decoder1.read(dst);
Assertions.assertEquals(16, bytesRead);
Assertions.assertEquals("1234567890123456", CodecTestUtils.convert(dst));
final List<? extends Header> trailers = decoder1.getTrailers();
Assertions.assertNotNull(trailers);
Assertions.assertEquals(4, trailers.size());
final Http1Config http1Config = Http1Config.custom().setMaxHeaderCount(3).build();
final ReadableByteChannel channel2 = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
final SessionInputBuffer inbuf2 = new SessionInputBufferImpl(1024, 256, 0, StandardCharsets.US_ASCII);
final BasicHttpTransportMetrics metrics2 = new BasicHttpTransportMetrics();
final ChunkDecoder decoder2 = new ChunkDecoder(channel2, inbuf2, http1Config, metrics2);
dst.clear();
Assertions.assertThrows(MessageConstraintException.class, () -> decoder2.read(dst));
}
Aggregations