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();
}
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();
}
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();
}
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();
}
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());
}
Aggregations