use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestLengthDelimitedEncoder method testCodingFragmentBuffering.
@Test
public void testCodingFragmentBuffering() 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, 32);
Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
Mockito.verify(channel, Mockito.never()).write(ArgumentMatchers.any());
Mockito.verify(outbuf, Mockito.times(1)).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("header\r\nstuff", s);
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestChunkEncoder 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 ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
encoder.write(CodecTestUtils.wrap("12345"));
encoder.write(CodecTestUtils.wrap("678"));
encoder.write(CodecTestUtils.wrap("90"));
final ByteBuffer empty = ByteBuffer.allocate(100);
empty.flip();
encoder.write(empty);
encoder.write(null);
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);
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestChunkEncoder method testChunkNoExceed.
@Test
public void testChunkNoExceed() throws Exception {
final WritableByteChannelMock channel = new WritableByteChannelMock(64);
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 16);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics);
encoder.write(CodecTestUtils.wrap("1234"));
encoder.complete();
outbuf.flush(channel);
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertTrue(encoder.isCompleted());
Assertions.assertEquals("4\r\n1234\r\n0\r\n\r\n", s);
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestChunkEncoder method testBufferFragments.
@Test
public void testBufferFragments() throws Exception {
final WritableByteChannelMock channel = Mockito.spy(new WritableByteChannelMock(1024));
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 1024);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final ChunkEncoder encoder = new ChunkEncoder(channel, outbuf, metrics, 1024);
Assertions.assertEquals(16, encoder.write(CodecTestUtils.wrap("0123456789ABCDEF")));
Assertions.assertEquals(16, encoder.write(CodecTestUtils.wrap("0123456789ABCDEF")));
Assertions.assertEquals(16, encoder.write(CodecTestUtils.wrap("0123456789ABCDEF")));
Mockito.verify(channel, Mockito.never()).write(ArgumentMatchers.any());
outbuf.flush(channel);
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertEquals("10\r\n0123456789ABCDEF\r\n10\r\n0123456789ABCDEF\r\n" + "10\r\n0123456789ABCDEF\r\n", s);
}
use of org.apache.hc.core5.http.nio.SessionOutputBuffer in project httpcomponents-core by apache.
the class TestChunkEncoder method testInvalidConstructor.
@Test
public void testInvalidConstructor() {
final WritableByteChannelMock channel = new WritableByteChannelMock(64);
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
Assertions.assertThrows(NullPointerException.class, () -> new ChunkEncoder(null, null, null));
Assertions.assertThrows(NullPointerException.class, () -> new ChunkEncoder(channel, null, null));
Assertions.assertThrows(NullPointerException.class, () -> new ChunkEncoder(channel, outbuf, null));
}
Aggregations