Search in sources :

Example 31 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testMultibyteCodedReadWriteLongLine.

@Test
public void testMultibyteCodedReadWriteLongLine() throws Exception {
    final String s1 = constructString(SWISS_GERMAN_HELLO);
    final String s2 = constructString(RUSSIAN_HELLO);
    final String s3 = "Like hello and stuff";
    final StringBuilder buf = new StringBuilder();
    for (int i = 0; i < 1024; i++) {
        buf.append(s1).append(s2).append(s3);
    }
    final String s = buf.toString();
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(16, StandardCharsets.UTF_8.newEncoder());
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.append(s);
    outbuffer.writeLine(chbuffer, outputStream);
    outbuffer.flush(outputStream);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.UTF_8.newDecoder());
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    chbuffer.clear();
    inBuffer.readLine(chbuffer, inputStream);
    Assertions.assertEquals(s, chbuffer.toString());
}
Also used : SessionInputBuffer(org.apache.hc.core5.http.io.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) ByteArrayInputStream(java.io.ByteArrayInputStream) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 32 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testWriteSmallFragmentBuffering.

@Test
public void testWriteSmallFragmentBuffering() throws Exception {
    final OutputStream outputStream = Mockito.mock(OutputStream.class);
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(new BasicHttpTransportMetrics(), 16, 16, null);
    outbuffer.write(1, outputStream);
    outbuffer.write(2, outputStream);
    outbuffer.write(new byte[] { 1, 2 }, outputStream);
    outbuffer.write(new byte[] { 3, 4 }, outputStream);
    outbuffer.flush(outputStream);
    Mockito.verify(outputStream, Mockito.times(1)).write(ArgumentMatchers.any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt());
    Mockito.verify(outputStream, Mockito.never()).write(ArgumentMatchers.anyInt());
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 33 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testWriteSmallFragmentNoBuffering.

@Test
public void testWriteSmallFragmentNoBuffering() throws Exception {
    final OutputStream outputStream = Mockito.mock(OutputStream.class);
    final SessionOutputBuffer outbuffer = new SessionOutputBufferImpl(new BasicHttpTransportMetrics(), 16, 0, null);
    outbuffer.write(1, outputStream);
    outbuffer.write(2, outputStream);
    outbuffer.write(new byte[] { 1, 2 }, outputStream);
    outbuffer.write(new byte[] { 3, 4 }, outputStream);
    Mockito.verify(outputStream, Mockito.times(2)).write(ArgumentMatchers.any(), ArgumentMatchers.anyInt(), ArgumentMatchers.anyInt());
    Mockito.verify(outputStream, Mockito.times(2)).write(ArgumentMatchers.anyInt());
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) BasicHttpTransportMetrics(org.apache.hc.core5.http.impl.BasicHttpTransportMetrics) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 34 with SessionOutputBuffer

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

the class TestIdentityEncoder 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 IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
    Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
    encoder.complete();
    Assertions.assertTrue(encoder.isCompleted());
    Assertions.assertEquals(5, metrics.getBytesTransferred());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("stuff", s);
    Assertions.assertEquals("[identity; 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 35 with SessionOutputBuffer

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

the class TestIdentityEncoder method testInvalidConstructor.

@Test
public void testInvalidConstructor() {
    final WritableByteChannelMock channel = new WritableByteChannelMock(64);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
    Assertions.assertThrows(NullPointerException.class, () -> new IdentityEncoder(null, null, null));
    Assertions.assertThrows(NullPointerException.class, () -> new IdentityEncoder(channel, null, null));
    Assertions.assertThrows(NullPointerException.class, () -> new IdentityEncoder(channel, outbuf, null));
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) 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