use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class H2ServerAndRequesterTest method testSequentialRequestsSameEndpoint.
@Test
public void testSequentialRequestsSameEndpoint() throws Exception {
server.start();
final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), scheme);
final ListenerEndpoint listener = future.get();
final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
requester.start();
final HttpHost target = new HttpHost(scheme.id, "localhost", address.getPort());
final Future<AsyncClientEndpoint> endpointFuture = requester.connect(target, Timeout.ofSeconds(5));
final AsyncClientEndpoint endpoint = endpointFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
try {
final Future<Message<HttpResponse, String>> resultFuture1 = endpoint.execute(new BasicRequestProducer(Method.POST, target, "/stuff", new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> message1 = resultFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
assertThat(message1, CoreMatchers.notNullValue());
final HttpResponse response1 = message1.getHead();
assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
final String body1 = message1.getBody();
assertThat(body1, CoreMatchers.equalTo("some stuff"));
final Future<Message<HttpResponse, String>> resultFuture2 = endpoint.execute(new BasicRequestProducer(Method.POST, target, "/other-stuff", new StringAsyncEntityProducer("some other stuff", ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> message2 = resultFuture2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
assertThat(message2, CoreMatchers.notNullValue());
final HttpResponse response2 = message2.getHead();
assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
final String body2 = message2.getBody();
assertThat(body2, CoreMatchers.equalTo("some other stuff"));
final Future<Message<HttpResponse, String>> resultFuture3 = endpoint.execute(new BasicRequestProducer(Method.POST, target, "/more-stuff", new StringAsyncEntityProducer("some more stuff", ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), null);
final Message<HttpResponse, String> message3 = resultFuture3.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
assertThat(message3, CoreMatchers.notNullValue());
final HttpResponse response3 = message3.getHead();
assertThat(response3.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
final String body3 = message3.getBody();
assertThat(body3, CoreMatchers.equalTo("some more stuff"));
} finally {
endpoint.releaseAndReuse();
}
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class H2ServerAndRequesterTest method testSequentialRequests.
@Test
public void testSequentialRequests() throws Exception {
server.start();
final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), scheme);
final ListenerEndpoint listener = future.get();
final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
requester.start();
final HttpHost target = new HttpHost(scheme.id, "localhost", address.getPort());
final Future<Message<HttpResponse, String>> resultFuture1 = requester.execute(new BasicRequestProducer(Method.POST, target, "/stuff", new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
final Message<HttpResponse, String> message1 = resultFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
assertThat(message1, CoreMatchers.notNullValue());
final HttpResponse response1 = message1.getHead();
assertThat(response1.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
final String body1 = message1.getBody();
assertThat(body1, CoreMatchers.equalTo("some stuff"));
final Future<Message<HttpResponse, String>> resultFuture2 = requester.execute(new BasicRequestProducer(Method.POST, target, "/other-stuff", new StringAsyncEntityProducer("some other stuff", ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
final Message<HttpResponse, String> message2 = resultFuture2.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
assertThat(message2, CoreMatchers.notNullValue());
final HttpResponse response2 = message2.getHead();
assertThat(response2.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
final String body2 = message2.getBody();
assertThat(body2, CoreMatchers.equalTo("some other stuff"));
final Future<Message<HttpResponse, String>> resultFuture3 = requester.execute(new BasicRequestProducer(Method.POST, target, "/more-stuff", new StringAsyncEntityProducer("some more stuff", ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
final Message<HttpResponse, String> message3 = resultFuture3.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
assertThat(message3, CoreMatchers.notNullValue());
final HttpResponse response3 = message3.getHead();
assertThat(response3.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
final String body3 = message3.getBody();
assertThat(body3, CoreMatchers.equalTo("some more stuff"));
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class H2ServerBootstrapFiltersTest method testSequentialRequests.
@Test
public void testSequentialRequests() throws Exception {
server.start();
final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTP);
final ListenerEndpoint listener = future.get();
final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
requester.start();
final HttpHost target = new HttpHost("http", "localhost", address.getPort());
final Future<Message<HttpResponse, String>> resultFuture = requester.execute(new BasicRequestProducer(Method.POST, target, "/stuff", new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
final Message<HttpResponse, String> message = resultFuture.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit());
assertThat(message, CoreMatchers.notNullValue());
final HttpResponse response = message.getHead();
assertThat(response.getCode(), CoreMatchers.equalTo(HttpStatus.SC_OK));
final Header testFilterHeader = response.getHeader("X-Test-Filter");
assertThat(testFilterHeader, CoreMatchers.notNullValue());
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class H2TLSIntegrationTest method testTLSClientAuthFailure.
@Test
public void testTLSClientAuthFailure() throws Exception {
server = AsyncServerBootstrap.bootstrap().setLookupRegistry(new UriPatternMatcher<>()).setIOReactorConfig(IOReactorConfig.custom().setSoTimeout(TIMEOUT).build()).setTlsStrategy(new BasicServerTlsStrategy(SSLTestContexts.createServerSSLContext(), (endpoint, sslEngine) -> sslEngine.setNeedClientAuth(true), null)).setStreamListener(LoggingHttp1StreamListener.INSTANCE_SERVER).setIOSessionDecorator(LoggingIOSessionDecorator.INSTANCE).setExceptionCallback(LoggingExceptionCallback.INSTANCE).setIOSessionListener(LoggingIOSessionListener.INSTANCE).register("*", () -> new EchoHandler(2048)).create();
server.start();
requester = H2RequesterBootstrap.bootstrap().setIOReactorConfig(IOReactorConfig.custom().setSoTimeout(TIMEOUT).build()).setTlsStrategy(new BasicClientTlsStrategy(SSLTestContexts.createClientSSLContext())).setStreamListener(LoggingHttp1StreamListener.INSTANCE_CLIENT).setConnPoolListener(LoggingConnPoolListener.INSTANCE).setIOSessionDecorator(LoggingIOSessionDecorator.INSTANCE).setExceptionCallback(LoggingExceptionCallback.INSTANCE).setIOSessionListener(LoggingIOSessionListener.INSTANCE).create();
server.start();
final Future<ListenerEndpoint> future = server.listen(new InetSocketAddress(0), URIScheme.HTTPS);
final ListenerEndpoint listener = future.get();
final InetSocketAddress address = (InetSocketAddress) listener.getAddress();
requester.start();
final HttpHost target = new HttpHost(URIScheme.HTTPS.id, "localhost", address.getPort());
final Future<Message<HttpResponse, String>> resultFuture1 = requester.execute(new BasicRequestProducer(Method.POST, target, "/stuff", new StringAsyncEntityProducer("some stuff", ContentType.TEXT_PLAIN)), new BasicResponseConsumer<>(new StringAsyncEntityConsumer()), TIMEOUT, null);
final ExecutionException exception = Assertions.assertThrows(ExecutionException.class, () -> resultFuture1.get(TIMEOUT.getDuration(), TIMEOUT.getTimeUnit()));
final Throwable cause = exception.getCause();
assertThat(cause, CoreMatchers.instanceOf(IOException.class));
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class Http1IntegrationTest method testPrematureResponse.
@Test
public void testPrematureResponse() throws Exception {
server.register("*", () -> new AsyncServerExchangeHandler() {
private final AtomicReference<AsyncResponseProducer> responseProducer = new AtomicReference<>();
@Override
public void handleRequest(final HttpRequest request, final EntityDetails entityDetails, final ResponseChannel responseChannel, final HttpContext context) throws HttpException, IOException {
final AsyncResponseProducer producer;
final Header h = request.getFirstHeader("password");
if (h != null && "secret".equals(h.getValue())) {
producer = new BasicResponseProducer(HttpStatus.SC_OK, "All is well");
} else {
producer = new BasicResponseProducer(HttpStatus.SC_UNAUTHORIZED, "You shall not pass");
}
responseProducer.set(producer);
producer.sendResponse(responseChannel, 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 {
}
@Override
public int available() {
final AsyncResponseProducer producer = responseProducer.get();
return producer.available();
}
@Override
public void produce(final DataStreamChannel channel) throws IOException {
final AsyncResponseProducer producer = responseProducer.get();
producer.produce(channel);
}
@Override
public void failed(final Exception cause) {
}
@Override
public void releaseResources() {
}
});
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 < 3; i++) {
final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/echo"));
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, new MultiLineEntityProducer("0123456789abcdef", 100000)), 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(HttpStatus.SC_UNAUTHORIZED, response1.getCode());
Assertions.assertNotNull("You shall not pass", result1.getBody());
Assertions.assertTrue(streamEndpoint.isOpen());
}
final HttpRequest request1 = new BasicHttpRequest(Method.POST, createRequestURI(serverEndpoint, "/echo"));
final Future<Message<HttpResponse, String>> future1 = streamEndpoint.execute(new BasicRequestProducer(request1, new MultiBinEntityProducer(new byte[] { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }, 100000, 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(HttpStatus.SC_UNAUTHORIZED, response1.getCode());
Assertions.assertNotNull("You shall not pass", result1.getBody());
Assertions.assertFalse(streamEndpoint.isOpen());
}
Aggregations