Search in sources :

Example 21 with SessionOutputBuffer

use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.

the class TestIdentityOutputStream method testClose.

@Test
public void testClose() 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[10];
    Assertions.assertThrows(IOException.class, () -> out.write(tmp));
    Assertions.assertThrows(IOException.class, () -> out.write(1));
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 22 with SessionOutputBuffer

use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.

the class TestIdentityOutputStream method testBasics.

@Test
public void testBasics() throws Exception {
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16);
    final OutputStream out = new IdentityOutputStream(outbuffer, outputStream);
    final byte[] tmp = new byte[10];
    out.write(tmp, 0, 10);
    out.write(tmp);
    out.write(1);
    out.flush();
    out.close();
    final byte[] data = outputStream.toByteArray();
    Assertions.assertEquals(21, data.length);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 23 with SessionOutputBuffer

use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.

the class TestChunkEncoder method testChunkExceed.

@Test
public void testChunkExceed() throws Exception {
    final WritableByteChannelMock channel = new WritableByteChannelMock(64);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
    final ByteBuffer src = CodecTestUtils.wrap("0123456789ABCDEF");
    Assertions.assertEquals(16, encoder.write(src));
    Assertions.assertEquals(0, src.remaining());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("4\r\n0123\r\n4\r\n4567\r\n4\r\n89AB\r\n4\r\nCDEF\r\n", s);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 24 with SessionOutputBuffer

use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.

the class TestChunkEncoder method testBasicCoding.

@Test
public void testBasicCoding() throws Exception {
    final WritableByteChannelMock channel = new WritableByteChannelMock(64);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
    encoder.write(CodecTestUtils.wrap("12345"));
    encoder.write(CodecTestUtils.wrap("678"));
    encoder.write(CodecTestUtils.wrap("90"));
    encoder.complete();
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertTrue(encoder.isCompleted());
    Assertions.assertEquals("5\r\n12345\r\n3\r\n678\r\n2\r\n90\r\n0\r\n\r\n", s);
    Assertions.assertEquals("[chunk-coded; completed: true]", encoder.toString());
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) Test(org.junit.jupiter.api.Test)

Example 25 with SessionOutputBuffer

use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.

the class TestChunkEncoder 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 ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
    encoder.write(CodecTestUtils.wrap("12345"));
    encoder.write(CodecTestUtils.wrap("678"));
    encoder.write(CodecTestUtils.wrap("90"));
    encoder.complete();
    Assertions.assertThrows(IllegalStateException.class, () -> encoder.write(CodecTestUtils.wrap("more stuff")));
    Assertions.assertThrows(IllegalStateException.class, () -> encoder.complete());
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)88 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)63 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)54 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)52 ByteArrayOutputStream (java.io.ByteArrayOutputStream)34 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)30 SessionOutputBuffer (org.apache.hc.core5.http.io.SessionOutputBuffer)26 RandomAccessFile (java.io.RandomAccessFile)11 FileChannel (java.nio.channels.FileChannel)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 SessionInputBuffer (org.apache.hc.core5.http.io.SessionInputBuffer)10 OutputStream (java.io.OutputStream)9 WritableByteChannel (java.nio.channels.WritableByteChannel)9 ByteBuffer (java.nio.ByteBuffer)6 CharsetEncoder (java.nio.charset.CharsetEncoder)6 ReadableByteChannel (java.nio.channels.ReadableByteChannel)5 SessionInputBuffer (org.apache.hc.core5.http.nio.SessionInputBuffer)4 Header (org.apache.hc.core5.http.Header)3 BufferedReader (java.io.BufferedReader)1 BufferedWriter (java.io.BufferedWriter)1