use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityEncoder 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 IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
encoder.complete();
Assertions.assertTrue(encoder.isCompleted());
Assertions.assertEquals(5, metrics.getBytesTransferred());
outbuf.flush(channel);
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertEquals("stuff", s);
Assertions.assertEquals("[identity; completed: true]", encoder.toString());
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityEncoder method testInvalidConstructor.
@Test
public void testInvalidConstructor() {
final WritableByteChannelMock channel = new WritableByteChannelMock(64);
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
Assertions.assertThrows(NullPointerException.class, () -> new IdentityEncoder(null, null, null));
Assertions.assertThrows(NullPointerException.class, () -> new IdentityEncoder(channel, null, null));
Assertions.assertThrows(NullPointerException.class, () -> new IdentityEncoder(channel, outbuf, null));
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityEncoder method testCodingEmptyFile.
@Test
public void testCodingEmptyFile() throws Exception {
final WritableByteChannelMock channel = new WritableByteChannelMock(64);
final SessionOutputBuffer outbuf = new SessionOutputBufferImpl(1024, 128);
final BasicHttpTransportMetrics metrics = new BasicHttpTransportMetrics();
final IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics);
encoder.write(CodecTestUtils.wrap("stuff;"));
// Create an empty file
createTempFile();
RandomAccessFile testfile = new RandomAccessFile(this.tmpfile, "rw");
testfile.close();
testfile = new RandomAccessFile(this.tmpfile, "rw");
try {
final FileChannel fchannel = testfile.getChannel();
encoder.transfer(fchannel, 0, 20);
encoder.write(CodecTestUtils.wrap("more stuff"));
} finally {
testfile.close();
}
final String s = channel.dump(StandardCharsets.US_ASCII);
Assertions.assertFalse(encoder.isCompleted());
Assertions.assertEquals("stuff;more stuff", s);
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestIdentityEncoder method testCodingFragmentBufferingMultipleFragments.
@Test
public void testCodingFragmentBufferingMultipleFragments() 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 IdentityEncoder encoder = new IdentityEncoder(channel, outbuf, metrics, 32);
Assertions.assertEquals(5, encoder.write(CodecTestUtils.wrap("stuff")));
Assertions.assertEquals(1, encoder.write(CodecTestUtils.wrap("-")));
Assertions.assertEquals(10, encoder.write(CodecTestUtils.wrap("more stuff")));
Mockito.verify(channel, Mockito.never()).write(ArgumentMatchers.any());
Mockito.verify(outbuf, Mockito.times(3)).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("stuff-more stuff", s);
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestFileAsyncEntityProducer method testTextContent.
@Test
public void testTextContent() throws Exception {
final AsyncEntityProducer producer = new FileEntityProducer(tempFile, ContentType.TEXT_PLAIN);
Assertions.assertEquals(6, producer.getContentLength());
Assertions.assertEquals(ContentType.TEXT_PLAIN.toString(), producer.getContentType());
Assertions.assertNull(producer.getContentEncoding());
final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
producer.produce(streamChannel);
producer.produce(streamChannel);
Assertions.assertFalse(byteChannel.isOpen());
Assertions.assertEquals("abcdef", byteChannel.dump(StandardCharsets.US_ASCII));
}
Aggregations