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