Search in sources :

Example 36 with SessionOutputBuffer

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);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) RandomAccessFile(java.io.RandomAccessFile) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) FileChannel(java.nio.channels.FileChannel) Test(org.junit.jupiter.api.Test)

Example 37 with SessionOutputBuffer

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);
}
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 38 with SessionOutputBuffer

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

Example 39 with SessionOutputBuffer

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

Example 40 with SessionOutputBuffer

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();
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) 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