def test_send_message_with_protocol(self): """ Ensures that the message is translated into a dictionary and passed into the protocol object in the expected way. """ nc = NetstringConnector(self.event_loop) protocol = mock.MagicMock() protocol.send_string = mock.MagicMock() msg = OK('uuid', 'recipient', 'sender', 9999, 'version', 'seal') nc._send_message_with_protocol(msg, protocol) expected = { 'message': 'ok', 'uuid': 'uuid', 'recipient': 'recipient', 'sender': 'sender', 'reply_port': 9999, 'version': 'version', 'seal': 'seal' } actual = json.loads(protocol.send_string.call_args[0][0]) self.assertEqual(expected, actual)
def test_send_with_cached_protocol(self): """ Send the message to the referenced contact using a cached protocol object. """ nc = NetstringConnector(self.event_loop) nc._send_message_with_protocol = mock.MagicMock() contact = PeerNode(PUBLIC_KEY, self.version, 'netstring://192.168.0.1:1908') msg = OK('uuid', 'recipient', 'sender', 9999, 'version', 'seal') protocol = mock.MagicMock() sender = Node(PUBLIC_KEY, PRIVATE_KEY, self.event_loop, nc, 1908) nc._connections[contact.network_id] = protocol result = nc.send(contact, msg, sender) self.assertIsInstance(result, asyncio.Future) self.assertTrue(result.done()) self.assertEqual(result.result(), True) nc._send_message_with_protocol.assert_called_once_with(msg, protocol)