Search in sources :

Example 51 with SessionOutputBuffer

use of org.apache.hc.core5.http.io.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();
}
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 52 with SessionOutputBuffer

use of org.apache.hc.core5.http.io.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'));
}
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 53 with SessionOutputBuffer

use of org.apache.hc.core5.http.io.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);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 54 with SessionOutputBuffer

use of org.apache.hc.core5.http.io.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);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 55 with SessionOutputBuffer

use of org.apache.hc.core5.http.io.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")));
}
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