use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class Http1IntegrationTest method testLargeGetsPipelined.
@Test
public void testLargeGetsPipelined() throws Exception {
server.register("/", () -> new MultiLineResponseHandler("0123456789abcdef", 2000));
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 < 5; i++) {
queue.add(streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/")), 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());
final String entity = result.getBody();
Assertions.assertNotNull(entity);
final StringTokenizer t = new StringTokenizer(entity, "\r\n");
while (t.hasMoreTokens()) {
Assertions.assertEquals("0123456789abcdef", t.nextToken());
}
}
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class Http1IntegrationTest method testExceptionInHandler.
@Test
public void testExceptionInHandler() throws Exception {
server.register("/hello", () -> new SingleLineResponseHandler("Hi there") {
@Override
protected void handle(final Message<HttpRequest, String> request, final AsyncServerRequestHandler.ResponseTrigger responseTrigger, final HttpContext context) throws IOException, HttpException {
throw new HttpException("Boom");
}
});
final InetSocketAddress serverEndpoint = server.start();
client.start();
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.GET, 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();
final String entity1 = result.getBody();
Assertions.assertNotNull(response1);
Assertions.assertEquals(500, response1.getCode());
Assertions.assertEquals("Boom", entity1);
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class Http1IntegrationTest method testProtocolException.
@Test
public void testProtocolException() throws Exception {
server.register("/boom", () -> new AsyncServerExchangeHandler() {
private final StringAsyncEntityProducer entityProducer = new StringAsyncEntityProducer("Everyting is OK");
@Override
public void releaseResources() {
entityProducer.releaseResources();
}
@Override
public void handleRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException {
final String requestUri = request.getRequestUri();
if (requestUri.endsWith("boom")) {
throw new ProtocolException("Boom!!!");
}
responseChannel.sendResponse(new BasicHttpResponse(200), entityProducer, context);
}
@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 {
// empty
}
@Override
public int available() {
return entityProducer.available();
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
entityProducer.produce(channel);
}
@Override
public void failed(final Exception cause) {
releaseResources();
}
});
final InetSocketAddress serverEndpoint = server.start();
client.start();
final Future<ClientSessionEndpoint> connectFuture = client.connect("localhost", serverEndpoint.getPort(), TIMEOUT);
final ClientSessionEndpoint streamEndpoint = connectFuture.get();
final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.GET, createRequestURI(serverEndpoint, "/boom")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result);
final HttpResponse response1 = result.getHead();
final String entity1 = result.getBody();
Assertions.assertNotNull(response1);
Assertions.assertEquals(HttpStatus.SC_BAD_REQUEST, response1.getCode());
Assertions.assertEquals("Boom!!!", entity1);
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class Http1IntegrationTest method testPipelinedInvalidRequest.
@Test
public void testPipelinedInvalidRequest() throws Exception {
server.register("/hello*", () -> new SingleLineResponseHandler("Hi back"));
final InetSocketAddress serverEndpoint = server.start();
client.start();
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.POST, createRequestURI(serverEndpoint, "/hello-1"), AsyncEntityProducers.create("Hi there")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final HttpRequest request2 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/hello-2"));
request2.addHeader(HttpHeaders.HOST, "blah:blah");
final Future<Message<HttpResponse, String>> future2 = streamEndpoint.execute(new BasicRequestProducer(request2, AsyncEntityProducers.create("Hi there")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Future<Message<HttpResponse, String>> future3 = streamEndpoint.execute(new BasicRequestProducer(Method.POST, createRequestURI(serverEndpoint, "/hello-3"), AsyncEntityProducers.create("Hi there")), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result1 = future1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result1);
final HttpResponse response1 = result1.getHead();
final String entity1 = result1.getBody();
Assertions.assertNotNull(response1);
Assertions.assertEquals(200, response1.getCode());
Assertions.assertEquals("Hi back", entity1);
final Message<HttpResponse, String> result2 = future2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result2);
final HttpResponse response2 = result2.getHead();
final String entity2 = result2.getBody();
Assertions.assertNotNull(response2);
Assertions.assertEquals(400, response2.getCode());
Assertions.assertTrue(entity2.length() > 0);
final Exception exception = Assertions.assertThrows(Exception.class, () -> future3.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
assertThat(exception, CoreMatchers.anyOf(CoreMatchers.instanceOf(CancellationException.class), CoreMatchers.instanceOf(ExecutionException.class)));
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class Http1IntegrationTest method testLargePost.
@Test
public void testLargePost() 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();
for (int i = 0; i < 5; i++) {
final Future<Message<HttpResponse, String>> future = streamEndpoint.execute(new BasicRequestProducer(Method.POST, createRequestURI(serverEndpoint, "/echo"), new MultiLineEntityProducer("0123456789abcdef", 5000)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> result = future.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
Assertions.assertNotNull(result);
final HttpResponse response = result.getHead();
Assertions.assertNotNull(response);
Assertions.assertEquals(200, response.getCode());
final String entity = result.getBody();
Assertions.assertNotNull(entity);
final StringTokenizer t = new StringTokenizer(entity, "\r\n");
while (t.hasMoreTokens()) {
Assertions.assertEquals("0123456789abcdef", t.nextToken());
}
}
}
Aggregations