Search in sources :

Example 1 with WritableByteChannelMock

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

the class TestFrameInOutBuffers method testReadWriteFrame.

@Test
public void testReadWriteFrame() throws Exception {
    final WritableByteChannelMock writableChannel = new WritableByteChannelMock(1024);
    final FrameOutputBuffer outbuffer = new FrameOutputBuffer(16 * 1024);
    final RawFrame frame = new RawFrame(FrameType.DATA.getValue(), 0, 1, ByteBuffer.wrap(new byte[] { 1, 2, 3, 4, 5 }));
    outbuffer.write(frame, writableChannel);
    final FrameInputBuffer inBuffer = new FrameInputBuffer(16 * 1024);
    final byte[] bytes = writableChannel.toByteArray();
    Assertions.assertEquals(FrameConsts.HEAD_LEN + 5, bytes.length);
    Assertions.assertEquals(1, outbuffer.getMetrics().getFramesTransferred());
    Assertions.assertEquals(bytes.length, outbuffer.getMetrics().getBytesTransferred());
    final ReadableByteChannelMock readableChannel = new ReadableByteChannelMock(bytes);
    final RawFrame frame2 = inBuffer.read(readableChannel);
    Assertions.assertEquals(FrameType.DATA.getValue(), frame2.getType());
    Assertions.assertEquals(0, frame2.getFlags());
    Assertions.assertEquals(1L, frame2.getStreamId());
    final ByteBuffer payload2 = frame2.getPayloadContent();
    Assertions.assertNotNull(payload2);
    Assertions.assertEquals(5, payload2.remaining());
    Assertions.assertEquals(1, payload2.get());
    Assertions.assertEquals(2, payload2.get());
    Assertions.assertEquals(3, payload2.get());
    Assertions.assertEquals(4, payload2.get());
    Assertions.assertEquals(5, payload2.get());
    Assertions.assertEquals(-1, readableChannel.read(ByteBuffer.allocate(1024)));
    Assertions.assertEquals(1, inBuffer.getMetrics().getFramesTransferred());
    Assertions.assertEquals(bytes.length, inBuffer.getMetrics().getBytesTransferred());
}
Also used : WritableByteChannelMock(org.apache.hc.core5.http2.WritableByteChannelMock) RawFrame(org.apache.hc.core5.http2.frame.RawFrame) ReadableByteChannelMock(org.apache.hc.core5.http2.ReadableByteChannelMock) ByteBuffer(java.nio.ByteBuffer) Test(org.junit.jupiter.api.Test)

Example 2 with WritableByteChannelMock

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

the class TestReactiveDataProducer method testStreamThatEndsNormally.

