Search in sources :

Example 26 with BasicHttpTransportMetrics

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

the class TestChunkDecoder method testMalformedChunk.

@Test
public void testMalformedChunk() throws Exception {
    final String s = "5\r\n01234----------------------------------------------------------" + "-----------------------------------------------------------------------------" + "-----------------------------------------------------------------------------";
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s }, StandardCharsets.US_ASCII);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(32, 32, 0, StandardCharsets.US_ASCII);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkDecoder decoder = new ChunkDecoder(channel, inbuf, metrics);
    final ByteBuffer dst = ByteBuffer.allocate(1024);
    Assertions.assertThrows(MalformedChunkCodingException.class, () -> decoder.read(dst));
}
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 27 with BasicHttpTransportMetrics

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

the class TestChunkDecoder method testMalformedChunkTruncatedChunk.

@Test
public void testMalformedChunkTruncatedChunk() throws Exception {
    final String s = "3\r\n12";
    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);
    Assertions.assertEquals(2, decoder.read(dst));
    Assertions.assertThrows(TruncatedChunkException.class, () -> decoder.read(dst));
}
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 28 with BasicHttpTransportMetrics

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

the class TestChunkDecoder method testReadingWitSmallBuffer.

@Test
public void testReadingWitSmallBuffer() throws Exception {
    final String s = "10\r\n1234567890123456\r\n" + "40\r\n12345678901234561234567890123456" + "12345678901234561234567890123456\r\n0\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);
    final ByteBuffer tmp = ByteBuffer.allocate(10);
    int bytesRead = 0;
    while (dst.hasRemaining() && !decoder.isCompleted()) {
        final int i = decoder.read(tmp);
        if (i > 0) {
            bytesRead += i;
            tmp.flip();
            dst.put(tmp);
            tmp.compact();
        }
    }
    Assertions.assertEquals(80, bytesRead);
    Assertions.assertEquals("12345678901234561234567890123456" + "12345678901234561234567890123456" + "1234567890123456", CodecTestUtils.convert(dst));
    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)

Example 29 with BasicHttpTransportMetrics

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

the class TestChunkDecoder method testDecodingWithSmallBuffer.

@Test
public void testDecodingWithSmallBuffer() throws Exception {
    final String s1 = "5\r\n01234\r\n5\r\n5678";
    final String s2 = "9\r\n6\r\nabcdef\r\n0\r\n\r\n";
    final ReadableByteChannel channel = new ReadableByteChannelMock(new String[] { s1, s2 }, 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);
    final ByteBuffer tmp = ByteBuffer.allocate(4);
    int bytesRead = 0;
    while (dst.hasRemaining() && !decoder.isCompleted()) {
        final int i = decoder.read(tmp);
        if (i > 0) {
            bytesRead += i;
        }
        tmp.flip();
        dst.put(tmp);
        tmp.compact();
    }
    Assertions.assertEquals(16, bytesRead);
    Assertions.assertEquals("0123456789abcdef", CodecTestUtils.convert(dst));
    Assertions.assertTrue(decoder.isCompleted());
    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)

Example 30 with BasicHttpTransportMetrics

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

the class TestChunkDecoder method testMissingLastCRLF.

@Test
public void testMissingLastCRLF() throws Exception {
    final String s = "10\r\n1234567890123456\r\n" + "5\r\n12345\r\n5\r\n12345";
    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);
    Assertions.assertThrows(MalformedChunkCodingException.class, () -> {
        while (dst.hasRemaining() && !decoder.isCompleted()) {
            decoder.read(dst);
        }
    });
}
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

BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)99 Test (org.junit.jupiter.api.Test)98 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)51 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)50 ReadableByteChannel (java.nio.channels.ReadableByteChannel)44 ReadableByteChannelMock (org.apache.hc.core5.http.ReadableByteChannelMock)44 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)44 ByteBuffer (java.nio.ByteBuffer)34 RandomAccessFile (java.io.RandomAccessFile)23 FileChannel (java.nio.channels.FileChannel)23 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)11 OutputStream (java.io.OutputStream)3 Http1Config (org.apache.hc.core5.http.config.Http1Config)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 MalformedChunkCodingException (org.apache.hc.core5.http.MalformedChunkCodingException)2 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)2 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1 IOException (java.io.IOException)1