Search in sources :

Example 1 with AbstractClassicEntityConsumer

use of org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityConsumer in project httpcomponents-core by apache.

the class H2IntegrationTest method testSlowResponseConsumer.

@Test
public void testSlowResponseConsumer() throws Exception {
    server.register("/", () -> new MultiLineResponseHandler("0123456789abcd", 3));
    final InetSocketAddress serverEndpoint = server.start();
    client.start(H2Config.custom().setInitialWindowSize(16).build());
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/"), null), new BasicResponseConsumer<>(new AbstractClassicEntityConsumer<String>(16, Executors.newSingleThreadExecutor()) {

        @Override
        protected String consumeData(final ContentType contentType, final InputStream inputStream) throws IOException {
            final Charset charset = ContentType.getCharset(contentType, StandardCharsets.US_ASCII);
            final StringBuilder buffer = new StringBuilder();
            try {
                final byte[] tmp = new byte[16];
                int l;
                while ((l = inputStream.read(tmp)) != -1) {
                    buffer.append(charset.decode(ByteBuffer.wrap(tmp, 0, l)));
                    Thread.sleep(500);
                }
            } catch (final InterruptedException ex) {
                Thread.currentThread().interrupt();
                throw new InterruptedIOException(ex.getMessage());
            }
            return buffer.toString();
        }
    }), 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("0123456789abcd", t1.nextToken());
    }
}
Also used : 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) InputStream(java.io.InputStream) Charset(java.nio.charset.Charset) HttpResponse(org.apache.hc.core5.http.HttpResponse) AbstractClassicEntityConsumer(org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityConsumer) StringTokenizer(java.util.StringTokenizer) Test(org.junit.Test)

Example 2 with AbstractClassicEntityConsumer

use of org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityConsumer in project httpcomponents-core by apache.

the class Http1IntegrationTest method testSlowResponseConsumer.

@Test
public void testSlowResponseConsumer() throws Exception {
    server.register("/", () -> new MultiLineResponseHandler("0123456789abcd", 100));
    final InetSocketAddress serverEndpoint = server.start();
    client.start(Http1Config.custom().setBufferSize(256).build());
    final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
    final ClientSessionEndpoint streamEndpoint = connectFuture.get();
    final HttpRequest request1 = new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/"));
    final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, null), new BasicResponseConsumer<>(new AbstractClassicEntityConsumer<String>(16, Executors.newSingleThreadExecutor()) {

        @Override
        protected String consumeData(final ContentType contentType, final InputStream inputStream) throws IOException {
            final Charset charset = ContentType.getCharset(contentType, StandardCharsets.US_ASCII);
            final StringBuilder buffer = new StringBuilder();
            try {
                final byte[] tmp = new byte[16];
                int l;
                while ((l = inputStream.read(tmp)) != -1) {
                    buffer.append(charset.decode(ByteBuffer.wrap(tmp, 0, l)));
                    Thread.sleep(50);
                }
            } catch (final InterruptedException ex) {
                Thread.currentThread().interrupt();
                throw new InterruptedIOException(ex.getMessage());
            }
            return buffer.toString();
        }
    }), 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("0123456789abcd", t1.nextToken());
    }
}
Also used : BasicHttpRequest(org.apache.hc.core5.http.message.BasicHttpRequest) HttpRequest(org.apache.hc.core5.http.HttpRequest) 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) InputStream(java.io.InputStream) 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) AbstractClassicEntityConsumer(org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityConsumer) StringTokenizer(java.util.StringTokenizer) Test(org.junit.Test)

Aggregations

InputStream (java.io.InputStream)2 InterruptedIOException (java.io.InterruptedIOException)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 HttpResponse (org.apache.hc.core5.http.HttpResponse)2 Message (org.apache.hc.core5.http.Message)2 BasicRequestProducer (org.apache.hc.core5.http.nio.support.BasicRequestProducer)2 AbstractClassicEntityConsumer (org.apache.hc.core5.http.nio.support.classic.AbstractClassicEntityConsumer)2 Test (org.junit.Test)2 HttpRequest (org.apache.hc.core5.http.HttpRequest)1 BasicHttpRequest (org.apache.hc.core5.http.message.BasicHttpRequest)1 BasicHttpResponse (org.apache.hc.core5.http.message.BasicHttpResponse)1