Search in sources :

Example 6 with ReadableByteChannelMock

use of org.apache.hc.core5.http.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());
}
Also used : WritableByteChannelMock(org.apache.hc.core5.http2.WritableByteChannelMock) 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 7 with ReadableByteChannelMock

use of org.apache.hc.core5.http.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));
}
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 8 with ReadableByteChannelMock

use of org.apache.hc.core5.http.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));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) RandomAccessFile(java.io.RandomAccessFile) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) FileChannel(java.nio.channels.FileChannel) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) Test(org.junit.jupiter.api.Test)

Example 9 with ReadableByteChannelMock

use of org.apache.hc.core5.http.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));
}
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 10 with ReadableByteChannelMock

use of org.apache.hc.core5.http.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));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) RandomAccessFile(java.io.RandomAccessFile) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) FileChannel(java.nio.channels.FileChannel) ReadableByteChannelMock(org.apache.hc.core5.http.ReadableByteChannelMock) 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