Search in sources :

Example 16 with BasicDataStreamChannel

use of org.apache.hc.core5.http.nio.BasicDataStreamChannel in project httpcomponents-core by apache.

the class TestAbstractCharAsyncEntityProducer method testProduceDataNoBuffering.

@Test
public void testProduceDataNoBuffering() throws Exception {
    final AsyncEntityProducer producer = new ChunkCharAsyncEntityProducer(256, 0, ContentType.TEXT_PLAIN, "this", "this and that");
    Assertions.assertEquals(-1, 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.assertTrue(byteChannel.isOpen());
    Assertions.assertEquals("this", byteChannel.dump(StandardCharsets.US_ASCII));
    producer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen());
    Assertions.assertEquals("this and that", 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)

Example 17 with BasicDataStreamChannel

use of org.apache.hc.core5.http.nio.BasicDataStreamChannel 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));
}
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)

Example 18 with BasicDataStreamChannel

use of org.apache.hc.core5.http.nio.BasicDataStreamChannel 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));
}
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)

Example 19 with BasicDataStreamChannel

use of org.apache.hc.core5.http.nio.BasicDataStreamChannel in project httpcomponents-core by apache.

the class TestReactiveEntityProducer 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 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.assertTrue(byteChannel.isOpen(), "Should be open");
    Assertions.assertEquals("123456", byteChannel.dump(StandardCharsets.US_ASCII));
    entityProducer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen(), "Should be closed");
    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 : ByteBuffer(java.nio.ByteBuffer) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)19 DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)18 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)15 BasicDataStreamChannel (org.apache.hc.core5.http.nio.BasicDataStreamChannel)15 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)14 ByteBuffer (java.nio.ByteBuffer)4 Path (java.nio.file.Path)2 HttpStreamResetException (org.apache.hc.core5.http.HttpStreamResetException)2 Header (org.apache.hc.core5.http.Header)1