Merge "Raise a new ValueError with the contents of the JSON is parsing fails" into stable/train

This commit is contained in:
Zuul 2020-04-30 05:21:39 +00:00 committed by Gerrit Code Review
commit c6b3322780
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):