Search in sources :

Example 6 with BasicDataStreamChannel

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

the class TestAbstractBinAsyncEntityProducer method testProduceDataNoBuffering.

@Test
public void testProduceDataNoBuffering() throws Exception {
    final AsyncEntityProducer producer = new ChunkByteAsyncEntityProducer(0, ContentType.TEXT_PLAIN, new byte[] { '1', '2', '3' }, new byte[] { '4', '5', '6' });
    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("123", byteChannel.dump(StandardCharsets.US_ASCII));
    producer.produce(streamChannel);
    Assertions.assertFalse(byteChannel.isOpen());
    Assertions.assertEquals("456", 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 7 with BasicDataStreamChannel

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

the class TestAbstractCharAsyncEntityProducer method testProduceDataWithBuffering.

@Test
public void testProduceDataWithBuffering() throws Exception {
    final AsyncEntityProducer producer = new ChunkCharAsyncEntityProducer(256, 5, ContentType.TEXT_PLAIN, "this", " and that", "all", " sorts of stuff");
    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("this and that", 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("all sorts of stuff", 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 8 with BasicDataStreamChannel

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

the class TestBasicAsyncEntityProducer method testTextContentRepeatable.

@Test
public void testTextContentRepeatable() 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());
    for (int i = 0; i < 3; i++) {
        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));
        producer.releaseResources();
    }
}
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 9 with BasicDataStreamChannel

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

the class TestDigestingEntityProducer method testProduceData.

@Test
public void testProduceData() throws Exception {
    final DigestingEntityProducer producer = new DigestingEntityProducer("MD5", new StringAsyncEntityProducer("12345", ContentType.TEXT_PLAIN));
    final WritableByteChannelMock byteChannel = new WritableByteChannelMock(1024);
    final BasicDataStreamChannel dataStreamChannel = new BasicDataStreamChannel(byteChannel);
    while (byteChannel.isOpen()) {
        producer.produce(dataStreamChannel);
    }
    Assertions.assertEquals("12345", byteChannel.dump(StandardCharsets.US_ASCII));
    final List<Header> trailers = dataStreamChannel.getTrailers();
    Assertions.assertNotNull(trailers);
    Assertions.assertEquals(2, trailers.size());
    Assertions.assertEquals("digest-algo", trailers.get(0).getName());
    Assertions.assertEquals("MD5", trailers.get(0).getValue());
    Assertions.assertEquals("digest", trailers.get(1).getName());
    Assertions.assertEquals("827ccb0eea8a706c4c34a16891f84e7b", trailers.get(1).getValue());
}
Also used : Header(org.apache.hc.core5.http.Header) WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 10 with BasicDataStreamChannel

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

the class TestFileAsyncEntityProducer method testTextContentRepeatable.

@Test
public void testTextContentRepeatable() 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());
    for (int i = 0; i < 3; i++) {
        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));
        producer.releaseResources();
    }
}
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) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) BasicDataStreamChannel(org.apache.hc.core5.http.nio.BasicDataStreamChannel) 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