Search in sources :

Example 11 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testWriteFromChannel.

@Test
public void testWriteFromChannel() throws Exception {
    final byte[] pattern = "0123456789ABCDEF0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(4096, 1024);
    outbuf.write(ByteBuffer.wrap(pattern, 0, 16));
    outbuf.write(ByteBuffer.wrap(pattern, 16, 10));
    outbuf.write(ByteBuffer.wrap(pattern, 26, 6));
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    final WritableByteChannel channel = newChannel(outStream);
    while (outbuf.flush(channel) > 0) {
    }
    Assertions.assertEquals(ByteBuffer.wrap(pattern), ByteBuffer.wrap(outStream.toByteArray()));
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 12 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testBasicReadWriteLine.

@Test
public void testBasicReadWriteLine() throws Exception {
    final String[] teststrs = new String[5];
    teststrs[0] = "Hello";
    teststrs[1] = "This string should be much longer than the size of the line buffer " + "which is only 16 bytes for this test";
    final StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < 15; i++) {
        buffer.append("123456789 ");
    }
    buffer.append("and stuff like that");
    teststrs[2] = buffer.toString();
    teststrs[3] = "";
    teststrs[4] = "And goodbye";
    final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16);
    for (final String teststr : teststrs) {
        chbuffer.clear();
        chbuffer.append(teststr);
        outbuf.writeLine(chbuffer);
    }
    // this write operation should have no effect
    outbuf.writeLine(null);
    final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
    final WritableByteChannel outChannel = newChannel(outStream);
    outbuf.flush(outChannel);
    final ReadableByteChannel channel = newChannel(outStream.toByteArray());
    final SessionInputBuffer inbuf = new SessionInputBufferImpl(1024, 16, 0);
    inbuf.fill(channel);
    for (final String teststr : teststrs) {
        chbuffer.clear();
        inbuf.readLine(chbuffer, true);
        Assertions.assertEquals(teststr, chbuffer.toString());
    }
    chbuffer.clear();
    Assertions.assertFalse(inbuf.readLine(chbuffer, true));
    chbuffer.clear();
    Assertions.assertFalse(inbuf.readLine(chbuffer, true));
}
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 13 with SessionOutputBuffer

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

the class TestIdentityEncoder method testCodingNoFragmentBuffering.

@Test
public void testCodingNoFragmentBuffering() 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, 0);
    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)

Example 14 with SessionOutputBuffer

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

the class TestLengthDelimitedEncoder method testCodingFragmentBufferingMultipleFragmentsBeyondContentLimit.

@Test
public void testCodingFragmentBufferingMultipleFragmentsBeyondContentLimit() 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 LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 16, 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; and a lot 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 15 with SessionOutputBuffer

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

the class TestLengthDelimitedEncoder method testCodingFromFileFlushBuffer.

@Test
public void testCodingFromFileFlushBuffer() 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, 16);
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.append("header");
    outbuf.writeLine(chbuffer);
    createTempFile();
    RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
    try {
        testfile.write("stuff;".getBytes(StandardCharsets.US_ASCII));
        testfile.write("more stuff".getBytes(StandardCharsets.US_ASCII));
    } finally {
        testfile.close();
    }
    testfile = new RandomAccessFile(this.tmpfile, "rw");
    try {
        final FileChannel fchannel = testfile.getChannel();
        encoder.transfer(fchannel, 0, 20);
    } finally {
        testfile.close();
    }
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertTrue(encoder.isCompleted());
    Assertions.assertEquals("header\r\nstuff;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) 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