@Test
public void testStreamThatEndsNormally() throws Exception {
    final Flowable<ByteBuffer> publisher = Flowable.just(ByteBuffer.wrap(new byte[] { '1', '2', '3' }), ByteBuffer.wrap(new byte[] { '4', '5', '6' }));
    final ReactiveDataProducer producer = new ReactiveDataProducer(publisher);
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    producer.produce(streamChannel);
    Assertions.assertTrue(byteChannel.isOpen());
    Assertions.assertEquals("123456", byteChannel.dump(StandardCharsets.US_ASCII));
    producer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen());
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
}
Also used : ByteBuffer(java.nio.ByteBuffer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 3 with WritableByteChannelMock

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

the class TestReactiveDataProducer method testStreamThatEndsWithError.

@Test
public void testStreamThatEndsWithError() throws Exception {
    final Flowable<ByteBuffer> publisher = Flowable.concatArray(Flowable.just(ByteBuffer.wrap(new byte[] { '1' }), ByteBuffer.wrap(new byte[] { '2' }), ByteBuffer.wrap(new byte[] { '3' }), ByteBuffer.wrap(new byte[] { '4' }), ByteBuffer.wrap(new byte[] { '5' }), ByteBuffer.wrap(new byte[] { '6' })), Flowable.error(new RuntimeException()));
    final ReactiveDataProducer producer = new ReactiveDataProducer(publisher);
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    producer.produce(streamChannel);
    Assertions.assertEquals("12345", byteChannel.dump(StandardCharsets.US_ASCII));
    final HttpStreamResetException exception = Assertions.assertThrows(HttpStreamResetException.class, () -> producer.produce(streamChannel));
    Assertions.assertTrue(exception.getCause() instanceof RuntimeException, "Expected published exception to be rethrown");
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
}
Also used : HttpStreamResetException(org.apache.hc.core5.http.HttpStreamResetException) ByteBuffer(java.nio.ByteBuffer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 4 with WritableByteChannelMock

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

the class TestReactiveEntityProducer method testStreamThatEndsWithError.

@Test
public void testStreamThatEndsWithError() throws Exception {
    final Flowable<ByteBuffer> publisher = Flowable.concatArray(Flowable.just(ByteBuffer.wrap(new byte[] { '1' }), ByteBuffer.wrap(new byte[] { '2' }), ByteBuffer.wrap(new byte[] { '3' }), ByteBuffer.wrap(new byte[] { '4' }), ByteBuffer.wrap(new byte[] { '5' }), ByteBuffer.wrap(new byte[] { '6' })), Flowable.error(new RuntimeException()));
    final ReactiveEntityProducer entityProducer = new ReactiveEntityProducer(publisher, CONTENT_LENGTH, CONTENT_TYPE, GZIP_CONTENT_ENCODING);
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    entityProducer.produce(streamChannel);
    Assertions.assertEquals("12345", byteChannel.dump(StandardCharsets.US_ASCII));
    final HttpStreamResetException exception = Assertions.assertThrows(HttpStreamResetException.class, () -> entityProducer.produce(streamChannel));
    Assertions.assertTrue(exception.getCause() instanceof RuntimeException, "Expected published exception to be rethrown");
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
    entityProducer.failed(exception);
    Assertions.assertEquals(1, entityProducer.available());
    Assertions.assertTrue(byteChannel.isOpen());
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
    Assertions.assertFalse(entityProducer.isChunked());
    Assertions.assertEquals(GZIP_CONTENT_ENCODING, entityProducer.getContentEncoding());
    Assertions.assertNull(entityProducer.getTrailerNames());
    Assertions.assertEquals(CONTENT_LENGTH, entityProducer.getContentLength());
    Assertions.assertEquals(CONTENT_TYPE.toString(), entityProducer.getContentType());
    Assertions.assertFalse(entityProducer.isRepeatable());
    Assertions.assertEquals(1, entityProducer.available());
    entityProducer.releaseResources();
}
Also used : HttpStreamResetException(org.apache.hc.core5.http.HttpStreamResetException) ByteBuffer(java.nio.ByteBuffer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 5 with WritableByteChannelMock

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

the class TestAbstractBinAsyncEntityProducer method testProduceDataWithBuffering2.

@Test
public void testProduceDataWithBuffering2() throws Exception {
    final AsyncEntityProducer producer = new ChunkByteAsyncEntityProducer(5, ContentType.TEXT_PLAIN, new byte[] { '1' }, new byte[] { '2' }, new byte[] { '3' }, new byte[] { '4', '5' }, new byte[] { '6' }, new byte[] { '7', '8' }, new byte[] { '9', '0' });
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final DataStreamChannel streamChannel = new BasicDataStreamChannel(byteChannel);
    producer.produce(streamChannel);
    Assertions.assertTrue(byteChannel.isOpen());
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
    producer.produce(streamChannel);
    Assertions.assertTrue(byteChannel.isOpen());
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
    producer.produce(streamChannel);
    Assertions.assertTrue(byteChannel.isOpen());
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
    producer.produce(streamChannel);
    Assertions.assertTrue(byteChannel.isOpen());
    Assertions.assertEquals("12345", byteChannel.dump(StandardCharsets.US_ASCII));
    producer.produce(streamChannel);
    Assertions.assertTrue(byteChannel.isOpen());
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
    producer.produce(streamChannel);
    Assertions.assertTrue(byteChannel.isOpen());
    Assertions.assertEquals("", byteChannel.dump(StandardCharsets.US_ASCII));
    producer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen());
    Assertions.assertEquals("67890", byteChannel.dump(StandardCharsets.US_ASCII));
}
Also used : AsyncEntityProducer(org.apache.hc.core5.http.nio.AsyncEntityProducer) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) 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