Search in sources :

Example 16 with DataStreamChannel

use of org.apache.hc.core5.http.nio.DataStreamChannel 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 17 with DataStreamChannel

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

Example 18 with DataStreamChannel

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

the class TestSharedOutputBuffer method testMultithreadingWriteStream.

@Test
public void testMultithreadingWriteStream() throws Exception {
    final Charset charset = StandardCharsets.US_ASCII;
    final SharedOutputBuffer outputBuffer = new SharedOutputBuffer(20);
    final WritableByteChannelMock channel = new WritableByteChannelMock(1024);
    final DataStreamChannelMock dataStreamChannel = new DataStreamChannelMock(channel);
    final ExecutorService executorService = Executors.newFixedThreadPool(2);
    final Future<Boolean> task1 = executorService.submit(() -> {
        final byte[] tmp = "1234567890".getBytes(charset);
        outputBuffer.write(tmp, 0, tmp.length);
        outputBuffer.write(tmp, 0, tmp.length);
        outputBuffer.write('1');
        outputBuffer.write('2');
        outputBuffer.write(tmp, 0, tmp.length);
        outputBuffer.write(tmp, 0, tmp.length);
        outputBuffer.write(tmp, 0, tmp.length);
        outputBuffer.writeCompleted();
        outputBuffer.writeCompleted();
        return Boolean.TRUE;
    });
    final Future<Boolean> task2 = executorService.submit(() -> {
        for (; ; ) {
            outputBuffer.flush(dataStreamChannel);
            if (outputBuffer.isEndStream()) {
                break;
            }
            if (!outputBuffer.hasData()) {
                dataStreamChannel.awaitOutputRequest();
            }
        }
        return Boolean.TRUE;
    });
    Assertions.assertEquals(Boolean.TRUE, task1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
    Assertions.assertEquals(Boolean.TRUE, task2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
    Assertions.assertEquals("1234567890123456789012123456789012345678901234567890", new String(channel.toByteArray(), charset));
}
Also used : WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) ExecutorService(java.util.concurrent.ExecutorService) Charset(java.nio.charset.Charset) Test(org.junit.jupiter.api.Test)

Example 19 with DataStreamChannel

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

the class TestSharedOutputBuffer method testBasis.

@Test
public void testBasis() throws Exception {
    final Charset charset = StandardCharsets.US_ASCII;
    final SharedOutputBuffer outputBuffer = new SharedOutputBuffer(30);
    final WritableByteChannelMock channel = new WritableByteChannelMock(1024);
    final DataStreamChannel dataStreamChannel = Mockito.spy(new DataStreamChannelMock(channel));
    outputBuffer.flush(dataStreamChannel);
    Mockito.verifyNoInteractions(dataStreamChannel);
    Assertions.assertEquals(0, outputBuffer.length());
    Assertions.assertEquals(30, outputBuffer.capacity());
    final byte[] tmp = "1234567890".getBytes(charset);
    outputBuffer.write(tmp, 0, tmp.length);
    outputBuffer.write(tmp, 0, tmp.length);
    outputBuffer.write('1');
    outputBuffer.write('2');
    Assertions.assertEquals(22, outputBuffer.length());
    Assertions.assertEquals(8, outputBuffer.capacity());
    Mockito.verifyNoInteractions(dataStreamChannel);
}
Also used : WritableByteChannelMock(org.apache.hc.core5.http.WritableByteChannelMock) Charset(java.nio.charset.Charset) DataStreamChannel(org.apache.hc.core5.http.nio.DataStreamChannel) Test(org.junit.jupiter.api.Test)

Example 20 with DataStreamChannel

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

the class TestPathAsyncEntityProducer method testTextContent.

@Test
public void testTextContent() throws Exception {
    final Path tempPath = tempFile.toPath();
    final AsyncEntityProducer producer = new PathEntityProducer(tempPath, ContentType.TEXT_PLAIN, StandardOpenOption.READ);
    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));
}
Also used : Path(java.nio.file.Path) 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

DataStreamChannel (org.apache.hc.core5.http.nio.DataStreamChannel)37 Test (org.junit.jupiter.api.Test)23 ByteBuffer (java.nio.ByteBuffer)18 WritableByteChannelMock (org.apache.hc.core5.http.WritableByteChannelMock)18 AsyncEntityProducer (org.apache.hc.core5.http.nio.AsyncEntityProducer)18 IOException (java.io.IOException)15 HttpException (org.apache.hc.core5.http.HttpException)15 BasicDataStreamChannel (org.apache.hc.core5.http.nio.BasicDataStreamChannel)15 HttpContext (org.apache.hc.core5.http.protocol.HttpContext)15 EntityDetails (org.apache.hc.core5.http.EntityDetails)14 HttpResponse (org.apache.hc.core5.http.HttpResponse)14 CapacityChannel (org.apache.hc.core5.http.nio.CapacityChannel)14 Header (org.apache.hc.core5.http.Header)10 HttpRequest (org.apache.hc.core5.http.HttpRequest)9 AsyncServerExchangeHandler (org.apache.hc.core5.http.nio.AsyncServerExchangeHandler)8 ResponseChannel (org.apache.hc.core5.http.nio.ResponseChannel)8 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)8 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)7 RequestChannel (org.apache.hc.core5.http.nio.RequestChannel)7 InetSocketAddress (java.net.InetSocketAddress)6