use of org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityProducer in project httpcomponents-core by apache.
the class Http1IntegrationTest method testSlowRequestProducer.
@Test
public void testSlowRequestProducer() throws Exception {
server.register("*", () -> new EchoHandler(2048));
final InetSocketAddress serverEndpoint = server.start();
client.start();
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/echo"));
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, new AbstractClassicEntityProducer(4096, ContentType.TEXT_PLAIN, Executors.newSingleThreadExecutor()) {
@Override
protected void produceData(final ContentType contentType, final OutputStream outputStream) throws IOException {
final Charset charset = ContentType.getCharset(contentType, StandardCharsets.US_ASCII);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, charset))) {
for (int i = 0; i < 500; i++) {
if (i % 100 == 0) {
writer.flush();
Thread.sleep(500);
}
writer.write("0123456789abcdef\r\n");
}
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new InterruptedIOException(ex.getMessage());
}
}
}), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result1 = future1.get(LONG_TIMEOUT.getDuration(), LONG_TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
Assertions.assertNotNull(response1);
Assertions.assertEquals(200, response1.getCode());
final String s1 = result1.getBody();
Assertions.assertNotNull(s1);
final StringTokenizer t1 = new StringTokenizer(s1, "\r\n");
while (t1.hasMoreTokens()) {
Assertions.assertEquals("0123456789abcdef", t1.nextToken());
}
}
use of org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityProducer in project httpcomponents-core by apache.
the class H2IntegrationTest method testSlowRequestProducer.
@Test
public void testSlowRequestProducer() throws Exception {
server.register("*", () -> new EchoHandler(2048));
final InetSocketAddress serverEndpoint = server.start();
client.start();
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/echo"));
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, new AbstractClassicEntityProducer(4096, ContentType.TEXT_PLAIN, Executors.newSingleThreadExecutor()) {
@Override
protected void produceData(final ContentType contentType, final OutputStream outputStream) throws IOException {
final Charset charset = ContentType.getCharset(contentType, StandardCharsets.US_ASCII);
try (BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, charset))) {
for (int i = 0; i < 500; i++) {
if (i % 100 == 0) {
writer.flush();
Thread.sleep(500);
}
writer.write("0123456789abcdef\r\n");
}
} catch (final InterruptedException ex) {
Thread.currentThread().interrupt();
throw new InterruptedIOException(ex.getMessage());
}
}
}), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result1 = future1.get(LONG_TIMEOUT.getDuration(), LONG_TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
Assertions.assertNotNull(response1);
Assertions.assertEquals(200, response1.getCode());
final String s1 = result1.getBody();
Assertions.assertNotNull(s1);
final StringTokenizer t1 = new StringTokenizer(s1, "\r\n");
while (t1.hasMoreTokens()) {
Assertions.assertEquals("0123456789abcdef", t1.nextToken());
}
}
Aggregations