use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestIdentityEncoder method testCodingEmptyFile.
@Test
public void testCodingEmptyFile() throws Exception {
final WritableByteChannelMock channel = new WritableByteChannelMock(64);
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
encoder.write(CodecTestUtils.wrap("stuff;"));
// Create an empty file
createTempFile();
RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
testfile.close();
testfile = new RandomAccessFile(this.tmpfile, "rw");
try {
final FileChannel fchannel = testfile.getChannel();
encoder.transfer(fchannel, 0, 20);
encoder.write(CodecTestUtils.wrap("more stuff"));
} finally {
testfile.close();
}
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertFalse(encoder.isCompleted());
Assertions.assertEquals("stuff;more stuff", s);
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestIdentityEncoder method testCodingFragmentBufferingMultipleFragments.
@Test
public void testCodingFragmentBufferingMultipleFragments() throws Exception {
final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64));
final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 32);
Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
Assertions.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff")));
Mockito.verify(channel, Mockito.never()).write(ArgumentMatchers.any());
Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any());
Mockito.verify(outbuf, Mockito.never()).flush(channel);
Assertions.assertEquals(0, metrics.getBytesTransferred());
outbuf.flush(channel);
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertEquals("stuff-more stuff", s);
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestChunkCoding method testChunkedOutputStreamClose.
@Test
public void testChunkedOutputStreamClose() throws IOException {
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2048);
out.close();
out.close();
Assertions.assertThrows(IOException.class, () -> out.write(new byte[] { 1, 2, 3 }));
Assertions.assertThrows(IOException.class, () -> out.write(1));
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestChunkCoding method testChunkedOutputStreamSmallChunk.
@Test
public void testChunkedOutputStreamSmallChunk() throws IOException {
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2);
out.write('1');
out.finish();
out.close();
final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
Assertions.assertEquals("1\r\n1\r\n0\r\n\r\n", content);
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer 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();
}
Aggregations