Search in sources :

Example 1 with AbstractClassicEntityProducer

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());
    }
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) InterruptedIOException(java.io.InterruptedIOException) Message(org.apache.hc.core5.http.Message) ContentType(org.apache.hc.core5.http.ContentType) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpResponse(org.apache.hc.core5.http.message.BasicHttpResponse) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) BufferedWriter(java.io.BufferedWriter) StringTokenizer(java.util.StringTokenizer) OutputStreamWriter(java.io.OutputStreamWriter) AbstractClassicEntityProducer(org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityProducer) Test(org.junit.Test)

Example 2 with AbstractClassicEntityProducer

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());
    }
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) StringAsyncEntityConsumer(org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer) InterruptedIOException(java.io.InterruptedIOException) Message(org.apache.hc.core5.http.Message) ContentType(org.apache.hc.core5.http.ContentType) InetSocketAddress(java.net.InetSocketAddress) BasicRequestProducer(org.apache.hc.core5.http.nio.support.BasicRequestProducer) OutputStream(java.io.OutputStream) Charset(java.nio.charset.Charset) HttpResponse(org.apache.hc.core5.http.HttpResponse) BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) BufferedWriter(java.io.BufferedWriter) StringTokenizer(java.util.StringTokenizer) OutputStreamWriter(java.io.OutputStreamWriter) AbstractClassicEntityProducer(org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityProducer) Test(org.junit.Test)

Aggregations

BufferedWriter (java.io.BufferedWriter)2 InterruptedIOException (java.io.InterruptedIOException)2 OutputStream (java.io.OutputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 InetSocketAddress (java.net.InetSocketAddress)2 Charset (java.nio.charset.Charset)2 StringTokenizer (java.util.StringTokenizer)2 ContentType (org.apache.hc.core5.http.ContentType)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)2 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 Message (org.apache.hc.core5.http.Message)2 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)2 StringAsyncEntityConsumer (org.apache.hc.core5.http.nio.entity.StringAsyncEntityConsumer)2 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)2 AbstractClassicEntityProducer (org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityProducer)2 Test (org.junit.Test)2 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)1