Search in sources :

Example 21 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer 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 22 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testReadByteBuffer.

@Test
public void testReadByteBuffer() throws Exception {
    final byte[] pattern = "0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
    final ReadableByteChannel channel = newChannel(pattern);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(4096, 1024, 0);
    while (inbuf.fill(channel) > 0) {
    }
    final ByteBuffer dst = ByteBuffer.allocate(10);
    Assertions.assertEquals(10, inbuf.read(dst));
    dst.flip();
    Assertions.assertEquals(dst, ByteBuffer.wrap(pattern, 0, 10));
    dst.clear();
    Assertions.assertEquals(6, inbuf.read(dst));
    dst.flip();
    Assertions.assertEquals(dst, ByteBuffer.wrap(pattern, 10, 6));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 23 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testLineLimit.

@Test
public void testLineLimit() throws Exception {
    final String s = "LoooooooooooooooooooooooooOOOOOOOOOOOOOOOOOOoooooooooooooooooooooong line\r\n";
    final CharArrayBuffer line = new CharArrayBuffer(64);
    final SessionInputBuffer inbuf1 = new SessionInputBufferImpl(128, 128);
    final ReadableByteChannel channel1 = newChannel(s);
    inbuf1.fill(channel1);
    Assertions.assertTrue(inbuf1.readLine(line, false));
    line.clear();
    final SessionInputBuffer inbuf2 = new SessionInputBufferImpl(128, 128, 10);
    final ReadableByteChannel channel2 = newChannel(s);
    inbuf2.fill(channel2);
    Assertions.assertThrows(MessageConstraintException.class, () -> inbuf2.readLine(line, false));
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 24 with SessionInputBuffer

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

the class TestSessionInOutBuffers method testWriteLineChunks.

@Test
public void testWriteLineChunks() throws Exception {
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(16, 16, 0);
    ReadableByteChannel inChannel = newChannel("One\r\nTwo\r\nThree");
    inbuf.fill(inChannel);
    final CharArrayBuffer line = new CharArrayBuffer(64);
    line.clear();
    Assertions.assertTrue(inbuf.readLine(line, false));
    Assertions.assertEquals("One", line.toString());
    outbuf.writeLine(line);
    line.clear();
    Assertions.assertTrue(inbuf.readLine(line, false));
    Assertions.assertEquals("Two", line.toString());
    outbuf.writeLine(line);
    line.clear();
    Assertions.assertFalse(inbuf.readLine(line, false));
    inChannel = newChannel("\r\nFour");
    inbuf.fill(inChannel);
    line.clear();
    Assertions.assertTrue(inbuf.readLine(line, false));
    Assertions.assertEquals("Three", line.toString());
    outbuf.writeLine(line);
    inbuf.fill(inChannel);
    line.clear();
    Assertions.assertTrue(inbuf.readLine(line, true));
    Assertions.assertEquals("Four", line.toString());
    outbuf.writeLine(line);
    line.clear();
    Assertions.assertFalse(inbuf.readLine(line, true));
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    final WritableByteChannel outChannel = newChannel(outStream);
    outbuf.flush(outChannel);
    final String s = new String(outStream.toByteArray(), StandardCharsets.US_ASCII);
    Assertions.assertEquals("One\r\nTwo\r\nThree\r\nFour\r\n", s);
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 25 with SessionInputBuffer

use of org.apache.hc.core5.http.io.SessionInputBuffer 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

Test (org.junit.jupiter.api.Test)123 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)62 ReadableByteChannel (java.nio.channels.ReadableByteChannel)61 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)61 ByteArrayInputStream (java.io.ByteArrayInputStream)58 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)47 ReadableByteChannelMock (org.apache.hc.core5.http.ReadableByteChannelMock)46 ByteBuffer (java.nio.ByteBuffer)30 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)25 ByteArrayOutputStream (java.io.ByteArrayOutputStream)22 Header (org.apache.hc.core5.http.Header)13 RandomAccessFile (java.io.RandomAccessFile)12 FileChannel (java.nio.channels.FileChannel)12 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)10 InputStream (java.io.InputStream)7 WritableByteChannel (java.nio.channels.WritableByteChannel)6 CharsetDecoder (java.nio.charset.CharsetDecoder)6 InterruptedIOException (java.io.InterruptedIOException)4 ClassicHttpRequest (org.apache.hc.core5.http.ClassicHttpRequest)4 ClassicHttpResponse (org.apache.hc.core5.http.ClassicHttpResponse)4