Search in sources :

Example 26 with SessionOutputBuffer

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

the class TestChunkEncoder method testTrailers.

@Test
public void testTrailers() throws IOException {
    final WritableByteChannelMock channel = new WritableByteChannelMock(64);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics, 0);
    encoder.write(CodecTestUtils.wrap("1"));
    encoder.write(CodecTestUtils.wrap("23"));
    encoder.complete(Arrays.asList(new BasicHeader("E", ""), new BasicHeader("Y", "Z")));
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertTrue(encoder.isCompleted());
    Assertions.assertEquals("1\r\n1\r\n2\r\n23\r\n0\r\nE: \r\nY: Z\r\n\r\n", s);
    Assertions.assertEquals("[chunk-coded; 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) BasicHeader(org.apache.hc.core5.http.message.BasicHeader) Test(org.junit.jupiter.api.Test)

Example 27 with SessionOutputBuffer

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

the class TestChunkEncoder method testLimitedChannel.

// See HTTPCORE-239
@Test
public void testLimitedChannel() throws Exception {
    final WritableByteChannelMock channel = new WritableByteChannelMock(16, 16);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
    // fill up the channel
    channel.write(CodecTestUtils.wrap("0123456789ABCDEF"));
    // fill up the out buffer
    outbuf.write(CodecTestUtils.wrap("0123456789ABCDEF"));
    final ByteBuffer src = CodecTestUtils.wrap("0123456789ABCDEF");
    Assertions.assertEquals(0, encoder.write(src));
    Assertions.assertEquals(0, encoder.write(src));
    Assertions.assertEquals(0, encoder.write(src));
    // should not be able to copy any bytes, until we flush the channel and buffer
    channel.reset();
    outbuf.flush(channel);
    channel.reset();
    Assertions.assertEquals(10, encoder.write(src));
    channel.flush();
    Assertions.assertEquals(6, encoder.write(src));
    channel.flush();
    Assertions.assertEquals(0, encoder.write(src));
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("4\r\n0123\r\n4\r\n4567\r\n2\r\n89\r\n4\r\nABCD\r\n2\r\nEF\r\n", 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 28 with SessionOutputBuffer

use of org.apache.hc.core5.http.io.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 outbuffer = new SessionOutputBufferImpl(16, encoder);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
    chbuffer.append(s);
    Assertions.assertThrows(CharacterCodingException.class, () -> outbuffer.writeLine(chbuffer, outputStream));
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) CharArrayBuffer(org.apache.hc.core5.util.CharArrayBuffer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CharsetEncoder(java.nio.charset.CharsetEncoder) Test(org.junit.jupiter.api.Test)

Example 29 with SessionOutputBuffer

use of org.apache.hc.core5.http.io.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 outbuffer = new SessionOutputBufferImpl(16, StandardCharsets.UTF_8.newEncoder());
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final CharArrayBuffer chbuffer = new CharArrayBuffer(16);
    for (int i = 0; i < 10; i++) {
        chbuffer.clear();
        chbuffer.append(s1);
        outbuffer.writeLine(chbuffer, outputStream);
        chbuffer.clear();
        chbuffer.append(s2);
        outbuffer.writeLine(chbuffer, outputStream);
        chbuffer.clear();
        chbuffer.append(s3);
        outbuffer.writeLine(chbuffer, outputStream);
    }
    outbuffer.flush(outputStream);
    final long bytesWritten = outbuffer.getMetrics().getBytesTransferred();
    final long expected = ((s1.getBytes(StandardCharsets.UTF_8).length + 2) + (s2.getBytes(StandardCharsets.UTF_8).length + 2) + (s3.getBytes(StandardCharsets.UTF_8).length + 2)) * 10;
    Assertions.assertEquals(expected, bytesWritten);
    final SessionInputBuffer inBuffer = new SessionInputBufferImpl(16, StandardCharsets.UTF_8.newDecoder());
    final ByteArrayInputStream inputStream = new ByteArrayInputStream(outputStream.toByteArray());
    for (int i = 0; i < 10; i++) {
        chbuffer.clear();
        inBuffer.readLine(chbuffer, inputStream);
        Assertions.assertEquals(s1, chbuffer.toString());
        chbuffer.clear();
        inBuffer.readLine(chbuffer, inputStream);
        Assertions.assertEquals(s2, chbuffer.toString());
        chbuffer.clear();
        inBuffer.readLine(chbuffer, inputStream);
        Assertions.assertEquals(s3, chbuffer.toString());
    }
    chbuffer.clear();
    Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
    chbuffer.clear();
    Assertions.assertEquals(-1, inBuffer.readLine(chbuffer, inputStream));
    final long bytesRead = inBuffer.getMetrics().getBytesTransferred();
    Assertions.assertEquals(expected, bytesRead);
}
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 30 with SessionOutputBuffer

use of org.apache.hc.core5.http.io.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 outbuffer = new SessionOutputBufferImpl(16, encoder);
    final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    final CharArrayBuffer chbuffer = new CharArrayBuffer(32);
    chbuffer.append(s);
    outbuffer.writeLine(chbuffer, outputStream);
    outbuffer.flush(outputStream);
    final String result = new String(outputStream.toByteArray(), StandardCharsets.ISO_8859_1);
    Assertions.assertEquals("This text contains a circumflex ? !!!\r\n", result);
}
Also used : SessionOutputBuffer(org.apache.hc.core5.http.io.SessionOutputBuffer) 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