use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestIdentityOutputStream method testBasicWrite.
@Test
public void testBasicWrite() throws Exception {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
final OutputStream out = new IdentityOutputStream(outbuffer, outputStream);
out.write(new byte[] { 'a', 'b' }, 0, 2);
out.write('c');
out.flush();
final byte[] input = outputStream.toByteArray();
Assertions.assertNotNull(input);
final byte[] expected = new byte[] { 'a', 'b', 'c' };
Assertions.assertEquals(expected.length, input.length);
for (int i = 0; i < expected.length; i++) {
Assertions.assertEquals(expected[i], input[i]);
}
out.close();
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestIdentityOutputStream method testClosedCondition.
@Test
public void testClosedCondition() throws Exception {
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
final OutputStream out = new IdentityOutputStream(outbuffer, outputStream);
out.close();
out.close();
final byte[] tmp = new byte[2];
Assertions.assertThrows(StreamClosedException.class, () -> out.write(tmp, 0, tmp.length));
Assertions.assertThrows(StreamClosedException.class, () -> out.write('a'));
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestChunkCoding method testChunkedOutputStream.
@Test
public void testChunkedOutputStream() 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.write('2');
out.write('3');
out.write('4');
out.finish();
out.close();
final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
Assertions.assertEquals("2\r\n12\r\n2\r\n34\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 testChunkedOutputStreamWithTrailers.
@Test
public void testChunkedOutputStreamWithTrailers() throws IOException {
final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
final ChunkedOutputStream out = new ChunkedOutputStream(outbuffer, outputStream, 2, () -> Arrays.asList(new BasicHeader("E", ""), new BasicHeader("Y", "Z")));
out.write('x');
out.finish();
out.close();
final String content = new String(outputStream.toByteArray(), StandardCharsets.US_ASCII);
Assertions.assertEquals("1\r\nx\r\n0\r\nE: \r\nY: Z\r\n\r\n", content);
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestLengthDelimitedEncoder method testCodingCompleted.
@Test
public void testCodingCompleted() throws Exception {
final WritableByteChannelMock channel = new WritableByteChannelMock(64);
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 5);
encoder.write(CodecTestUtils.wrap("stuff"));
Assertions.assertThrows(IllegalStateException.class, () -> encoder.write(CodecTestUtils.wrap("more stuff")));
}
Aggregations