WebSocketNativeAuthenticationHandler extends WebSocketHandlerAdapter { @Override public void setNextHandler(WebSocketHandler handler) { super.setNextHandler(handler); handler.setListener(new WebSocketHandlerListener() { @Override public void connectionOpened(WebSocketChannel channel, String protocol) { clearAuthenticationCredentials(channel); listener.connectionOpened(channel, protocol); } @Override public void redirected(WebSocketChannel channel, String location) { clearAuthenticationCredentials(channel); listener.redirected(channel, location); } @Override public void authenticationRequested(WebSocketChannel channel, String location, String challenge) { handleAuthenticationRequested(channel, location, challenge); } @Override public void binaryMessageReceived(WebSocketChannel channel, WrappedByteBuffer buf) { listener.binaryMessageReceived(channel, buf); } @Override public void textMessageReceived(WebSocketChannel channel, String message) { listener.textMessageReceived(channel, message); } @Override public void connectionClosed(WebSocketChannel channel, boolean wasClean, int code, String reason) { clearAuthenticationCredentials(channel); 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) { clearAuthenticationCredentials(channel); listener.connectionFailed(channel, ex); } @Override public void commandMessageReceived(WebSocketChannel channel, CommandMessage message) { } }); }  @Override 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"}; 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("")); } }); WebSocketFactory wsFactory = WebSocketFactory.createWebSocketFactory(); ChallengeHandler ch = new DummyChallellengeHandler(); wsFactory.setDefaultChallengeHandler(ch); WebSocket ws = wsFactory.createWebSocket(URI.create("ws: WSURI uri = new WSURI("ws: WebSocketNativeChannel channel = new WebSocketNativeChannel(uri); channel.setParent(((WebSocketImpl)ws).getCompositeChannel()); ((WebSocketImpl)ws).getCompositeChannel().setChallengeHandler(ch); WebSocketNativeAuthenticationHandler handler = new WebSocketNativeAuthenticationHandler(); handler.setNextHandler(nextHandler); handler.setListener(listener); handler.processConnect(channel, uri, requestedProtocols); context.assertIsSatisfied(); }
@Test public void testProcessAuthorizeWithoutChallengeHandler() throws URISyntaxException { Mockery context = new Mockery(); final WebSocketHandler nextHandler = context.mock(WebSocketHandler.class); final WebSocketHandlerListener listener = context.mock(WebSocketHandlerListener.class); context.checking(new Expectations() { { oneOf(nextHandler).setListener(with(aNonNull(WebSocketHandlerListener.class))); will(saveParameter("listener", 0)); oneOf(nextHandler).processTextMessage(with(aNonNull(WebSocketChannel.class)), with("Application Basic")); will(new CustomAction("will fire 401 challenge") { @Override public Object invoke(Invocation invocation) { WebSocketHandlerListener listener = (WebSocketHandlerListener)lookup("listener"); WebSocketNativeChannel channel = (WebSocketNativeChannel)invocation.getParameter(0); listener.authenticationRequested(channel, "ws: return null; } }); oneOf(nextHandler).processClose(with(aNonNull(WebSocketChannel.class)), with(1000), with(aNull(String.class))); oneOf(listener).connectionClosed(with(aNonNull(WebSocketChannel.class)), with(aNonNull(Exception.class))); } }); WebSocketFactory wsFactory = WebSocketFactory.createWebSocketFactory(); ChallengeHandler ch = new DummyChallellengeHandler(); wsFactory.setDefaultChallengeHandler(ch); WebSocket ws = wsFactory.createWebSocket(URI.create("ws: WSURI uri = new WSURI("ws: WebSocketNativeChannel channel = new WebSocketNativeChannel(uri); channel.setParent(((WebSocketImpl)ws).getCompositeChannel()); ((WebSocketImpl)ws).getCompositeChannel().setChallengeHandler(ch); WebSocketNativeAuthenticationHandler handler = new WebSocketNativeAuthenticationHandler(); handler.setNextHandler(nextHandler); handler.setListener(listener); nextHandler.processTextMessage(channel, "Application Basic"); context.assertIsSatisfied(); }
@Test public void testProcessAuthorizeWithChallengeHandler() throws URISyntaxException { Mockery context = new Mockery(); final WebSocketHandler nextHandler = context.mock(WebSocketHandler.class); final WebSocketHandlerListener listener = context.mock(WebSocketHandlerListener.class); context.checking(new Expectations() { { oneOf(nextHandler).setListener(with(aNonNull(WebSocketHandlerListener.class))); will(saveParameter("listener", 0)); oneOf(nextHandler).processTextMessage(with(aNonNull(WebSocketChannel.class)), with("Application Basic")); will(new CustomAction("will fire 401 challenge") { @Override public Object invoke(Invocation invocation) { WebSocketHandlerListener listener = (WebSocketHandlerListener)lookup("listener"); WebSocketChannel channel = (WebSocketChannel)invocation.getParameter(0); listener.authenticationRequested(channel, "ws: return null; } }); oneOf(nextHandler).processAuthorize(with(aNonNull(WebSocketChannel.class)), with("Basic am9lOndlbGNvbWU=")); } }); WebSocketFactory wsFactory = WebSocketFactory.createWebSocketFactory(); ChallengeHandler ch = BasicChallengeHandler.create().setLoginHandler(new MyLoginHandler()); wsFactory.setDefaultChallengeHandler(ch); WebSocket ws = wsFactory.createWebSocket(URI.create("ws: WSURI uri = new WSURI("ws: WebSocketNativeChannel channel = new WebSocketNativeChannel(uri); channel.setParent(((WebSocketImpl)ws).getCompositeChannel()); ((WebSocketImpl)ws).getCompositeChannel().setChallengeHandler(ch); WebSocketNativeAuthenticationHandler handler = new WebSocketNativeAuthenticationHandler(); handler.setNextHandler(nextHandler); handler.setListener(listener); nextHandler.processTextMessage(channel, "Application Basic"); context.assertIsSatisfied(); wsFactory.setDefaultChallengeHandler(new DummyChallellengeHandler()); }
@Test public void testProcessBinaryMessage() throws URISyntaxException { 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.binaryMessageReceived(channel, message); return null; } }); oneOf(listener).binaryMessageReceived(with(aNonNull(WebSocketChannel.class)), with(message)); } }); WSURI uri = new WSURI("ws: WebSocketNativeChannel channel = new WebSocketNativeChannel(uri); WebSocketNativeAuthenticationHandler handler = new WebSocketNativeAuthenticationHandler(); handler.setNextHandler(nextHandler); handler.setListener(listener); handler.processBinaryMessage(channel, message); context.assertIsSatisfied(); }
@Test public void testProcessTextMessage() throws URISyntaxException { 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.binaryMessageReceived(channel, message); return null; } }); oneOf(listener).binaryMessageReceived(with(aNonNull(WebSocketChannel.class)), with(new ByteBufferMatcher("test message", "test"))); } }); WSURI uri = new WSURI("ws: WebSocketNativeChannel channel = new WebSocketNativeChannel(uri); WebSocketNativeAuthenticationHandler handler = new WebSocketNativeAuthenticationHandler(); handler.setNextHandler(nextHandler); handler.setListener(listener); handler.processTextMessage(channel, "test message"); context.assertIsSatisfied(); }