use of io.undertow.protocols.ssl.UndertowXnioSsl in project undertow by undertow-io.
the class WebSocketClient13TestCase method testTextMessageWss.
@Test
public void testTextMessageWss() throws Exception {
UndertowXnioSsl ssl = new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, DefaultServer.getClientSSLContext());
final WebSocketClient.ConnectionBuilder connectionBuilder = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI("wss://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostSSLPort("default"))).setSsl(ssl);
IoFuture<WebSocketChannel> future = connectionBuilder.connect();
future.await(4, TimeUnit.SECONDS);
final WebSocketChannel webSocketChannel = future.get();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> result = new AtomicReference<>();
webSocketChannel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
String data = message.getData();
result.set(data);
latch.countDown();
}
@Override
protected void onError(WebSocketChannel channel, Throwable error) {
super.onError(channel, error);
error.printStackTrace();
latch.countDown();
}
});
webSocketChannel.resumeReceives();
StreamSinkFrameChannel sendChannel = webSocketChannel.send(WebSocketFrameType.TEXT);
new StringWriteChannelListener("Hello World").setup(sendChannel);
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals("Hello World", result.get());
webSocketChannel.sendClose();
}
use of io.undertow.protocols.ssl.UndertowXnioSsl in project light-4j by networknt.
the class Http2ClientIT method testMultipleHttpGetSsl.
/*
@Test
public void testMultipleHttp2Post() throws Exception {
//
final Http2Client 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, pool, 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.POST).setPath(POST);
request.getRequestHeaders().put(Headers.HOST, "localhost");
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(pool) {
@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();
}
});
}
}
});
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals(10, responses.size());
for (final String response : responses) {
Assert.assertEquals(postMessage, response);
}
} finally {
IoUtils.safeClose(connection);
}
}
*/
@Test
public void testMultipleHttpGetSsl() throws Exception {
//
final Http2Client client = createClient();
final List<AtomicReference<ClientResponse>> references = new CopyOnWriteArrayList<>();
final CountDownLatch latch = new CountDownLatch(10);
SSLContext context = client.createSSLContext();
XnioSsl ssl = new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, Http2Client.SSL_BUFFER_POOL, context);
final ClientConnection connection = client.connect(new URI("https://localhost:7778"), worker, ssl, Http2Client.POOL, OptionMap.EMPTY).get();
try {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
for (int i = 0; i < 10; i++) {
AtomicReference<ClientResponse> reference = new AtomicReference<>();
references.add(i, reference);
final ClientRequest request = new ClientRequest().setMethod(Methods.GET).setPath(MESSAGE);
request.getRequestHeaders().put(Headers.HOST, "localhost");
connection.sendRequest(request, client.createClientCallback(reference, latch));
}
}
});
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals(10, references.size());
for (final AtomicReference<ClientResponse> reference : references) {
Assert.assertEquals(message, reference.get().getAttachment(Http2Client.RESPONSE_BODY));
Assert.assertEquals("HTTP/1.1", reference.get().getProtocol().toString());
}
} finally {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
IoUtils.safeClose(connection);
}
});
}
}
use of io.undertow.protocols.ssl.UndertowXnioSsl in project light-4j by networknt.
the class Http2ClientTest method testSingleHttp2PostSsl.
@Test
public void testSingleHttp2PostSsl() throws Exception {
//
final Http2Client client = createClient();
final String postMessage = "This is a post request";
final List<String> responses = new CopyOnWriteArrayList<>();
final CountDownLatch latch = new CountDownLatch(1);
SSLContext context = client.createSSLContext();
XnioSsl ssl = new UndertowXnioSsl(worker.getXnio(), OptionMap.EMPTY, Http2Client.SSL_BUFFER_POOL, context);
final ClientConnection connection = client.connect(new URI("https://localhost:7778"), worker, ssl, Http2Client.POOL, OptionMap.create(UndertowOptions.ENABLE_HTTP2, true)).get();
try {
connection.getIoThread().execute(new Runnable() {
@Override
public void run() {
final ClientRequest request = new ClientRequest().setMethod(Methods.POST).setPath(POST);
request.getRequestHeaders().put(Headers.HOST, "localhost");
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(Http2Client.POOL) {
@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();
}
});
}
});
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals(1, responses.size());
for (final String response : responses) {
Assert.assertEquals(postMessage, response);
}
} finally {
IoUtils.safeClose(connection);
}
}
use of io.undertow.protocols.ssl.UndertowXnioSsl 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.protocols.ssl.UndertowXnioSsl 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);
}
}
Aggregations