Search in sources :

Example 21 with AsyncHttpClient

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());
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 22 with AsyncHttpClient

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());
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 23 with AsyncHttpClient

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");
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 24 with AsyncHttpClient

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();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient) Test(org.testng.annotations.Test)

Example 25 with AsyncHttpClient

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");
    }
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ProxyServer(org.asynchttpclient.proxy.ProxyServer) AsyncHttpClient(org.asynchttpclient.AsyncHttpClient)

Aggregations

AsyncHttpClient (org.asynchttpclient.AsyncHttpClient)146 Test (org.testng.annotations.Test)119 Response (org.asynchttpclient.Response)71 AbstractBasicTest (org.asynchttpclient.AbstractBasicTest)66 HttpServletResponse (javax.servlet.http.HttpServletResponse)40 CountDownLatch (java.util.concurrent.CountDownLatch)31 DefaultAsyncHttpClient (org.asynchttpclient.DefaultAsyncHttpClient)26 AtomicReference (java.util.concurrent.atomic.AtomicReference)23 RequestBuilder (org.asynchttpclient.RequestBuilder)16 IOException (java.io.IOException)14 RouteBuilder (org.apache.camel.builder.RouteBuilder)14 ExecutionException (java.util.concurrent.ExecutionException)13 Request (org.asynchttpclient.Request)13 WebSocket (org.asynchttpclient.ws.WebSocket)12 Test (org.junit.Test)11 AsyncHttpClientConfig (org.asynchttpclient.AsyncHttpClientConfig)10 File (java.io.File)9 WebSocketTextListener (org.asynchttpclient.ws.WebSocketTextListener)9 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)8 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)7