@Test(timeOut = 60000) public void onCloseWithCodeServerClose() throws Throwable { try (AsyncHttpClient client = getAsyncHttpClient(null)) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<String> text = new AtomicReference<>(""); client.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new Listener(latch, text)).build()).get(); latch.await(); final String[] parts = text.get().split(" "); assertEquals(text.get(), "1001-Idle Timeout"); } }
@Override @Test public void onCloseWithCode() throws Throwable { super.onCloseWithCode(); // To change body of overridden methods use File | Settings | File Templates. } }
@Test(timeOut = 60000, expectedExceptions = { IllegalStateException.class, IllegalArgumentException.class } ) public void wrongStatusCode() throws Throwable { try (AsyncHttpClient client = getAsyncHttpClient(null)) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Throwable> throwable = new AtomicReference<>(); client.prepareGet("http://apache.org").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() { @Override public void onMessage(String message) { } @Override public void onOpen(com.ning.http.client.ws.WebSocket websocket) { } @Override public void onClose(com.ning.http.client.ws.WebSocket websocket) { } @Override public void onError(Throwable t) { throwable.set(t); latch.countDown(); } }).build()); latch.await(); assertNotNull(throwable.get()); throw throwable.get(); } }
@Test(timeOut = 60000, expectedExceptions = { IllegalStateException.class, IllegalArgumentException.class } ) public void wrongProtocolCode() throws Throwable { try (AsyncHttpClient client = getAsyncHttpClient(null)) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<Throwable> throwable = new AtomicReference<>();
@Test(timeOut = 60000) public void onCloseWithCode() throws Throwable { try (AsyncHttpClient client = getAsyncHttpClient(null)) { final CountDownLatch latch = new CountDownLatch(1); final AtomicReference<String> text = new AtomicReference<>(""); WebSocket websocket = client.prepareGet(getTargetUrl()).execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new Listener(latch, text)).build()).get(); websocket.close(); latch.await(); assertTrue(text.get().startsWith("1000")); } }
@Test(timeOut = 60000, expectedExceptions = { ExecutionException.class }) public void getWebSocketThrowsException() throws Throwable { final CountDownLatch latch = new CountDownLatch(1); try (AsyncHttpClient client = getAsyncHttpClient(null)) { client.prepareGet("http://apache.org").execute(new WebSocketUpgradeHandler.Builder().addWebSocketListener(new WebSocketTextListener() { @Override public void onMessage(String message) { } @Override public void onOpen(com.ning.http.client.ws.WebSocket websocket) { } @Override public void onClose(com.ning.http.client.ws.WebSocket websocket) { } @Override public void onError(Throwable t) { latch.countDown(); } }).build()).get(); } latch.await(); }