use of io.undertow.client.UndertowClient in project undertow by undertow-io.
the class AjpClientTestCase method testPostRequest.
@Test
public void testPostRequest() throws Exception {
//
final UndertowClient client = createClient();
final String postMessage = "This is a post request";
final List<String> responses = new CopyOnWriteArrayList<>();
final CountDownLatch latch = new CountDownLatch(10);
final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), OptionMap.EMPTY).get();
try {
connection.getIoThread().execute(() -> {
for (int i = 0; i < 10; i++) {
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(POST);
request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
request.getRequestHeaders().put(Headers.TRANSFER_ENCODING, "chunked");
connection.sendRequest(request, new ClientCallback<ClientExchange>() {
@Override
public void completed(ClientExchange result) {
new StringWriteChannelListener(postMessage).setup(result.getRequestChannel());
result.setResponseListener(new ClientCallback<ClientExchange>() {
@Override
public void completed(ClientExchange result) {
new StringReadChannelListener(DefaultServer.getBufferPool()) {
@Override
protected void stringDone(String string) {
responses.add(string);
latch.countDown();
}
@Override
protected void error(IOException e) {
e.printStackTrace();
latch.countDown();
}
}.setup(result.getResponseChannel());
}
@Override
public void failed(IOException e) {
e.printStackTrace();
latch.countDown();
}
});
}
@Override
public void failed(IOException e) {
e.printStackTrace();
latch.countDown();
}
});
}
});
assertTrue(latch.await(10, TimeUnit.SECONDS));
Assert.assertEquals(10, responses.size());
for (final String response : responses) {
Assert.assertEquals(postMessage, response);
}
} finally {
IoUtils.safeClose(connection);
}
}
use of io.undertow.client.UndertowClient in project undertow by undertow-io.
the class HttpClientTestCase method testReadTimeout.
@Test
public void testReadTimeout() throws Exception {
//
final UndertowClient client = createClient();
exception = null;
final List<ClientResponse> responses = new CopyOnWriteArrayList<>();
final CountDownLatch latch = new CountDownLatch(1);
OptionMap.Builder builder = OptionMap.builder();
builder.set(Options.READ_TIMEOUT, 600);
final ClientConnection connection = client.connect(ADDRESS, worker, DefaultServer.getBufferPool(), builder.getMap()).get();
final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(READTIMEOUT);
request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
connection.sendRequest(request, createClientCallback(responses, latch, false));
try {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 1; i++) {
final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(READTIMEOUT);
request.getRequestHeaders().put(Headers.HOST, DefaultServer.getHostAddress());
connection.sendRequest(request, createClientCallback(responses, latch, false));
}
}
});
latch.await(10, TimeUnit.SECONDS);
// exception expected because of read timeout
Assert.assertNotNull(exception);
} finally {
IoUtils.safeClose(connection);
}
}
use of io.undertow.client.UndertowClient in project undertow by undertow-io.
the class HttpClientTestCase 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, DefaultServer.getBufferPool(), OptionMap.EMPTY).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);
}
}
Aggregations