use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.
the class TestContentLengthInputStream method testBasics.
@Test
public void testBasics() throws IOException {
final String s = "1234567890123456";
final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
final InputStream in = new ContentLengthInputStream(inBuffer, inputStream, 10L);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final byte[] buffer = new byte[50];
int len = in.read(buffer, 0, 2);
outputStream.write(buffer, 0, len);
len = in.read(buffer);
outputStream.write(buffer, 0, len);
final String result = new String(outputStream.toByteArray(), StandardCharsets.ISO_8859_1);
Assertions.assertEquals(result, "1234567890");
in.close();
}
use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.
the class TestContentLengthInputStream method testClose.
@Test
public void testClose() throws IOException {
final String s = "1234567890123456-";
final ByteArrayInputStream inputStream = new ByteArrayInputStream(s.getBytes(StandardCharsets.ISO_8859_1));
final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16);
final InputStream in = new ContentLengthInputStream(inBuffer, inputStream, 16L);
in.close();
in.close();
Assertions.assertThrows(StreamClosedException.class, in::read);
final byte[] tmp = new byte[10];
Assertions.assertThrows(StreamClosedException.class, () -> in.read(tmp));
Assertions.assertThrows(StreamClosedException.class, () -> in.read(tmp, 0, tmp.length));
Assertions.assertEquals('-', inBuffer.read(inputStream));
}
use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.
the class TestContentLengthInputStream method testSkip.
@Test
public void testSkip() throws IOException {
final ByteArrayInputStream inputStream1 = new ByteArrayInputStream(new byte[20]);
final SessionInputBuffer inBuffer1 = new SessionInputBufferImpl(16);
final InputStream in1 = new ContentLengthInputStream(inBuffer1, inputStream1, 10L);
Assertions.assertEquals(10, in1.skip(10));
Assertions.assertEquals(-1, in1.read());
in1.close();
final ByteArrayInputStream inputStream2 = new ByteArrayInputStream(new byte[20]);
final SessionInputBuffer inBuffer2 = new SessionInputBufferImpl(16);
final InputStream in2 = new ContentLengthInputStream(inBuffer2, inputStream2, 10L);
in2.read();
Assertions.assertEquals(9, in2.skip(10));
Assertions.assertEquals(-1, in2.read());
in2.close();
final ByteArrayInputStream inputStream3 = new ByteArrayInputStream(new byte[20]);
final SessionInputBuffer inBuffer3 = new SessionInputBufferImpl(16);
final InputStream in3 = new ContentLengthInputStream(inBuffer3, inputStream3, 2L);
in3.read();
in3.read();
Assertions.assertTrue(in3.skip(10) <= 0);
Assertions.assertEquals(0, in3.skip(-1));
Assertions.assertEquals(-1, in3.read());
in3.close();
final ByteArrayInputStream inputStream4 = new ByteArrayInputStream(new byte[20]);
final SessionInputBuffer inBuffer4 = new SessionInputBufferImpl(16);
final InputStream in4 = new ContentLengthInputStream(inBuffer4, inputStream4, 10L);
Assertions.assertEquals(5, in4.skip(5));
Assertions.assertEquals(5, in4.read(new byte[20]));
in4.close();
}
use of org.apache.hc.core5.http.io.SessionInputBuffer in project httpcomponents-core by apache.
the class TestChunkCoding method testCorruptChunkedInputStreamNegativeSize.
// Negative chunk size
@Test
public void testCorruptChunkedInputStreamNegativeSize() throws IOException {
final String s = "-5\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.io.SessionInputBuffer in project httpcomponents-core by apache.
the class TestChunkCoding method testChunkedInputStreamClose.
@Test
public void testChunkedInputStreamClose() throws IOException {
final String s = "5\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);
in.close();
in.close();
Assertions.assertThrows(StreamClosedException.class, () -> in.read());
final byte[] tmp = new byte[10];
Assertions.assertThrows(StreamClosedException.class, () -> in.read(tmp));
Assertions.assertThrows(StreamClosedException.class, () -> in.read(tmp, 0, tmp.length));
}
Aggregations