Search in sources :

Example 96 with SessionInputBuffer

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

the class TestChunkCoding method testCorruptChunkedInputStreamTruncatedChunk.

// Truncated chunk
@Test
public void testCorruptChunkedInputStreamTruncatedChunk() throws IOException {
    final String s = "3\r\n12";
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
    final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
    final byte[] buffer = new byte[300];
    Assertions.assertEquals(2, in.read(buffer));
    Assertions.assertThrows(MalformedChunkCodingException.class, () -> in.read(buffer));
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 97 with SessionInputBuffer

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

the class TestChunkCoding method testCorruptChunkedInputStreamInvalidSize.

// Invalid chunk size
@Test
public void testCorruptChunkedInputStreamInvalidSize() throws IOException {
    final String s = "whatever\r\n01234\r\n5\r\n56789\r\n0\r\n";
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
    final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
    Assertions.assertThrows(MalformedChunkCodingException.class, in::read);
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 98 with SessionInputBuffer

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

the class TestChunkCoding method testAvailable.

@Test
public void testAvailable() throws IOException {
    final String s = "5\r\n12345\r\n0\r\n";
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
    final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
    Assertions.assertEquals(0, in.available());
    in.read();
    Assertions.assertEquals(4, in.available());
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 99 with SessionInputBuffer

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

the class TestChunkCoding method testCorruptChunkedInputStreamMissingLF.

// Missing LF
@Test
public void testCorruptChunkedInputStreamMissingLF() throws IOException {
    final String s = "5\r01234\r\n5\r\n56789\r\n0\r\n";
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
    final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
    Assertions.assertThrows(MalformedChunkCodingException.class, in::read);
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) Test(org.junit.jupiter.api.Test)

Example 100 with SessionInputBuffer

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

the class TestChunkCoding method testChunkedInputStreamLargeBuffer.

@Test
public void testChunkedInputStreamLargeBuffer() throws IOException {
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(CHUNKED_INPUT.getBytes(StandardCharsets.ISO_8859_1));
    final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
    final byte[] buffer = new byte[300];
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len;
    while ((len = in.read(buffer)) > 0) {
        out.write(buffer, 0, len);
    }
    Assertions.assertEquals(-1, in.read(buffer));
    Assertions.assertEquals(-1, in.read(buffer));
    in.close();
    final String result = new String(out.toByteArray(), StandardCharsets.ISO_8859_1);
    Assertions.assertEquals(result, CHUNKED_RESULT);
    final Header[] footers = in.getFooters();
    Assertions.assertNotNull(footers);
    Assertions.assertEquals(2, footers.length);
    Assertions.assertEquals("Footer1", footers[0].getName());
    Assertions.assertEquals("abcde", footers[0].getValue());
    Assertions.assertEquals("Footer2", footers[1].getName());
    Assertions.assertEquals("fghij", footers[1].getValue());
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) Header(org.apache.hc.core5.http.Header) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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