Search in sources :

Example 11 with ReadableByteChannelMock

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

the class TestLengthDelimitedDecoder method testDecodingFileWithBufferedSessionData.

@Test
public void testDecodingFileWithBufferedSessionData() 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);
    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() - 7, 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)

Example 12 with ReadableByteChannelMock

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

the class TestLengthDelimitedDecoder method testDecodingFromSessionBuffer1.

@Test
public void testDecodingFromSessionBuffer1() 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();
    inbuf.fill(channel);
    Assertions.assertEquals(6, inbuf.length());
    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(0, 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(10, metrics.getBytesTransferred());
    dst.clear();
    bytesRead = decoder.read(dst);
    Assertions.assertEquals(-1, bytesRead);
    Assertions.assertTrue(decoder.isCompleted());
    Assertions.assertEquals(10, 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)

Example 13 with ReadableByteChannelMock

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

the class TestLengthDelimitedDecoder method testWriteBeyondFileSize.

@Test
public void testWriteBeyondFileSize() throws Exception {
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { "a" }, 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, 1);
    createTempFile();
    try (RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw")) {
        final FileChannel fchannel = testfile.getChannel();
        Assertions.assertEquals(0, testfile.length());
        Assertions.assertThrows(IOException.class, () -> decoder.transfer(fchannel, 5, 10));
    }
}
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 14 with ReadableByteChannelMock

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

the class TestLengthDelimitedDecoder method testBasicDecodingSmallBuffer.

@Test
public void testBasicDecodingSmallBuffer() 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(4);
    int bytesRead = decoder.read(dst);
    Assertions.assertEquals(4, bytesRead);
    Assertions.assertEquals("stuf", CodecTestUtils.convert(dst));
    Assertions.assertFalse(decoder.isCompleted());
    Assertions.assertEquals(4, metrics.getBytesTransferred());
    dst.clear();
    bytesRead = decoder.read(dst);
    Assertions.assertEquals(2, bytesRead);
    Assertions.assertEquals("f;", CodecTestUtils.convert(dst));
    Assertions.assertFalse(decoder.isCompleted());
    Assertions.assertEquals(6, metrics.getBytesTransferred());
    dst.clear();
    bytesRead = decoder.read(dst);
    Assertions.assertEquals(4, bytesRead);
    Assertions.assertEquals("more", CodecTestUtils.convert(dst));
    Assertions.assertFalse(decoder.isCompleted());
    Assertions.assertEquals(10, metrics.getBytesTransferred());
    dst.clear();
    bytesRead = decoder.read(dst);
    Assertions.assertEquals(4, bytesRead);
    Assertions.assertEquals(" stu", CodecTestUtils.convert(dst));
    Assertions.assertFalse(decoder.isCompleted());
    Assertions.assertEquals(14, metrics.getBytesTransferred());
    dst.clear();
    bytesRead = decoder.read(dst);
    Assertions.assertEquals(2, bytesRead);
    Assertions.assertEquals("ff", 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)

Example 15 with ReadableByteChannelMock

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

the class TestChunkDecoder method testComplexDecoding.

@Test
public void testComplexDecoding() 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\nFooter2: 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);
    int bytesRead = 0;
    while (dst.hasRemaining() && !decoder.isCompleted()) {
        final int i = decoder.read(dst);
        if (i > 0) {
            bytesRead += i;
        }
    }
    Assertions.assertEquals(26, bytesRead);
    Assertions.assertEquals("12345678901234561234512345", CodecTestUtils.convert(dst));
    final List<? extends Header> trailers = decoder.getTrailers();
    Assertions.assertEquals(2, trailers.size());
    Assertions.assertEquals("Footer1", trailers.get(0).getName());
    Assertions.assertEquals("abcde", trailers.get(0).getValue());
    Assertions.assertEquals("Footer2", trailers.get(1).getName());
    Assertions.assertEquals("fghij", trailers.get(1).getValue());
    dst.clear();
    bytesRead = decoder.read(dst);
    Assertions.assertEquals(-1, bytesRead);
    Assertions.assertTrue(decoder.isCompleted());
}
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