def test_resolve_top_level_list_attribute(self): ws_message = WSMessage().with_attribute("colour", "${/0/colour}") response = [ {"colour": "red"}, {"colour": "green"}, {"colour": "blue"} ] expected_value = "{\"colour\": \"red\"}" ws_message = ws_message.resolve(response) self.assertEqual(expected_value, str(ws_message))
def test_add_key_value_message(self): message = WSMessage().with_attribute("test", 123) ws_tester = (WSTest("wss://example.com").with_message(message)) self.assertEqual(1, len(ws_tester.messages)) self.assertEqual(message, ws_tester.messages[0])
async def test_websocket_senfing_message_with_no_delay( self, mock_websockets, mock_sleep): ws_tester = (WSTest("ws://example.com").with_message( WSMessage().with_attribute("test", 123))) mock_socket = MagicMock() mock_socket.close = MagicMock(return_value=asyncio.Future()) mock_socket.close.return_value.set_result(MagicMock()) send_future = asyncio.Future() send_future.set_result({}) mock_socket.send = MagicMock(return_value=send_future) mock_socket.recv = MagicMock(return_value=asyncio.Future()) mock_socket.recv.return_value.set_result(MagicMock()) mock_websockets.return_value = asyncio.Future() mock_websockets.return_value.set_result(mock_socket) await ws_tester.run() self.assertTrue(ws_tester.is_complete()) mock_socket.send.assert_called_once_with("{\"test\": 123}") mock_sleep.assert_not_called() mock_socket.close.assert_called_once()
async def test_websocket_test_receive_response_with_unresolved_trigger( self, mock_ssl, mock_websockets): ws_tester = (WSTest("wss://example.com").with_response( WSResponse().with_attribute("type").with_trigger( WSMessage().with_attribute("test", "${body}")))) mock_socket = MagicMock() mock_socket.close = MagicMock(return_value=asyncio.Future()) mock_socket.close.return_value.set_result(MagicMock()) send_future = asyncio.Future() send_future.set_result({}) mock_socket.send = MagicMock(return_value=send_future) receive_future = asyncio.Future() receive_future.set_result(json.dumps({"type": "Hello, world!"})) mock_socket.recv = MagicMock( side_effect=[receive_future, asyncio.Future()]) mock_websockets.return_value = asyncio.Future() mock_websockets.return_value.set_result(mock_socket) ssl_context = MagicMock() mock_ssl.return_value = ssl_context await ws_tester.run() self.assertTrue(ws_tester.is_complete()) mock_socket.send.assert_called_once_with("{\"test\": \"${body}\"}") mock_socket.close.assert_called_once()
async def test_websocket_test_send_single_message(self, mock_ssl, mock_websockets): message = WSMessage().with_attribute("test", 123) ws_tester = WSTest("wss://example.com").with_message(message) mock_socket = MagicMock() mock_socket.close = MagicMock(return_value=asyncio.Future()) mock_socket.close.return_value.set_result(MagicMock()) future = asyncio.Future() mock_socket.send = MagicMock(return_value=future) future.set_result({}) mock_websockets.return_value = asyncio.Future() mock_websockets.return_value.set_result(mock_socket) ssl_context = MagicMock() mock_ssl.return_value = ssl_context await ws_tester.run() self.assertFalse(ws_tester.messages) self.assertEqual(1, len(ws_tester.sent_messages)) mock_socket.send.assert_called_once_with("{\"test\": 123}") mock_socket.close.assert_called_once()
def test_with_delay(self): ws_message = ( WSMessage() .with_attribute("test", 123) .with_delay(0.1) ) self.assertEqual(0.1, ws_message.delay)
def test_resolve_attribute(self): ws_message = ( WSMessage() .with_attribute("test", 123) .with_attribute("example", "${body/example}") ) response = { "type": "message", "body": { "example": 456 } } expected_value = "{\"test\": 123, \"example\": 456}" ws_message = ws_message.resolve(response) self.assertEqual(expected_value, str(ws_message))
async def test_websocket_message_timeout(self, mock_ssl, mock_websockets): ws_tester = ( WSTest("wss://example.com").with_message_timeout(0.1).with_message( WSMessage().with_attribute("test", 123))) mock_socket = MagicMock() mock_socket.close = MagicMock(return_value=asyncio.Future()) mock_socket.close.return_value.set_result(MagicMock()) mock_socket.send = MagicMock(return_value=asyncio.Future()) mock_websockets.return_value = asyncio.Future() mock_websockets.return_value.set_result(mock_socket) ssl_context = MagicMock() mock_ssl.return_value = ssl_context self.assertEqual(ws_tester.message_timeout, 0.1) with self.assertRaises(WSTimeoutError): await ws_tester.run() mock_socket.close.assert_called_once()
def test_create_ws_message(self): ws_message = WSMessage() self.assertDictEqual({}, ws_message.attributes)
def test_stringify_with_attribute(self): ws_message = WSMessage().with_attribute("test", 123) expected_value = "{\"test\": 123}" self.assertEqual(expected_value, str(ws_message))
def test_with_attribute(self): ws_message = WSMessage().with_attribute("test", 123) self.assertIn("test", ws_message.attributes) self.assertEqual(1, len(ws_message.attributes))
def test_with_trigger(self): message = WSMessage().with_attribute("test", 123) ws_response = WSResponse().with_trigger(message) self.assertEqual(1, len(ws_response.triggers)) self.assertEqual(message, ws_response.triggers[0])