use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ByteMessageTest method echoTwoMessagesTest.
@Test(groups = "standalone")
public void echoTwoMessagesTest() throws Exception {
try (AsyncHttpClient c = asyncHttpClient()) {
final CountDownLatch latch = new CountDownLatch(2);
final AtomicReference<byte[]> text = new AtomicReference<>(null);
WebSocket websocket = c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketByteListener() {
@Override
public void onOpen(WebSocket websocket) {
}
@Override
public void onClose(WebSocket websocket) {
latch.countDown();
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
latch.countDown();
}
@Override
public void onMessage(byte[] message) {
if (text.get() == null) {
text.set(message);
} else {
byte[] n = new byte[text.get().length + message.length];
System.arraycopy(text.get(), 0, n, 0, text.get().length);
System.arraycopy(message, 0, n, text.get().length, message.length);
text.set(n);
}
latch.countDown();
}
}).build()).get();
websocket.sendMessage("ECHO".getBytes()).sendMessage("ECHO".getBytes());
latch.await();
assertEquals(text.get(), "ECHOECHO".getBytes());
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ByteMessageTest method echoByte.
@Test(groups = "standalone")
public void echoByte() throws Exception {
try (AsyncHttpClient c = asyncHttpClient()) {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<byte[]> text = new AtomicReference<>(new byte[0]);
WebSocket websocket = c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketByteListener() {
@Override
public void onOpen(WebSocket websocket) {
}
@Override
public void onClose(WebSocket websocket) {
latch.countDown();
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
latch.countDown();
}
@Override
public void onMessage(byte[] message) {
text.set(message);
latch.countDown();
}
}).build()).get();
websocket.sendMessage("ECHO".getBytes());
latch.await();
assertEquals(text.get(), "ECHO".getBytes());
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class CloseCodeReasonMessageTest method onCloseWithCodeServerClose.
@Test(groups = "standalone", timeOut = 60000)
public void onCloseWithCodeServerClose() throws Exception {
try (AsyncHttpClient c = asyncHttpClient()) {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> text = new AtomicReference<>("");
c.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new Listener(latch, text)).build()).get();
latch.await();
assertEquals(text.get(), "1001-Idle Timeout");
}
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class CloseCodeReasonMessageTest method getWebSocketThrowsException.
@Test(groups = "online", timeOut = 60000, expectedExceptions = ExecutionException.class)
public void getWebSocketThrowsException() throws Throwable {
final CountDownLatch latch = new CountDownLatch(1);
try (AsyncHttpClient client = asyncHttpClient()) {
client.prepareGet("http://apache.org").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
@Override
public void onMessage(String message) {
}
@Override
public void onOpen(WebSocket websocket) {
}
@Override
public void onClose(WebSocket websocket) {
}
@Override
public void onError(Throwable t) {
latch.countDown();
}
}).build()).get();
}
latch.await();
}
use of org.asynchttpclient.AsyncHttpClient in project async-http-client by AsyncHttpClient.
the class ProxyTunnellingTest method runTest.
private void runTest(boolean secure) throws Exception {
setUpServers(secure);
String targetUrl = String.format("%s://localhost:%d/", secure ? "wss" : "ws", port2);
// CONNECT happens over HTTP, not HTTPS
ProxyServer ps = proxyServer("localhost", port1).build();
try (AsyncHttpClient asyncHttpClient = asyncHttpClient(config().setProxyServer(ps).setUseInsecureTrustManager(true))) {
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> text = new AtomicReference<>("");
WebSocket websocket = asyncHttpClient.prepareGet(targetUrl).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() {
@Override
public void onMessage(String message) {
text.set(message);
latch.countDown();
}
@Override
public void onOpen(WebSocket websocket) {
}
@Override
public void onClose(WebSocket websocket) {
latch.countDown();
}
@Override
public void onError(Throwable t) {
t.printStackTrace();
latch.countDown();
}
}).build()).get();
websocket.sendMessage("ECHO");
latch.await();
assertEquals(text.get(), "ECHO");
}
}
Aggregations