SocketIoClient { void sendPacket(final Packet packet) { if (mConnection.getReadyState() == ReadyState.OPEN) { mEncoder.encode(packet, objects -> { for (Object item : objects) { final io.socket.engineio.parser.Packet engineIoPacket = new io.socket.engineio.parser.Packet(io.socket.engineio.parser.Packet.MESSAGE); engineIoPacket.data = item; mConnection.send(engineIoPacket); } }); } } SocketIoClient(SocketIoServer server, EngineIoSocket connection);   }
@Test public void test_sendPacket_nonbinary() { final EngineIoServer engineIoServer = new EngineIoServer(); final SocketIoServer socketIoServer = new SocketIoServer(engineIoServer); final EngineIoWebSocket webSocket = Mockito.spy(new EngineIoWebSocketImpl()); final Emitter.Listener connectionListener = Mockito.mock(Emitter.Listener.class); Mockito.doAnswer(invocation -> { final Object[] args = invocation.getArguments(); final EngineIoSocket socket = (EngineIoSocket) args[0]; final SocketIoClient client = new SocketIoClient(socketIoServer, socket); final JSONArray packetData = new JSONArray(); packetData.put("foo"); packetData.put(1); final Packet<JSONArray> packet = new Packet<>(Parser.EVENT, packetData); try { Mockito.reset(webSocket); client.sendPacket(packet); Mockito.verify(webSocket, Mockito.times(1)) .write(Mockito.anyString()); } catch (IOException ignore) { } return null; }).when(connectionListener).call(Mockito.any()); engineIoServer.on("connection", connectionListener); engineIoServer.handleWebSocket(webSocket); Mockito.verify(connectionListener, Mockito.times(1)) .call(Mockito.any(EngineIoSocket.class)); }
@Test public void test_sendPacket_binary() { final EngineIoServer engineIoServer = new EngineIoServer(); final SocketIoServer socketIoServer = new SocketIoServer(engineIoServer); final EngineIoWebSocket webSocket = Mockito.spy(new EngineIoWebSocketImpl()); final Emitter.Listener connectionListener = Mockito.mock(Emitter.Listener.class); Mockito.doAnswer(invocation -> { final Object[] args = invocation.getArguments(); final EngineIoSocket socket = (EngineIoSocket) args[0]; final SocketIoClient client = new SocketIoClient(socketIoServer, socket); final JSONArray packetData = new JSONArray(); packetData.put("foo"); packetData.put(1); packetData.put(new byte[16]); final Packet<JSONArray> packet = new Packet<>(Parser.EVENT, packetData); try { Mockito.reset(webSocket); client.sendPacket(packet); Mockito.verify(webSocket, Mockito.times(1)) .write(Mockito.anyString()); Mockito.verify(webSocket, Mockito.times(1)) .write(Mockito.any(byte[].class)); } catch (IOException ignore) { } return null; }).when(connectionListener).call(Mockito.any()); engineIoServer.on("connection", connectionListener); engineIoServer.handleWebSocket(webSocket); Mockito.verify(connectionListener, Mockito.times(1)) .call(Mockito.any(EngineIoSocket.class)); }