async def test_response_error(self): with asynctest.patch( 'synse_server.api.websocket.MessageHandler.handle_request_status' ) as mock_handler: # noqa: E501 mock_handler.side_effect = ValueError('test error') with asynctest.patch( 'websockets.WebSocketCommonProtocol.send') as mock_send: p = websocket.Payload( json.dumps(dict( id=4, event='request/status', data={}, ))) m = websocket.MessageHandler( websockets.WebSocketCommonProtocol()) await m.dispatch(p) mock_send.assert_called_once() mock_send.assert_called_with( json.dumps({ 'id': 4, 'event': 'response/error', 'data': { 'http_code': 500, 'description': 'An unexpected error occurred.', 'timestamp': '2019-04-22T13:30:00Z', 'context': 'test error', } }))
def test_init_ok(self): data = '{"id": 1, "event": "test", "data": {"foo": "bar"}}' payload = websocket.Payload(data) assert isinstance(payload, websocket.Payload) assert payload.id == 1 assert payload.event == 'test' assert payload.data == {'foo': 'bar'}
def make_payload(data, id=None, event=None): """Test utility to create a websocket payload, with some defaults.""" return websocket.Payload( json.dumps( dict( id=id or 'testing', event=event or 'testing', data=data, )))
def test_init_missing_optional(self): data = '{"id": 1, "event": "test"}' payload = websocket.Payload(data) assert isinstance(payload, websocket.Payload) assert payload.id == 1 assert payload.event == 'test' assert payload.data == {}
async def test_response(self): with asynctest.patch( 'synse_server.api.websocket.MessageHandler.handle_request_status' ) as mock_handler: # noqa: E501 p = websocket.Payload( json.dumps(dict( id=2, event='request/status', data={}, ))) m = websocket.MessageHandler(websockets.WebSocketCommonProtocol()) await m.dispatch(p) mock_handler.assert_called_once()
async def test_response_no_handler(self): with asynctest.patch( 'websockets.WebSocketCommonProtocol.send') as mock_send: p = websocket.Payload( json.dumps(dict( id=3, event='foo/bar', data={}, ))) m = websocket.MessageHandler(websockets.WebSocketCommonProtocol()) await m.dispatch(p) mock_send.assert_called_once() mock_send.assert_called_with( json.dumps({ 'id': 3, 'event': 'response/error', 'data': { 'description': 'unsupported event type: foo/bar', 'timestamp': '2019-04-22T13:30:00Z', } }))
def test_init_missing_required(self, data): with pytest.raises(ValueError): websocket.Payload(data)
def test_init_json_error(self): data = '{{"}' with pytest.raises(Exception): websocket.Payload(data)