use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestFrameInOutBuffers method testReadWriteFrame.
@Test
public void testReadWriteFrame() throws Exception {
final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024);
final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
final RawFrame frame = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5 }));
outbuffer.write(frame, writableChannel);
final FrameInputBuffer inBuffer = new FrameInputBuffer(16 * 1024);
final byte[] bytes = writableChannel.toByteArray();
Assertions.assertEquals(FrameConsts.HEAD_LEN + 5, bytes.length);
Assertions.assertEquals(1, outbuffer.getMetrics().getFramesTransferred());
Assertions.assertEquals(bytes.length, outbuffer.getMetrics().getBytesTransferred());
final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(bytes);
final RawFrame frame2 = inBuffer.read(readableChannel);
Assertions.assertEquals(FrameType.DATA.getValue(), frame2.getType());
Assertions.assertEquals(0, frame2.getFlags());
Assertions.assertEquals(1L, frame2.getStreamId());
final ByteBuffer payload2 = frame2.getPayloadContent();
Assertions.assertNotNull(payload2);
Assertions.assertEquals(5, payload2.remaining());
Assertions.assertEquals(1, payload2.get());
Assertions.assertEquals(2, payload2.get());
Assertions.assertEquals(3, payload2.get());
Assertions.assertEquals(4, payload2.get());
Assertions.assertEquals(5, payload2.get());
Assertions.assertEquals(-1, readableChannel.read(ByteBuffer.allocate(1024)));
Assertions.assertEquals(1, inBuffer.getMetrics().getFramesTransferred());
Assertions.assertEquals(bytes.length, inBuffer.getMetrics().getBytesTransferred());
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testInvalidConstructor.
@Test
public void testInvalidConstructor() {
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();
Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedDecoder(null, null, null, 10));
Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedDecoder(channel, null, null, 10));
Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedDecoder(channel, inbuf, null, 10));
Assertions.assertThrows(IllegalArgumentException.class, () -> new LengthDelimitedDecoder(channel, inbuf, metrics, -10));
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testDecodingFileWithOffsetAndBufferedSessionData.
@Test
public void testDecodingFileWithOffsetAndBufferedSessionData() 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(7, i);
final byte[] beginning = "beginning; ".getBytes(StandardCharsets.US_ASCII);
createTempFile();
RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
try {
testfile.write(beginning);
} finally {
testfile.close();
}
testfile = new RandomAccessFile(this.tmpfile, "rw");
try {
final FileChannel fchannel = testfile.getChannel();
long pos = beginning.length;
while (!decoder.isCompleted()) {
if (testfile.length() < pos) {
testfile.setLength(pos);
}
final long bytesRead = decoder.transfer(fchannel, pos, 10);
if (bytesRead > 0) {
pos += bytesRead;
}
}
} finally {
testfile.close();
}
// count everything except the initial 7 bytes that went to the session buffer
Assertions.assertEquals(this.tmpfile.length() - 7 - beginning.length, metrics.getBytesTransferred());
Assertions.assertEquals("beginning; stuff; more stuff; a lot more stuff!", CodecTestUtils.readFromFile(this.tmpfile));
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testInvalidInput.
@Test
public void testInvalidInput() throws Exception {
final String s = "stuff";
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 LengthDelimitedDecoder decoder = new LengthDelimitedDecoder(channel, inbuf, metrics, 3);
Assertions.assertThrows(NullPointerException.class, () -> decoder.read(null));
}
use of org.apache.hc.core5.http2.ReadableByteChannelMock in project httpcomponents-core by apache.
the class TestLengthDelimitedDecoder method testBasicDecodingFile.
/* ----------------- FileChannel Part testing --------------------------- */
@Test
public void testBasicDecodingFile() 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);
createTempFile();
try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
final FileChannel fchannel = testfile.getChannel();
long pos = 0;
while (!decoder.isCompleted()) {
final long bytesRead = decoder.transfer(fchannel, pos, 10);
if (bytesRead > 0) {
pos += bytesRead;
}
}
}
Assertions.assertEquals(this.tmpfile.length(), metrics.getBytesTransferred());
Assertions.assertEquals("stuff; more stuff; a lot more stuff!", CodecTestUtils.readFromFile(this.tmpfile));
}
Aggregations