WebSocketEmulatedHandler extends WebSocketHandlerAdapter { @Override public synchronized void processConnect(WebSocketChannel channel, WSURI location, String[] protocols) { LOG.entering(CLASS_NAME, "connect", channel); String path = location.getPath(); if (path.endsWith("/")) { path = path.substring(0, path.length()-1); } try { CreateChannel createChannel = new CreateChannel(); createChannel.setParent(channel); createChannel.setProtocols(protocols); HttpURI createUri = HttpURI.replaceScheme(location, location.getHttpEquivalentScheme()) .replacePath(path + "/;e/cbm"); createHandler.processOpen(createChannel, createUri); } catch (Exception e) { LOG.log(Level.FINE, e.getMessage(), e); listener.connectionFailed(channel, e); } } WebSocketEmulatedHandler(); @Override synchronized void processConnect(WebSocketChannel channel, WSURI location, String[] protocols); @Override synchronized void processClose(WebSocketChannel channel, int code, String reason); @Override void processTextMessage(WebSocketChannel channel, String message); @Override void processBinaryMessage(WebSocketChannel channel, WrappedByteBuffer message);  }
@Test public void testProcessOpen() throws URISyntaxException { Mockery context = new Mockery(); final CreateHandler createHandler = context.mock(CreateHandler.class); final UpstreamHandler upstreamHandler = context.mock(UpstreamHandler.class); final DownstreamHandler downstreamHandler = context.mock(DownstreamHandler.class); final WebSocketHandlerListener listener = context.mock(WebSocketHandlerListener.class); CreateHandlerFactory defaultCreateHandlerFactory = WebSocketEmulatedHandler.createHandlerFactory; UpstreamHandlerFactory defaultUpstreamHandlerFactory = WebSocketEmulatedHandler.upstreamHandlerFactory; DownstreamHandlerFactory defaultDownstreamHandlerFactory = WebSocketEmulatedHandler.downstreamHandlerFactory; context.checking(new Expectations() { { oneOf(createHandler).setListener(with(aNonNull(CreateHandlerListener.class))); will(saveParameter("createListener", 0)); oneOf(downstreamHandler).setListener(with(aNonNull(DownstreamHandlerListener.class))); will(saveParameter("downstreamListener", 0)); oneOf(upstreamHandler).setListener(with(aNonNull(UpstreamHandlerListener.class))); will(saveParameter("upstreamListener", 0)); oneOf(createHandler).processOpen(with(aNonNull(CreateChannel.class)), with(aNonNull(HttpURI.class))); will(new CustomAction("will fire createCompleted") { @Override public Object invoke(Invocation invocation) throws URISyntaxException { CreateHandlerListener listener = (CreateHandlerListener)lookup("createListener"); CreateChannel channel = (CreateChannel)invocation.getParameter(0); listener.createCompleted(channel, new HttpURI("http: return null; } }); oneOf(downstreamHandler).processConnect(with(aNonNull(DownstreamChannel.class)), with(aNonNull(HttpURI.class))); will(new CustomAction("will fire downstream connected") { @Override public Object invoke(Invocation invocation) throws URISyntaxException { DownstreamHandlerListener listener = (DownstreamHandlerListener)lookup("downstreamListener"); DownstreamChannel channel = (DownstreamChannel)invocation.getParameter(0); listener.downstreamOpened(channel); return null; } }); oneOf(listener).connectionOpened(with(aNonNull(WebSocketChannel.class)), with("foo")); } }); WSURI uri = new WSURI("ws: WebSocketEmulatedChannel channel = new WebSocketEmulatedChannel(uri); WebSocketEmulatedHandler.createHandlerFactory = new CreateHandlerFactory() { @Override public CreateHandler createCreateHandler() { return createHandler; } }; WebSocketEmulatedHandler.upstreamHandlerFactory = new UpstreamHandlerFactory() { @Override public UpstreamHandler createUpstreamHandler() { return upstreamHandler; } }; WebSocketEmulatedHandler.downstreamHandlerFactory = new DownstreamHandlerFactory() { @Override public DownstreamHandler createDownstreamHandler() { return downstreamHandler; } }; WebSocketEmulatedHandler handler = new WebSocketEmulatedHandler(); handler.setListener(listener); handler.processConnect(channel, uri, new String[]{"foo"}); context.assertIsSatisfied(); WebSocketEmulatedHandler.createHandlerFactory = defaultCreateHandlerFactory; WebSocketEmulatedHandler.upstreamHandlerFactory = defaultUpstreamHandlerFactory; WebSocketEmulatedHandler.downstreamHandlerFactory = defaultDownstreamHandlerFactory; }
@Test public void testProcessFailed() throws URISyntaxException { Mockery context = new Mockery(); final CreateHandler createHandler = context.mock(CreateHandler.class); final UpstreamHandler upstreamHandler = context.mock(UpstreamHandler.class); final DownstreamHandler downstreamHandler = context.mock(DownstreamHandler.class); final WebSocketHandlerListener listener = context.mock(WebSocketHandlerListener.class); CreateHandlerFactory defaultCreateHandlerFactory = WebSocketEmulatedHandler.createHandlerFactory; UpstreamHandlerFactory defaultUpstreamHandlerFactory = WebSocketEmulatedHandler.upstreamHandlerFactory; DownstreamHandlerFactory defaultDownstreamHandlerFactory = WebSocketEmulatedHandler.downstreamHandlerFactory; context.checking(new Expectations() { { oneOf(createHandler).setListener(with(aNonNull(CreateHandlerListener.class))); will(saveParameter("createListener", 0)); oneOf(downstreamHandler).setListener(with(aNonNull(DownstreamHandlerListener.class))); will(saveParameter("downstreamListener", 0)); oneOf(upstreamHandler).setListener(with(aNonNull(UpstreamHandlerListener.class))); will(saveParameter("upstreamListener", 0)); oneOf(createHandler).processOpen(with(aNonNull(CreateChannel.class)), with(aNonNull(HttpURI.class))); will(new CustomAction("will fire createCompleted") { @Override public Object invoke(Invocation invocation) throws URISyntaxException { CreateHandlerListener listener = (CreateHandlerListener)lookup("createListener"); CreateChannel channel = (CreateChannel)invocation.getParameter(0); listener.createFailed(channel, new IllegalStateException()); return null; } }); oneOf(listener).connectionFailed(with(aNonNull(WebSocketChannel.class)), with(aNonNull(Exception.class))); } }); WSURI uri = new WSURI("ws: String[] protocols = new String[] {"foo"}; WebSocketEmulatedChannel channel = new WebSocketEmulatedChannel(uri); WebSocketEmulatedHandler.createHandlerFactory = new CreateHandlerFactory() { @Override public CreateHandler createCreateHandler() { return createHandler; } }; WebSocketEmulatedHandler.upstreamHandlerFactory = new UpstreamHandlerFactory() { @Override public UpstreamHandler createUpstreamHandler() { return upstreamHandler; } }; WebSocketEmulatedHandler.downstreamHandlerFactory = new DownstreamHandlerFactory() { @Override public DownstreamHandler createDownstreamHandler() { return downstreamHandler; } }; WebSocketEmulatedHandler handler = new WebSocketEmulatedHandler(); handler.setListener(listener); handler.processConnect(channel, uri, protocols); context.assertIsSatisfied(); WebSocketEmulatedHandler.createHandlerFactory = defaultCreateHandlerFactory; WebSocketEmulatedHandler.upstreamHandlerFactory = defaultUpstreamHandlerFactory; WebSocketEmulatedHandler.downstreamHandlerFactory = defaultDownstreamHandlerFactory; }