Search in sources :

Example 21 with BasicHttpTransportMetrics

use of org.apache.hc.core5.http.impl.BasicHttpTransportMetrics 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)

Example 22 with BasicHttpTransportMetrics

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

the class TestChunkDecoder method testBasicDecoding.

@Test
public void testBasicDecoding() throws Exception {
    final String s = "5\r\n01234\r\n5\r\n56789\r\n6\r\nabcdef\r\n0\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 = decoder.read(dst);
    Assertions.assertEquals(16, bytesRead);
    Assertions.assertEquals("0123456789abcdef", CodecTestUtils.convert(dst));
    final List<? extends Header> trailers = decoder.getTrailers();
    Assertions.assertNull(trailers);
    dst.clear();
    bytesRead = decoder.read(dst);
    Assertions.assertEquals(-1, bytesRead);
    Assertions.assertTrue(decoder.isCompleted());
    Assertions.assertEquals("[chunk-coded; completed: true]", decoder.toString());
}
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 23 with BasicHttpTransportMetrics

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

the class TestChunkDecoder method testMalformedChunkSizeDecoding.

@Test
public void testMalformedChunkSizeDecoding() throws Exception {
    final String s = "5\r\n01234\r\n5zz\r\n56789\r\n6\r\nabcdef\r\n0\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);
    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 24 with BasicHttpTransportMetrics

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

the class TestChunkDecoder method testMissingClosingChunk.

@Test
public void testMissingClosingChunk() throws Exception {
    final String s = "10\r\n1234567890123456\r\n" + "5\r\n12345\r\n5\r\n12345\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);
    Assertions.assertThrows(ConnectionClosedException.class, () -> {
        long bytesRead = 0;
        try {
            while (dst.hasRemaining() && !decoder.isCompleted()) {
                final int i = decoder.read(dst);
                if (i > 0) {
                    bytesRead += i;
                }
            }
        } catch (final MalformedChunkCodingException ex) {
            Assertions.assertEquals(26L, bytesRead);
            Assertions.assertEquals("12345678901234561234512345", CodecTestUtils.convert(dst));
            Assertions.assertTrue(decoder.isCompleted());
            throw ex;
        }
    });
}
Also used : MalformedChunkCodingException(org.apache.hc.core5.http.MalformedChunkCodingException) 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 25 with BasicHttpTransportMetrics

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

the class TestIdentityInputStream method testAvailableInStream.

@Test
public void testAvailableInStream() throws Exception {
    final byte[] input = new byte[] { 'a', 'b', 'c', 'd', 'e', 'f' };
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(input);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(new BasicHttpTransportMetrics(), 16, 0, 1024, null);
    final IdentityInputStream in = new IdentityInputStream(inBuffer, inputStream);
    final byte[] tmp = new byte[3];
    Assertions.assertEquals(3, in.read(tmp));
    Assertions.assertEquals(3, in.available());
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) 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