Search in sources :

Example 36 with ReadableByteChannelMock

use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.

the class TestFrameInOutBuffers method testReadFramePartialReads.

@Test
public void testReadFramePartialReads() throws Exception {
    final FrameInputBuffer inBuffer = new FrameInputBuffer(16 * 1024);
    final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(new byte[] { 0, 0 }, new byte[] { 10, 0, 9, 0 }, new byte[] { 0, 0, 8 }, new byte[] { 4 }, new byte[] { 1, 2, 3, 4 }, new byte[] { 5, 0 }, new byte[] { 0, 0, 0 });
    final RawFrame frame = inBuffer.read(readableChannel);
    Assertions.assertEquals(FrameType.DATA, FrameType.valueOf(frame.getType()));
    Assertions.assertEquals(FrameFlag.of(FrameFlag.END_STREAM, FrameFlag.PADDED), frame.getFlags());
    Assertions.assertEquals(8, frame.getStreamId());
    final ByteBuffer payload = frame.getPayloadContent();
    Assertions.assertNotNull(payload);
    Assertions.assertEquals(5, payload.remaining());
    Assertions.assertEquals(1, payload.get());
    Assertions.assertEquals(2, payload.get());
    Assertions.assertEquals(3, payload.get());
    Assertions.assertEquals(4, payload.get());
    Assertions.assertEquals(5, payload.get());
    Assertions.assertEquals(-1, readableChannel.read(ByteBuffer.allocate(1024)));
}
Also used : RawFrame(org.apache.hc.core5.http2.frame.RawFrame) ReadableByteChannelMock(org.apache.hc.core5.http2.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 37 with ReadableByteChannelMock

use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.

the class TestFrameInOutBuffers method testReadFrameExceedingLimit.

@Test
public void testReadFrameExceedingLimit() throws Exception {
    final FrameInputBuffer inBuffer = new FrameInputBuffer(16 * 1024);
    final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(new byte[] { 0, -128, -128, 0, 0, 0, 0, 0, 1 });
    Assertions.assertThrows(H2ConnectionException.class, () -> inBuffer.read(readableChannel));
}
Also used : ReadableByteChannelMock(org.apache.hc.core5.http2.ReadableByteChannelMock) Test(org.junit.jupiter.api.Test)

Example 38 with ReadableByteChannelMock

use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.

the class TestIdentityDecoder 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);
    Assertions.assertThrows(NullPointerException.class, () -> new IdentityDecoder(null, null, null));
    Assertions.assertThrows(NullPointerException.class, () -> new IdentityDecoder(channel, null, null));
    Assertions.assertThrows(NullPointerException.class, () -> new IdentityDecoder(channel, inbuf, null));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) Test(org.junit.jupiter.api.Test)

Example 39 with ReadableByteChannelMock

use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.

the class TestIdentityDecoder 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 IdentityDecoder decoder = new IdentityDecoder(channel, inbuf, metrics);
    Assertions.assertThrows(NullPointerException.class, () -> decoder.read(null));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) Test(org.junit.jupiter.api.Test)

Example 40 with ReadableByteChannelMock

use of org.apache.hc.core5.http.ReadableByteChannelMock in project httpcomponents-core by apache.

the class TestLengthDelimitedDecoder method testCodingBeyondContentLimit.

@Test
public void testCodingBeyondContentLimit() 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);
    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());
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)54 ReadableByteChannel (java.nio.channels.ReadableByteChannel)46 ReadableByteChannelMock (org.apache.hc.core5.http.ReadableByteChannelMock)46 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)46 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)44 ByteBuffer (java.nio.ByteBuffer)33 RandomAccessFile (java.io.RandomAccessFile)12 FileChannel (java.nio.channels.FileChannel)12 ReadableByteChannelMock (org.apache.hc.core5.http2.ReadableByteChannelMock)8 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)5 Http1Config (org.apache.hc.core5.http.config.Http1Config)2 MalformedChunkCodingException (org.apache.hc.core5.http.MalformedChunkCodingException)1 WritableByteChannelMock (org.apache.hc.core5.http2.WritableByteChannelMock)1 BasicH2TransportMetrics (org.apache.hc.core5.http2.impl.BasicH2TransportMetrics)1