async def test_websocket_receives_and_handles_multiple_responses( self, mock_ssl, mock_websockets): first_response = WSResponse().with_attribute('body') second_response = WSResponse().with_attribute('type') ws_tester = (WSTest('wss://example.com').with_parameter( 'example', 123).with_response(first_response).with_response(second_response)) mock_socket = MagicMock() mock_socket.close = MagicMock(return_value=asyncio.Future()) mock_socket.close.return_value.set_result(MagicMock()) first_future = asyncio.Future() first_future.set_result(json.dumps({'body': {}})) second_future = asyncio.Future() second_future.set_result(json.dumps({'type': {}})) mock_socket.recv = MagicMock( side_effect=[second_future, first_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.assertEqual(2, len(ws_tester.received_json)) self.assertEqual(2, len(ws_tester.received_responses)) self.assertTrue(ws_tester.is_complete()) mock_socket.close.assert_called_once()
async def test_websocket_receives_and_handles_single_response( self, mock_ssl, mock_websockets): response = WSResponse().with_attribute('body') ws_tester = (WSTest('wss://example.com').with_parameter( 'example', 123).with_response(response)) mock_socket = MagicMock() mock_socket.close = MagicMock(return_value=asyncio.Future()) mock_socket.close.return_value.set_result(MagicMock()) response_json = json.dumps({'body': {}}) future = asyncio.Future() future.set_result(response_json) mock_socket.recv = MagicMock(side_effect=[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.assertEqual(response, ws_tester.received_responses[0]) self.assertEqual(response_json, ws_tester.received_json[0]) self.assertTrue(ws_tester.is_complete()) mock_socket.close.assert_called_once()
def test_all_attributes_is_match(self): ws_response = (WSResponse().with_attribute( 'type', 'new_request').with_attribute('body')) test_data = {'type': 'new_request', 'body': {}} self.assertTrue(ws_response.is_match(test_data))
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()
def test_no_attributes_match(self): ws_response = (WSResponse().with_attribute( 'type', 'new_request').with_attribute('body')) test_data = {'not_type': 'new_request', 'not_body': {}} self.assertFalse(ws_response.is_match(test_data))
async def test_websocket_response_timeout_with_received_response_logging_disabled( self, mock_ssl, mock_websockets): ws_tester = (WSTest('wss://example.com').with_response_timeout( 0.1).with_response(WSResponse().with_attribute('message', 'hello'))) mock_socket = MagicMock() mock_socket.close = MagicMock(return_value=asyncio.Future()) mock_socket.close.return_value.set_result(MagicMock()) first_future = asyncio.Future() first_future.set_result(json.dumps({'message': 'bye'})) mock_socket.recv = MagicMock( side_effect=[first_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 with self.assertRaises(WSTimeoutError) as ex: await ws_tester.run() expected_error = 'Timed out waiting for responses:\n{"message": "hello"}' self.assertEqual(expected_error, str(ex.exception)) mock_socket.close.assert_called_once()
def test_resolved_attribute_no_match(self): ws_response = (WSResponse().with_attribute( 'type', 'new_request').with_attribute('body/attribute', 'value')) test_data = {'type': 'new_request', 'body': {'attribute': 'not_value'}} self.assertFalse(ws_response.is_match(test_data))
def test_resolve_top_level_list_without_index(self): ws_response = WSResponse().with_attribute('//colour', 'blue') test_data = [{ 'colour': 'red' }, { 'colour': 'green' }, { 'colour': 'blue' }] self.assertTrue(ws_response.is_match(test_data))
def test_resolved_attribute_by_list_index(self): ws_response = WSResponse().with_attribute('body/0/colour', 'red') test_data = { 'body': [{ 'colour': 'red' }, { 'colour': 'green' }, { 'colour': 'blue' }] } self.assertTrue(ws_response.is_match(test_data))
def test_resolved_attribute_by_list_without_index_no_match(self): ws_response = WSResponse().with_attribute('body//colour', 'yellow') test_data = { 'body': [{ 'colour': 'red' }, { 'colour': 'green' }, { 'colour': 'blue' }] } self.assertFalse(ws_response.is_match(test_data))
def test_resolved_recursive_attribute_match(self): ws_response = WSResponse().with_attribute('body/first/second/third', 'value') test_data = { 'type': 'new_request', 'body': { 'first': { 'second': { 'third': 'value', 'fourth': 'not_value' } } } } self.assertTrue(ws_response.is_match(test_data))
async def test_websocket_test_timeout(self, mock_ssl, mock_websockets): response = WSResponse().with_attribute('body') ws_tester = (WSTest('wss://example.com').with_parameter( 'example', 123).with_test_timeout(0.1).with_response(response)) mock_socket = MagicMock() mock_socket.close = MagicMock(return_value=asyncio.Future()) mock_socket.close.return_value.set_result(MagicMock()) mock_socket.recv = 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.test_timeout, 0.1) with self.assertRaises(WSTimeoutError): await ws_tester.run() mock_socket.close.assert_called_once()
def test_create_ws_response(self): ws_response = WSResponse() self.assertDictEqual({}, ws_response.attributes)
def test_stringify(self): response = WSResponse().with_attribute('test', 123) self.assertEqual('{"test": 123}', str(response))
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])
def test_with_attribute(self): ws_response = WSResponse().with_attribute('test') self.assertIn('test', ws_response.attributes) self.assertEqual(1, len(ws_response.attributes))
def test_resolved_attribute_by_list_index_not_enough_elements(self): ws_response = WSResponse().with_attribute('body/0/colour', 'red') test_data = {'body': []} self.assertFalse(ws_response.is_match(test_data))
def test_resolve_by_key_when_list_fails(self): ws_response = WSResponse().with_attribute('body/colour', 'red') test_data = {'body': ['red', 'green', 'blue']} self.assertFalse(ws_response.is_match(test_data))
def test_resolve_by_index_when_dict_fails(self): ws_response = WSResponse().with_attribute('body/0/colour', 'red') test_data = {'body': {'colour': 'red'}} self.assertFalse(ws_response.is_match(test_data))
def test_with_attribute_with_value(self): ws_response = WSResponse().with_attribute('test', 123) self.assertEqual(123, ws_response.attributes['test']) self.assertEqual(1, len(ws_response.attributes))
def test_resolved_attribute_by_just_list_index(self): ws_response = WSResponse().with_attribute('body/0/', 'red') test_data = {'body': ['red', 'green', 'blue']} self.assertTrue(ws_response.is_match(test_data))
def test_websocket_with_expected_response(self): response = WSResponse().with_attribute('type') ws_tester = WSTest('wss://example.com').with_response(response) self.assertTrue(ws_tester.expected_responses) self.assertEqual(response, ws_tester.expected_responses[0])