use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class H2IntegrationTest method testHeaderTooLargePost.
@Test
public void testHeaderTooLargePost() throws Exception {
server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
final InetSocketAddress serverEndpoint = server.start(H2Config.custom().setMaxHeaderListSize(100).build());
client.start(new DefaultHttpProcessor(new H2RequestContent(), new H2RequestTargetHost(), new H2RequestConnControl()), H2Config.DEFAULT);
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/hello"));
request1.setHeader("big-f-header", "1234567890123456789012345678901234567890123456789012345678901234567890" + "1234567890123456789012345678901234567890");
final byte[] b = new byte[2048];
for (int i = 0; i < b.length; i++) {
b[i] = (byte) ('a' + i % 10);
}
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, AsyncEntityProducers.create(b, ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
Assertions.assertNotNull(response1);
Assertions.assertEquals(431, response1.getCode());
Assertions.assertEquals("Maximum header list size exceeded", result1.getBody());
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class H2IntegrationTest method testSimpleGet.
@Test
public void testSimpleGet() throws Exception {
server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
final InetSocketAddress serverEndpoint = server.start();
client.start();
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 < 10; i++) {
queue.add(streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null));
}
while (!queue.isEmpty()) {
final Future<Message<HttpResponse, String>> future = queue.remove();
final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result);
final HttpResponse response = result.getHead();
final String entity = result.getBody();
Assertions.assertNotNull(response);
Assertions.assertEquals(200, response.getCode());
Assertions.assertEquals("Hi there", entity);
}
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class H2IntegrationTest method testExcessOfConcurrentStreams.
@Test
public void testExcessOfConcurrentStreams() throws Exception {
server.register("/", () -> new MultiLineResponseHandler("0123456789abcdef", 2000));
final InetSocketAddress serverEndpoint = server.start(H2Config.custom().setMaxConcurrentStreams(20).build());
client.start(H2Config.custom().setMaxConcurrentStreams(20).build());
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final Queue<Future<Message<HttpResponse, Void>>> queue = new LinkedList<>();
for (int i = 0; i < 2000; i++) {
final HttpRequest request1 = new BasicHttpRequest(Method.GET, createRequestURI(serverEndpoint, "/"));
final Future<Message<HttpResponse, Void>> future = streamEndpoint.execute(new BasicRequestProducer(request1, null), new BasicResponseConsumer<>(new DiscardingEntityConsumer<>()), null);
queue.add(future);
}
while (!queue.isEmpty()) {
final Future<Message<HttpResponse, Void>> future = queue.remove();
final Message<HttpResponse, Void> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result);
final HttpResponse response = result.getHead();
Assertions.assertNotNull(response);
Assertions.assertEquals(200, response.getCode());
}
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class H2IntegrationTest method testSimpleHead.
@Test
public void testSimpleHead() throws Exception {
server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
final InetSocketAddress serverEndpoint = server.start();
client.start();
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
for (int i = 0; i < 5; i++) {
final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.HEAD, createRequestURI(serverEndpoint, "/hello")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result);
final HttpResponse response1 = result.getHead();
Assertions.assertNotNull(response1);
Assertions.assertEquals(200, response1.getCode());
Assertions.assertNull(result.getBody());
}
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer 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());
}
}
Aggregations