Search in sources :

Example 6 with SessionOutputBuffer

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

the class TestLengthDelimitedEncoder 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 LengthDelimitedEncoder encoder = new LengthDelimitedEncoder(channel, outbuf, metrics, 100, 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)

Example 7 with SessionOutputBuffer

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

the class TestLengthDelimitedEncoder method testInvalidConstructor.

@Test
public void testInvalidConstructor() {
    final WritableByteChannelMock channel = new WritableByteChannelMock(64);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedEncoder(null, null, null, 10));
    Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedEncoder(channel, null, null, 10));
    Assertions.assertThrows(NullPointerException.class, () -> new LengthDelimitedEncoder(channel, outbuf, null, 10));
    Assertions.assertThrows(IllegalArgumentException.class, () -> new LengthDelimitedEncoder(channel, outbuf, metrics, -10));
}
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 8 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testUnmappableInputActionIgnore.

@Test
public void testUnmappableInputActionIgnore() 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.IGNORE);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, encoder);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final WritableByteChannel channel = newChannel(baos);
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.append(s);
    outbuf.writeLine(chbuffer);
    outbuf.flush(channel);
    final String result = new String(baos.toByteArray(), StandardCharsets.US_ASCII);
    Assertions.assertEquals("This text contains a circumflex !!!\r\n", result);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CharsetEncoder(java.nio.charset.CharsetEncoder) Test(org.junit.jupiter.api.Test)

Example 9 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testComplexReadWriteLine.

@Test
public void testComplexReadWriteLine() throws Exception {
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16);
    outbuf.write(ByteBuffer.wrap(new byte[] { 'a', '\n' }));
    outbuf.write(ByteBuffer.wrap(new byte[] { '\r', '\n' }));
    outbuf.write(ByteBuffer.wrap(new byte[] { '\r', '\r', '\n' }));
    outbuf.write(ByteBuffer.wrap(new byte[] { '\n' }));
    final StringBuilder buffer = new StringBuilder();
    for (int i = 0; i < 14; i++) {
        buffer.append("a");
    }
    final String s1 = buffer.toString();
    buffer.append("\r\n");
    outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes(StandardCharsets.US_ASCII)));
    buffer.setLength(0);
    for (int i = 0; i < 15; i++) {
        buffer.append("a");
    }
    final String s2 = buffer.toString();
    buffer.append("\r\n");
    outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes(StandardCharsets.US_ASCII)));
    buffer.setLength(0);
    for (int i = 0; i < 16; i++) {
        buffer.append("a");
    }
    final String s3 = buffer.toString();
    buffer.append("\r\n");
    outbuf.write(ByteBuffer.wrap(buffer.toString().getBytes(StandardCharsets.US_ASCII)));
    outbuf.write(ByteBuffer.wrap(new byte[] { 'a' }));
    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);
    final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals("a", chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals("", chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals("\r", chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals("", chbuffer.toString());
    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());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertEquals("a", chbuffer.toString());
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    Assertions.assertFalse(inbuf.readLine(chbuffer, true));
    chbuffer.clear();
    inbuf.readLine(chbuffer, true);
    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) WritableByteChannel(java.nio.channels.WritableByteChannel) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.junit.jupiter.api.Test)

Example 10 with SessionOutputBuffer

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

the class TestSessionInOutBuffers method testUnmappableInputActionReplace.

@Test
public void testUnmappableInputActionReplace() 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.REPLACE);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16, encoder);
    final ByteArrayOutputStream baos = new ByteArrayOutputStream();
    final WritableByteChannel channel = newChannel(baos);
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    chbuffer.append(s);
    outbuf.writeLine(chbuffer);
    outbuf.flush(channel);
    final String result = new String(baos.toByteArray(), StandardCharsets.US_ASCII);
    Assertions.assertEquals("This text contains a circumflex ? !!!\r\n", result);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.nio.SessionOutputBuffer) WritableByteChannel(java.nio.channels.WritableByteChannel) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CharsetEncoder(java.nio.charset.CharsetEncoder) 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