Search in sources :

Example 61 with SessionOutputBuffer

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

the class TestLengthDelimitedEncoder method testCodingFragmentBufferingChannelSaturated2.

@Test
public void testCodingFragmentBufferingChannelSaturated2() throws Exception {
    final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(64, 8));
    final SessionOutputBuffer outbuf = Mockito.spy(new SessionOutputBufferImpl(1024, 128));
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 8);
    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("much more stuff")));
    Mockito.verify(channel, Mockito.times(3)).write(ArgumentMatchers.any());
    Mockito.verify(outbuf, Mockito.times(3)).write(ArgumentMatchers.<ByteBuffer>any());
    Mockito.verify(outbuf, Mockito.times(1)).flush(channel);
    Assertions.assertEquals(8, metrics.getBytesTransferred());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("stuff--m", s);
    Assertions.assertEquals(0, outbuf.length());
}
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 62 with SessionOutputBuffer

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

the class TestLengthDelimitedEncoder method testCodingEmptyBuffer.

@Test
public void testCodingEmptyBuffer() 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);
    encoder.write(CodecTestUtils.wrap("stuff;"));
    final ByteBuffer empty = ByteBuffer.allocate(100);
    empty.flip();
    encoder.write(empty);
    encoder.write(null);
    encoder.write(CodecTestUtils.wrap("more stuff"));
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertTrue(encoder.isCompleted());
    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) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 63 with SessionOutputBuffer

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

the class TestLengthDelimitedEncoder method testCodingBeyondContentLimitFromFile.

@Test
public void testCodingBeyondContentLimitFromFile() 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);
    createTempFile();
    RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
    try {
        testfile.write("stuff;".getBytes(StandardCharsets.US_ASCII));
        testfile.write("more stuff; and a lot 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("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 64 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testUnmappableInputActionReport.

@Test
public void testUnmappableInputActionReport() throws Exception {
    final String s = "This text contains a circumflex \u0302!!!";
    final CharsetEncoder encoder = StandardCharsets.ISO_8859_1.newEncoder();
    encoder.onMalformedInput(CodingErrorAction.IGNORE);
    encoder.onUnmappableCharacter(CodingErrorAction.REPORT);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, encoder);
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.append(s);
    Assertions.assertThrows(CharacterCodingException.class, () -> outbuf.writeLine(chbuffer));
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) CharsetEncoder(java.nio.charset.CharsetEncoder) Test(org.junit.jupiter.api.Test)

Example 65 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testWriteByteBuffer.

@Test
public void testWriteByteBuffer() throws Exception {
    final byte[] pattern = "0123456789ABCDEF0123456789ABCDEF".getBytes(StandardCharsets.US_ASCII);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(4096, 1024);
    final ReadableByteChannel src = newChannel(pattern);
    outbuf.write(src);
    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 : ReadableByteChannel(java.nio.channels.ReadableByteChannel) SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) 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