use of io.undertow.client.ClientConnection in project undertow by undertow-io.
the class Http2ClientTestCase method testHeadRequest.
@Test
public void testHeadRequest() throws Exception {
//
final UndertowClient client = createClient();
final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
final CountDownLatch latch = new CountDownLatch(10);
final ClientConnection connection = client.connect(ADDRESS, worker, new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, DefaultServer.getClientSSLContext()), DefaultServer.getBufferPool(), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
try {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
final ClientRequest request = new ClientRequest().setMethod(Methods.HEAD).setPath(MESSAGE);
request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
connection.sendRequest(request, createClientCallback(responses, latch));
}
}
});
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals(10, responses.size());
for (final ClientResponse response : responses) {
Assert.assertEquals("", response.getAttachment(RESPONSE_BODY));
}
} finally {
IoUtils.safeClose(connection);
}
}
use of io.undertow.client.ClientConnection in project undertow by undertow-io.
the class Http2ClientTestCase method testSimpleBasic.
@Test
public void testSimpleBasic() throws Exception {
//
final UndertowClient client = createClient();
final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
final CountDownLatch latch = new CountDownLatch(10);
final ClientConnection connection = client.connect(ADDRESS, worker, new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, DefaultServer.getClientSSLContext()), DefaultServer.getBufferPool(), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
try {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(MESSAGE);
request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
connection.sendRequest(request, createClientCallback(responses, latch));
}
}
});
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals(10, responses.size());
for (final ClientResponse response : responses) {
Assert.assertEquals(message, response.getAttachment(RESPONSE_BODY));
}
} finally {
IoUtils.safeClose(connection);
}
}
use of io.undertow.client.ClientConnection in project undertow by undertow-io.
the class AjpClientTestCase method testSendPing.
@Test
public void testSendPing() throws Exception {
//
final UndertowClient client = createClient();
final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
final FutureResult<Boolean> result = new FutureResult<>();
final CountDownLatch latch = new CountDownLatch(3);
final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
assertTrue(connection.isPingSupported());
try {
connection.getIoThread().execute(() -> {
final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(MESSAGE);
request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
connection.sendRequest(request, createClientCallback(responses, latch));
connection.sendPing(new ClientConnection.PingListener() {
@Override
public void acknowledged() {
result.setResult(true);
latch.countDown();
}
@Override
public void failed(IOException e) {
result.setException(e);
latch.countDown();
}
}, 5, TimeUnit.SECONDS);
connection.sendRequest(request, createClientCallback(responses, latch));
});
assertTrue(latch.await(10, TimeUnit.SECONDS));
Assert.assertEquals(2, responses.size());
assertTrue(result.getIoFuture().get());
for (final ClientResponse response : responses) {
Assert.assertEquals(message, response.getAttachment(RESPONSE_BODY));
}
// now try a failed ping
try {
undertow.stop();
final FutureResult<Boolean> failResult = new FutureResult<>();
connection.getIoThread().execute(() -> connection.sendPing(new ClientConnection.PingListener() {
@Override
public void acknowledged() {
failResult.setResult(true);
}
@Override
public void failed(IOException e) {
failResult.setException(e);
}
}, 4, TimeUnit.SECONDS));
try {
failResult.getIoFuture().get();
Assert.fail("ping should have failed");
} catch (IOException e) {
// ignored
}
} finally {
// add an extra sleep time to make sure we are not getting a BindException
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// ignore
}
undertow.start();
}
} finally {
IoUtils.safeClose(connection);
}
}
use of io.undertow.client.ClientConnection in project undertow by undertow-io.
the class AjpClientTestCase method testConnectionClose.
@Test
public void testConnectionClose() throws Exception {
//
final UndertowClient client = createClient();
final CountDownLatch latch = new CountDownLatch(1);
final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
try {
ClientRequest request = new ClientRequest().setPath(MESSAGE).setMethod(Methods.GET);
request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
request.getRequestHeaders().add(Headers.CONNECTION, Headers.CLOSE.toString());
connection.sendRequest(request, createClientCallback(responses, latch));
latch.await();
final ClientResponse response = responses.iterator().next();
Assert.assertEquals(message, response.getAttachment(RESPONSE_BODY));
Assert.assertFalse(connection.isOpen());
} finally {
IoUtils.safeClose(connection);
}
}
use of io.undertow.client.ClientConnection in project undertow by undertow-io.
the class H2CUpgradeResetTestCase method testUpgradeWithReset.
/**
* The real test that sends several POST requests with and without reset.
* @throws Exception Some error
*/
@Test
public void testUpgradeWithReset() throws Exception {
final UndertowClient client = UndertowClient.getInstance();
// the client connection uses the small byte-buffer of 1024 to force the continuation frames
final ClientConnection connection = client.connect(new URI("http://" + DefaultServer.getHostAddress() + ":" + (DefaultServer.getHostPort() + 1)), worker, new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, DefaultServer.getClientSSLContext()), DefaultServer.getBufferPool(), OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
try {
// the first request triggers the upgrade to H2C and sends a RST
sendRequest(connection, 10, true);
// send several requests with and without reset
sendRequest(connection, 10, false);
sendRequest(connection, 10, true);
sendRequest(connection, 10, false);
} finally {
IoUtils.safeClose(connection);
}
}
Aggregations