use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestBasicAsyncEntityProducer method testTextContent.
@Test
public void testTextContent() throws Exception {
final AsyncEntityProducer producer = new BasicAsyncEntityProducer("abc", ContentType.TEXT_PLAIN);
Assertions.assertEquals(3, 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);
Assertions.assertFalse(byteChannel.isOpen());
Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestBasicAsyncEntityProducer method testBinaryContent.
@Test
public void testBinaryContent() throws Exception {
final AsyncEntityProducer producer = new BasicAsyncEntityProducer(new byte[] { 'a', 'b', 'c' }, ContentType.DEFAULT_BINARY);
Assertions.assertEquals(3, producer.getContentLength());
Assertions.assertEquals(ContentType.DEFAULT_BINARY.toString(), producer.getContentType());
Assertions.assertNull(producer.getContentEncoding());
final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
producer.produce(streamChannel);
Assertions.assertFalse(byteChannel.isOpen());
Assertions.assertEquals("abc", byteChannel.dump(StandardCharsets.US_ASCII));
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestAbstractH2StreamMultiplexer method testInputOneFrame.
@Test
public void testInputOneFrame() throws Exception {
final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024);
final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
final byte[] data = new byte[FrameConsts.MIN_FRAME_SIZE];
for (int i = 0; i < FrameConsts.MIN_FRAME_SIZE; i++) {
data[i] = (byte) (i % 16);
}
final RawFrame frame = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(data));
outbuffer.write(frame, writableChannel);
final byte[] bytes = writableChannel.toByteArray();
final AbstractH2StreamMultiplexer streamMultiplexer = new H2StreamMultiplexerImpl(protocolIOSession, DefaultFrameFactory.INSTANCE, StreamIdGenerator.ODD, httpProcessor, CharCodingConfig.DEFAULT, H2Config.custom().setMaxFrameSize(FrameConsts.MIN_FRAME_SIZE).build(), h2StreamListener);
Assertions.assertThrows(H2ConnectionException.class, () -> streamMultiplexer.onInput(ByteBuffer.wrap(bytes)));
Mockito.verify(h2StreamListener).onFrameInput(Mockito.same(streamMultiplexer), Mockito.eq(1), Mockito.any());
Assertions.assertThrows(H2ConnectionException.class, () -> {
int pos = 0;
int remaining = bytes.length;
while (remaining > 0) {
final int chunk = Math.min(2048, remaining);
streamMultiplexer.onInput(ByteBuffer.wrap(bytes, pos, chunk));
pos += chunk;
remaining -= chunk;
}
Mockito.verify(h2StreamListener).onFrameInput(Mockito.same(streamMultiplexer), Mockito.eq(1), Mockito.any());
});
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestAbstractH2StreamMultiplexer method testInputMultipleFrames.
@Test
public void testInputMultipleFrames() throws Exception {
final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024);
final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
final byte[] data = new byte[FrameConsts.MIN_FRAME_SIZE];
for (int i = 0; i < FrameConsts.MIN_FRAME_SIZE; i++) {
data[i] = (byte) (i % 16);
}
final RawFrame frame1 = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(data));
outbuffer.write(frame1, writableChannel);
final RawFrame frame2 = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(data));
outbuffer.write(frame2, writableChannel);
final byte[] bytes = writableChannel.toByteArray();
final AbstractH2StreamMultiplexer streamMultiplexer = new H2StreamMultiplexerImpl(protocolIOSession, DefaultFrameFactory.INSTANCE, StreamIdGenerator.ODD, httpProcessor, CharCodingConfig.DEFAULT, H2Config.custom().setMaxFrameSize(FrameConsts.MIN_FRAME_SIZE).build(), h2StreamListener);
Assertions.assertThrows(H2ConnectionException.class, () -> streamMultiplexer.onInput(ByteBuffer.wrap(bytes)));
Mockito.verify(h2StreamListener).onFrameInput(Mockito.same(streamMultiplexer), Mockito.eq(1), Mockito.any());
Assertions.assertThrows(H2ConnectionException.class, () -> {
int pos = 0;
int remaining = bytes.length;
while (remaining > 0) {
final int chunk = Math.min(4096, remaining);
streamMultiplexer.onInput(ByteBuffer.wrap(bytes, pos, chunk));
pos += chunk;
remaining -= chunk;
}
Mockito.verify(h2StreamListener).onFrameInput(Mockito.same(streamMultiplexer), Mockito.eq(1), Mockito.any());
});
}
use of org.apache.hc.core5.http2.WritableByteChannelMock in project httpcomponents-core by apache.
the class TestFrameInOutBuffers method testPartialFrameWrite.
@Test
public void testPartialFrameWrite() throws Exception {
final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024, FrameConsts.HEAD_LEN + 10);
final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
final RawFrame frame = new RawFrame(FrameType.DATA.getValue(), FrameFlag.END_STREAM.getValue(), 5, ByteBuffer.wrap(new byte[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }));
outbuffer.write(frame, writableChannel);
Assertions.assertArrayEquals(new byte[] { 0, 0, 16, 0, 1, 0, 0, 0, 5, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 }, writableChannel.toByteArray());
Assertions.assertFalse(outbuffer.isEmpty());
outbuffer.flush(writableChannel);
Assertions.assertArrayEquals(new byte[] { 0, 0, 16, 0, 1, 0, 0, 0, 5, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57 }, writableChannel.toByteArray());
writableChannel.flush();
outbuffer.flush(writableChannel);
Assertions.assertArrayEquals(new byte[] { 0, 0, 16, 0, 1, 0, 0, 0, 5, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 97, 98, 99, 100, 101, 102 }, writableChannel.toByteArray());
}
Aggregations