use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class H2TLSIntegrationTest method testTLSTrustFailure.
@Test
public void testTLSTrustFailure() throws Exception {
server = AsyncServerBootstrap.bootstrap().setLookupRegistry(new UriPatternMatcher<>()).setIOReactorConfig(IOReactorConfig.custom().setSoTimeout(TIMEOUT).build()).setTlsStrategy(new BasicServerTlsStrategy(SSLTestContexts.createServerSSLContext())).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(SSLContexts.createDefault())).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(SSLHandshakeException.class));
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class Http1AuthenticationTest method testPostRequestAuthentication.
@Test
public void testPostRequestAuthentication() 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("localhost", address.getPort());
final Random rnd = new Random();
final byte[] stuff = new byte[10240];
for (int i = 0; i < stuff.length; i++) {
stuff[i] = (byte) ('a' + rnd.nextInt(10));
}
final HttpRequest request1 = new BasicHttpRequest(Method.POST, target, "/stuff");
final Future<Message<HttpResponse, String>> resultFuture1 = requester.execute(new BasicRequestProducer(request1, AsyncEntityProducers.create(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_UNAUTHORIZED));
final String body1 = message1.getBody();
assertThat(body1, CoreMatchers.equalTo("You shall not pass!!!"));
final HttpRequest request2 = new BasicHttpRequest(Method.POST, target, "/stuff");
request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
final Future<Message<HttpResponse, String>> resultFuture2 = requester.execute(new BasicRequestProducer(request2, AsyncEntityProducers.create(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(new String(stuff, StandardCharsets.US_ASCII)));
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class Http1AuthenticationTest method testPostRequestAuthenticationNoExpectContinue.
@Test
public void testPostRequestAuthenticationNoExpectContinue() 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("localhost", address.getPort());
final Random rnd = new Random();
final byte[] stuff = new byte[10240];
for (int i = 0; i < stuff.length; i++) {
stuff[i] = (byte) ('a' + rnd.nextInt(10));
}
final HttpRequest request1 = new BasicHttpRequest(Method.POST, target, "/stuff");
request1.setVersion(HttpVersion.HTTP_1_0);
final Future<Message<HttpResponse, String>> resultFuture1 = requester.execute(new BasicRequestProducer(request1, AsyncEntityProducers.create(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_UNAUTHORIZED));
final String body1 = message1.getBody();
assertThat(body1, CoreMatchers.equalTo("You shall not pass!!!"));
final HttpRequest request2 = new BasicHttpRequest(Method.POST, target, "/stuff");
request2.setVersion(HttpVersion.HTTP_1_0);
request2.setHeader(HttpHeaders.AUTHORIZATION, "let me pass");
final Future<Message<HttpResponse, String>> resultFuture2 = requester.execute(new BasicRequestProducer(request2, AsyncEntityProducers.create(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(new String(stuff, StandardCharsets.US_ASCII)));
}
use of org.apache.hc.core5.http.nio.support.BasicResponseConsumer in project httpcomponents-core by apache.
the class Http1IntegrationTest method testSimpleGetsPipelined.
@Test
public void testSimpleGetsPipelined() 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 < 5; 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 Http1IntegrationTest method testSimpleGetIdentityTransfer.
@Test
public void testSimpleGetIdentityTransfer() throws Exception {
server.register("/hello", () -> new SingleLineResponseHandler("Hi there"));
final HttpProcessor httpProcessor = new DefaultHttpProcessor(new RequestValidateHost());
final InetSocketAddress serverEndpoint = server.start(httpProcessor, Http1Config.DEFAULT);
client.start();
final int reqNo = 5;
for (int i = 0; i < reqNo; i++) {
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());
streamEndpoint.close();
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);
}
}
Aggregations