WebSocketNativeHandshakeHandler extends WebSocketHandlerAdapter { @Override public void setNextHandler(final WebSocketHandler handler) { super.setNextHandler(handler); nextHandler.setListener(new WebSocketHandlerListener() { @Override public void connectionOpened(WebSocketChannel channel, String supportProtocol) { if (WebSocketHandshakeObject.KAAZING_EXTENDED_HANDSHAKE.equals(supportProtocol)) { sendHandshakePayload(channel, null); } else { listener.connectionOpened(channel, channel.getProtocol()); } } @Override public void redirected(WebSocketChannel channel, String location) { listener.redirected(channel, location); } @Override public void authenticationRequested(WebSocketChannel channel, String location, String challenge) { listener.authenticationRequested(channel, location, challenge); } @Override public void binaryMessageReceived(WebSocketChannel channel, WrappedByteBuffer buf) { WebSocketSelectedChannel selectedChannel = (WebSocketSelectedChannel) channel; if (selectedChannel.getReadyState() == ReadyState.OPEN) { listener.binaryMessageReceived(channel, buf); } else { handleHandshakeMessage(channel, buf); } } @Override public void textMessageReceived(WebSocketChannel channel, String message) { listener.textMessageReceived(channel, message); } @Override public void commandMessageReceived(WebSocketChannel channel, CommandMessage message) { listener.commandMessageReceived(channel, message); } @Override public void connectionClosed(WebSocketChannel channel, boolean wasClean, int code, String reason) { listener.connectionClosed(channel, wasClean, code, reason); } @Override public void connectionClosed(WebSocketChannel channel, Exception ex) { listener.connectionClosed(channel, ex); } @Override public void connectionFailed(WebSocketChannel channel, Exception ex) { listener.connectionClosed(channel, false, 0, null); } }); } WebSocketNativeHandshakeHandler(); @Override void processConnect(WebSocketChannel channel, WSURI uri, String[] protocols); @Override void processAuthorize(WebSocketChannel channel, String authorizeToken); @Override void setNextHandler(final WebSocketHandler handler);  }
@Test @Ignore(" channel readyState must be Open for this test to run") public void testProcessBinaryMessage() throws URISyntaxException, Exception { Mockery context = new Mockery(); final WebSocketHandler nextHandler = context.mock(WebSocketHandler.class); final WebSocketHandlerListener listener = context.mock(WebSocketHandlerListener.class); final WrappedByteBuffer message = new WrappedByteBuffer("test message".getBytes()); context.checking(new Expectations() { { oneOf(nextHandler).setListener(with(aNonNull(WebSocketHandlerListener.class))); will(saveParameter("listener", 0)); oneOf(nextHandler).processBinaryMessage(with(aNonNull(WebSocketChannel.class)), with(message)); will(new CustomAction("will fire text message received") { @Override public Object invoke(Invocation invocation) { WebSocketHandlerListener listener = (WebSocketHandlerListener)lookup("listener"); WebSocketChannel channel = (WebSocketChannel)invocation.getParameter(0); listener.connectionOpened(channel, null); listener.binaryMessageReceived(channel, message); return null; } }); oneOf(listener).binaryMessageReceived(with(aNonNull(WebSocketChannel.class)), with(message)); } }); WSURI uri = new WSURI("ws: WebSocketChannel channel = createNativeChannel(uri); WebSocketNativeHandshakeHandler handler = new WebSocketNativeHandshakeHandler(); handler.setNextHandler(nextHandler); handler.setListener(listener); handler.processBinaryMessage(channel, message); context.assertIsSatisfied(); }
@Test @Ignore(" channel readyState must be Open for this test to run") public void testProcessTextMessage() throws URISyntaxException, Exception { Mockery context = new Mockery(); final WebSocketHandler nextHandler = context.mock(WebSocketHandler.class); final WebSocketHandlerListener listener = context.mock(WebSocketHandlerListener.class); final WrappedByteBuffer message = new WrappedByteBuffer("test message".getBytes()); context.checking(new Expectations() { { oneOf(nextHandler).setListener(with(aNonNull(WebSocketHandlerListener.class))); will(saveParameter("listener", 0)); oneOf(nextHandler).processTextMessage(with(aNonNull(WebSocketChannel.class)), with("test message")); 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, null); listener.binaryMessageReceived(channel, message); return null; } }); oneOf(listener).binaryMessageReceived(with(aNonNull(WebSocketChannel.class)), with(new ByteBufferMatcher("test message", "test"))); } }); WSURI uri = new WSURI("ws: WebSocketChannel channel = createNativeChannel(uri); WebSocketNativeHandshakeHandler handler = new WebSocketNativeHandshakeHandler(); handler.setNextHandler(nextHandler); handler.setListener(listener); handler.processTextMessage(channel, "test message"); context.assertIsSatisfied(); }