Search in sources :

Example 26 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestChunkEncoder method testChunkExceed.

@Test
public void testChunkExceed() throws Exception {
    final WritableByteChannelMock channel = new WritableByteChannelMock(64);
    final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(16, 16);
    final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
    final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
    final ByteBuffer src = CodecTestUtils.wrap("0123456789ABCDEF");
    Assertions.assertEquals(16, encoder.write(src));
    Assertions.assertEquals(0, src.remaining());
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertEquals("4\r\n0123\r\n4\r\n4567\r\n4\r\n89AB\r\n4\r\nCDEF\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 27 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestChunkEncoder method testBasicCoding.

@Test
public void testBasicCoding() throws Exception {
    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);
    encoder.write(CodecTestUtils.wrap("12345"));
    encoder.write(CodecTestUtils.wrap("678"));
    encoder.write(CodecTestUtils.wrap("90"));
    encoder.complete();
    outbuf.flush(channel);
    final String s = channel.dump(StandardCharsets.US_ASCII);
    Assertions.assertTrue(encoder.isCompleted());
    Assertions.assertEquals("5\r\n12345\r\n3\r\n678\r\n2\r\n90\r\n0\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) Test(org.junit.jupiter.api.Test)

Example 28 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.

the class TestChunkEncoder method testCodingCompleted.

@Test
public void testCodingCompleted() throws Exception {
    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);
    encoder.write(CodecTestUtils.wrap("12345"));
    encoder.write(CodecTestUtils.wrap("678"));
    encoder.write(CodecTestUtils.wrap("90"));
    encoder.complete();
    Assertions.assertThrows(IllegalStateException.class, () -> encoder.write(CodecTestUtils.wrap("more stuff")));
    Assertions.assertThrows(IllegalStateException.class, () -> encoder.complete());
}
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 29 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock 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 30 with WritableByteChannelMock

use of org.apache.hc.core5.http2.WritableByteChannelMock 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)

Aggregations

Test (org.junit.jupiter.api.Test)79 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)70 SessionOutputBuffer (org.apache.hc.core5.http.nio.SessionOutputBuffer)52 BasicHttpTransportMetrics (org.apache.hc.core5.http.impl.BasicHttpTransportMetrics)50 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)20 BasicDataStreamChannel (org.apache.hc.core5.http.nio.BasicDataStreamChannel)15 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)14 RandomAccessFile (java.io.RandomAccessFile)11 FileChannel (java.nio.channels.FileChannel)11 ByteBuffer (java.nio.ByteBuffer)10 CharArrayBuffer (org.apache.hc.core5.util.CharArrayBuffer)10 WritableByteChannelMock (org.apache.hc.core5.http2.WritableByteChannelMock)5 RawFrame (org.apache.hc.core5.http2.frame.RawFrame)5 Charset (java.nio.charset.Charset)3 Path (java.nio.file.Path)2 HttpStreamResetException (org.apache.hc.core5.http.HttpStreamResetException)2 ExecutorService (java.util.concurrent.ExecutorService)1 Header (org.apache.hc.core5.http.Header)1 BasicHeader (org.apache.hc.core5.http.message.BasicHeader)1 ReadableByteChannelMock (org.apache.hc.core5.http2.ReadableByteChannelMock)1