Raise a new ValueError with the contents of the JSON is parsing fails

Change-Id: I52c728fb9fae9ec955caf8a63adb00ee708d9419
This commit is contained in:
Dougal Matthews 2019-12-19 15:21:17 +00:00 committed by Dougal Matthews
parent 458799f4aa
commit 41b51731ba
2 changed files with 26 additions and 1 deletions

View File

@ -128,7 +128,12 @@ class WebsocketClient(object):
return data
def recv(self):
return json.loads(self._ws.recv())
content = self._ws.recv()
try:
return json.loads(content)
except ValueError:
raise ValueError(
"No JSON object could be decoded; {0!r}".format(content))
def wait_for_messages(self, timeout=None):
"""Wait for messages on a Zaqar queue

View File

@ -49,6 +49,26 @@ class TestPlugin(base.TestCase):
self.assertEqual(clientmgr.get_endpoint_for_service_type.call_count, 2)
ws_create_connection.assert_called_with("ws://0.0.0.0")
@mock.patch("websocket.create_connection")
def test_invalid_json(self, ws_create_connection):
clientmgr = mock.MagicMock()
clientmgr.get_endpoint_for_service_type.return_value = fakes.WS_URL
clientmgr.auth.get_token.return_value = "TOKEN"
clientmgr.auth_ref.project_id = "ID"
clientmgr.cacert = None
ws_create_connection.return_value.recv.return_value = "BAD JSON"
client = plugin.make_client(clientmgr)
# NOTE(d0ugal): Manually catching errors rather than using assertRaises
# so we can assert the message.
try:
client.messaging_websocket()
self.assertFail()
except ValueError as e:
self.assertEqual(
str(e), "No JSON object could be decoded; 'BAD JSON'")
@mock.patch.object(plugin.WebsocketClient, "recv")
@mock.patch("websocket.create_connection")
def test_handle_websocket_multiple(self, ws_create_connection, recv_mock):