Search in sources :

Example 66 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testMultibyteCodedReadWriteLine.

@Test
public void testMultibyteCodedReadWriteLine() throws Exception {
    final String s1 = constructString(SWISS_GERMAN_HELLO);
    final String s2 = constructString(RUSSIAN_HELLO);
    final String s3 = "Like hello and stuff";
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, StandardCharsets.UTF_8.newEncoder());
    final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
    for (int i = 0; i < 10; i++) {
        chbuffer.clear();
        chbuffer.append(s1);
        outbuf.writeLine(chbuffer);
        chbuffer.clear();
        chbuffer.append(s2);
        outbuf.writeLine(chbuffer);
        chbuffer.clear();
        chbuffer.append(s3);
        outbuf.writeLine(chbuffer);
    }
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    final WritableByteChannel outChannel = newChannel(outStream);
    outbuf.flush(outChannel);
    final byte[] tmp = outStream.toByteArray();
    final ReadableByteChannel channel = newChannel(tmp);
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(16, 16, 0, StandardCharsets.UTF_8.newDecoder());
    while (inbuf.fill(channel) > 0) {
    }
    for (int i = 0; i < 10; i++) {
        chbuffer.clear();
        inbuf.readLine(chbuffer, true);
        Assertions.assertEquals(s1, chbuffer.toString());
        chbuffer.clear();
        inbuf.readLine(chbuffer, true);
        Assertions.assertEquals(s2, chbuffer.toString());
        chbuffer.clear();
        inbuf.readLine(chbuffer, true);
        Assertions.assertEquals(s3, chbuffer.toString());
    }
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionInputBuffer(org.apache.hc.core5.http.nio.SessionInputBuffer) SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 67 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testInputMatchesBufferLength.

@Test
public void testInputMatchesBufferLength() throws Exception {
    final String s1 = "abcde";
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 5);
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.append(s1);
    outbuf.writeLine(chbuffer);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) Test(org.junit.jupiter.api.Test)

Example 68 with SessionOutputBuffer

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

the class TestIdentityEncoder method testCodingFragmentBufferingTinyFragments.

@Test
public void testCodingFragmentBufferingTinyFragments() 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, 1);
    Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
    Assertions.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff")));
    Mockito.verify(channel, Mockito.times(5)).write(ArgumentMatchers.any());
    Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any());
    Mockito.verify(outbuf, Mockito.times(3)).flush(channel);
    Assertions.assertEquals(18, 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 69 with SessionOutputBuffer

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

the class TestIdentityEncoder method testCodingFragmentBufferingBufferFlush2.

@Test
public void testCodingFragmentBufferingBufferFlush2() 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, 8);
    Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
    Assertions.assertEquals(16, encoder.write(CodecTestUtils.wrap("-much more stuff")));
    Mockito.verify(channel, Mockito.times(2)).write(ArgumentMatchers.any());
    Mockito.verify(outbuf, Mockito.times(1)).write(ArgumentMatchers.<ByteBuffer>any());
    Mockito.verify(outbuf, Mockito.times(1)).flush(channel);
    Assertions.assertEquals(21, metrics.getBytesTransferred());
    Assertions.assertEquals(0, outbuf.length());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("stuff-much 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 70 with SessionOutputBuffer

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

the class TestIdentityEncoder method testCodingFragmentBufferingLargeFragment.

@Test
public void testCodingFragmentBufferingLargeFragment() 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 CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.append("header");
    outbuf.writeLine(chbuffer);
    final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 2);
    Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
    Mockito.verify(channel, Mockito.times(2)).write(ArgumentMatchers.any());
    Mockito.verify(outbuf, Mockito.never()).write(ArgumentMatchers.<ByteBuffer>any());
    Mockito.verify(outbuf, Mockito.times(1)).flush(channel);
    Assertions.assertEquals(13, metrics.getBytesTransferred());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("header\r\nstuff", 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) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) 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