WebSocketNativeBalancingHandler extends WebSocketHandlerAdapter { @Override public void processConnect(WebSocketChannel channel, WSURI uri, String[] protocols) { LOG.entering(CLASS_NAME, "connect", new Object[] { uri, protocols }); WebSocketNativeChannel wsChannel = (WebSocketNativeChannel)channel; wsChannel.balanced.set(0); nextHandler.processConnect(channel, uri.addQueryParameter(".kl=Y"), protocols); }  @Override void processConnect(WebSocketChannel channel, WSURI uri, String[] protocols); void setNextHandler(WebSocketHandler handler);  }
@Test public void testProcessConnect() throws URISyntaxException { Mockery context = new Mockery(); final WebSocketHandler nextHandler = context.mock(WebSocketHandler.class); final WebSocketHandlerListener listener = context.mock(WebSocketHandlerListener.class); final String[] requestedProtocols = new String[] { "foo" }; final String buf = '\uf0ff' + "N"; context.checking(new Expectations() { { oneOf(nextHandler).setListener(with(aNonNull(WebSocketHandlerListener.class))); will(saveParameter("listener", 0)); oneOf(nextHandler).processConnect(with(aNonNull(WebSocketChannel.class)), with(aNonNull(WSURI.class)), with(equal(requestedProtocols))); will(new CustomAction("will fire connectionOpen") { @Override public Object invoke(Invocation invocation) { WebSocketHandlerListener listener = (WebSocketHandlerListener)lookup("listener"); WebSocketChannel channel = (WebSocketChannel)invocation.getParameter(0); listener.connectionOpened(channel, ""); return null; } }); oneOf(listener).connectionOpened(with(aNonNull(WebSocketChannel.class)), with("")); will(new CustomAction("will balancer message") { @Override public Object invoke(Invocation invocation) throws UnsupportedEncodingException { WebSocketHandlerListener listener = (WebSocketHandlerListener)lookup("listener"); WebSocketChannel channel = (WebSocketChannel)invocation.getParameter(0); WrappedByteBuffer message = new WrappedByteBuffer(buf.getBytes("UTF-8")); listener.binaryMessageReceived(channel, message); return null; } }); oneOf(listener).binaryMessageReceived(with(aNonNull(WebSocketChannel.class)),with(aNonNull(WrappedByteBuffer.class))); } }); WSURI uri = new WSURI("ws: WebSocketChannel channel = new WebSocketNativeChannel(uri); WebSocketNativeBalancingHandler handler = new WebSocketNativeBalancingHandler(); handler.setNextHandler(nextHandler); handler.setListener(listener); handler.processConnect(channel, uri, requestedProtocols); context.assertIsSatisfied(); }