Search in sources :

Example 61 with SessionInputBuffer

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

the class TestChunkCoding method testChunkedConsistence.

@Test
public void testChunkedConsistence() throws IOException {
    final String input = "76126;27823abcd;:q38a-\nkjc\rk%1ad\tkh/asdui\r\njkh+?\\suweb";
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2048);
    out.write(input.getBytes(StandardCharsets.ISO_8859_1));
    out.flush();
    out.close();
    out.close();
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    final ChunkedInputStream in = new ChunkedInputStream(inBuffer, inputStream);
    final byte[] d = new byte[10];
    final ByteArrayOutputStream result = new ByteArrayOutputStream();
    int len = 0;
    while ((len = in.read(d)) > 0) {
        result.write(d, 0, len);
    }
    final String output = new String(result.toByteArray(), StandardCharsets.ISO_8859_1);
    Assertions.assertEquals(input, output);
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 62 with SessionInputBuffer

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

the class TestChunkCoding method testChunkedInputStreamSmallBuffer.

// Test for when buffer is smaller than chunk size.
@Test
public void testChunkedInputStreamSmallBuffer() 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[7];
    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 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)

Example 63 with SessionInputBuffer

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

the class TestChunkCoding method testEmptyChunkedInputStream.

@Test
public void testEmptyChunkedInputStream() throws IOException {
    final String s = "0\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);
    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(0, out.size());
    in.close();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 64 with SessionInputBuffer

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

the class TestChunkCoding method testCorruptChunkedInputStreamInvalidFooter.

// Invalid footer
@Test
public void testCorruptChunkedInputStreamInvalidFooter() throws IOException {
    final String s = "1\r\n0\r\n0\r\nstuff\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);
    in.read();
    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 65 with SessionInputBuffer

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

the class TestChunkCoding method testCorruptChunkedInputStreamClose.

@Test
public void testCorruptChunkedInputStreamClose() 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);
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) 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