use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testTruncatedContentWithFile.
@Test
public void testTruncatedContentWithFile() 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);
createTempFile();
try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
final FileChannel fchannel = testfile.getChannel();
final long bytesRead = decoder.transfer(fchannel, 0, Integer.MAX_VALUE);
Assertions.assertEquals(10, bytesRead);
Assertions.assertThrows(ConnectionClosedException.class, () -> decoder.transfer(fchannel, 0, Integer.MAX_VALUE));
}
}
use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testZeroLengthDecoding.
@Test
public void testZeroLengthDecoding() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "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, 0);
final ByteBuffer dst = ByteBuffer.allocate(1024);
final int bytesRead = decoder.read(dst);
Assertions.assertEquals(-1, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(0, metrics.getBytesTransferred());
}
use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testDecodingFromSessionBuffer2.
@Test
public void testDecodingFromSessionBuffer2() 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();
inbuf.fill(channel);
inbuf.fill(channel);
Assertions.assertEquals(38, inbuf.length());
final LengthDelimitedDecoder decoder = new LengthDelimitedDecoder(channel, inbuf, metrics, 16);
final ByteBuffer dst = ByteBuffer.allocate(1024);
int bytesRead = decoder.read(dst);
Assertions.assertEquals(16, bytesRead);
Assertions.assertEquals("stuff;more stuff", CodecTestUtils.convert(dst));
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(0, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(-1, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(0, metrics.getBytesTransferred());
}
use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testDecodingFileWithLimit.
@Test
public void testDecodingFileWithLimit() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff; more stuff; ", "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, 36);
final int i = inbuf.fill(channel);
Assertions.assertEquals(19, i);
createTempFile();
try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
final FileChannel fchannel = testfile.getChannel();
long pos = 0;
// transferred from buffer
long bytesRead = decoder.transfer(fchannel, pos, 1);
Assertions.assertEquals(1, bytesRead);
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(0, metrics.getBytesTransferred());
pos += bytesRead;
bytesRead = decoder.transfer(fchannel, pos, 2);
Assertions.assertEquals(2, bytesRead);
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(0, metrics.getBytesTransferred());
pos += bytesRead;
bytesRead = decoder.transfer(fchannel, pos, 17);
Assertions.assertEquals(16, bytesRead);
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(0, metrics.getBytesTransferred());
pos += bytesRead;
// transferred from channel
bytesRead = decoder.transfer(fchannel, pos, 1);
Assertions.assertEquals(1, bytesRead);
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(1, metrics.getBytesTransferred());
pos += bytesRead;
bytesRead = decoder.transfer(fchannel, pos, 2);
Assertions.assertEquals(2, bytesRead);
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(3, metrics.getBytesTransferred());
pos += bytesRead;
bytesRead = decoder.transfer(fchannel, pos, 15);
Assertions.assertEquals(14, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(17, metrics.getBytesTransferred());
pos += bytesRead;
bytesRead = decoder.transfer(fchannel, pos, 1);
Assertions.assertEquals(-1, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(17, metrics.getBytesTransferred());
}
Assertions.assertEquals("stuff; more stuff; a lot more stuff!", CodecTestUtils.readFromFile(this.tmpfile));
}
use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testBasicDecoding.
@Test
public void testBasicDecoding() throws Exception {
final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "stuff;", "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);
final ByteBuffer dst = ByteBuffer.allocate(1024);
int bytesRead = decoder.read(dst);
Assertions.assertEquals(6, bytesRead);
Assertions.assertEquals("stuff;", CodecTestUtils.convert(dst));
Assertions.assertFalse(decoder.isCompleted());
Assertions.assertEquals(6, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(10, bytesRead);
Assertions.assertEquals("more stuff", CodecTestUtils.convert(dst));
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(16, metrics.getBytesTransferred());
dst.clear();
bytesRead = decoder.read(dst);
Assertions.assertEquals(-1, bytesRead);
Assertions.assertTrue(decoder.isCompleted());
Assertions.assertEquals(16, metrics.getBytesTransferred());
Assertions.assertEquals("[content length: 16; pos: 16; completed: true]", decoder.toString());
}
Aggregations