use of org.apache.hc.core5.http.nio.AsyncEntityProducer in project httpcomponents-core by apache.
the class Http1IntegrationTest method testDelayedExpectationVerification.
@Test
public void testDelayedExpectationVerification() throws Exception {
server.register("*", () -> new AsyncServerExchangeHandler() {
private final Random random = new Random(System.currentTimeMillis());
private final AsyncEntityProducer entityProducer = AsyncEntityProducers.create("All is well");
@Override
public void handleRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException {
Executors.newSingleThreadExecutor().execute(() -> {
try {
if (entityDetails != null) {
final Header h = request.getFirstHeader(HttpHeaders.EXPECT);
if (h != null && HeaderElements.CONTINUE.equalsIgnoreCase(h.getValue())) {
Thread.sleep(random.nextInt(1000));
responseChannel.sendInformation(new BasicHttpResponse(HttpStatus.SC_CONTINUE), context);
}
final HttpResponse response = new BasicHttpResponse(200);
synchronized (entityProducer) {
responseChannel.sendResponse(response, entityProducer, context);
}
}
} catch (final Exception ignore) {
// ignore
}
});
}
@Override
public void updateCapacity(final CapacityChannel capacityChannel) throws IOException {
capacityChannel.update(Integer.MAX_VALUE);
}
@Override
public void consume(final ByteBuffer src) throws IOException {
}
@Override
public void streamEnd(final List<? extends Header> trailers) throws HttpException, IOException {
}
@Override
public int available() {
synchronized (entityProducer) {
return entityProducer.available();
}
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
synchronized (entityProducer) {
entityProducer.produce(channel);
}
}
@Override
public void failed(final Exception cause) {
}
@Override
public void releaseResources() {
}
});
final InetSocketAddress serverEndpoint = server.start();
client.start(Http1Config.custom().setWaitForContinueTimeout(Timeout.ofMilliseconds(100)).build());
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final Queue<Future<Message<HttpResponse, String>>> queue = new LinkedList<>();
for (int i = 0; i < 5; i++) {
queue.add(streamEndpoint.execute(new BasicRequestProducer(Method.POST, createRequestURI(serverEndpoint, "/"), AsyncEntityProducers.create("Some important message")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null));
}
while (!queue.isEmpty()) {
final Future<Message<HttpResponse, String>> future = queue.remove();
final Message<HttpResponse, String> result = future.get(LONG_TIMEOUT.getDuration(), LONG_TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result);
final HttpResponse response = result.getHead();
Assertions.assertNotNull(response);
Assertions.assertEquals(200, response.getCode());
Assertions.assertNotNull("All is well", result.getBody());
}
}
use of org.apache.hc.core5.http.nio.AsyncEntityProducer 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));
}
use of org.apache.hc.core5.http.nio.AsyncEntityProducer in project httpcomponents-core by apache.
the class TestAbstractBinAsyncEntityProducer method testProduceDataWithBuffering1.
@Test
public void testProduceDataWithBuffering1() throws Exception {
final AsyncEntityProducer producer = new ChunkByteAsyncEntityProducer(5, ContentType.TEXT_PLAIN, new byte[] { '1', '2', '3' }, new byte[] { '4', '5', '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("123", byteChannel.dump(StandardCharsets.US_ASCII));
producer.produce(streamChannel);
Assertions.assertTrue(byteChannel.isOpen());
Assertions.assertEquals("45678", byteChannel.dump(StandardCharsets.US_ASCII));
producer.produce(streamChannel);
Assertions.assertFalse(byteChannel.isOpen());
Assertions.assertEquals("90", byteChannel.dump(StandardCharsets.US_ASCII));
}
use of org.apache.hc.core5.http.nio.AsyncEntityProducer 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));
}
use of org.apache.hc.core5.http.nio.AsyncEntityProducer 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));
}
Aggregations