Handle bytes and str in py3

in python3, the autobahn lib's input and output is bytes instead
of string.

Change-Id: I6ce5fdad9c4a66a6c9a6f4c0b31af80ef2ea6e48
This commit is contained in:
xywang 2018-07-18 15:02:27 +08:00
parent 179a08d604
commit 6319cfa07f
7 changed files with 143 additions and 126 deletions

View File

@ -43,13 +43,13 @@ class TestMessagingProtocol(base.TestBase):
self.protocol.sendMessage = send_mock
self.protocol.onMessage(payload, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
payload = "123"
self.protocol.onMessage(payload, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
def test_on_message_with_invalid_input_binary(self):
@ -91,7 +91,10 @@ class TestMessagingProtocol(base.TestBase):
send_mock = mock.Mock()
self.protocol.sendMessage = send_mock
self.protocol.onMessage(req, in_binary)
resp = loads(send_mock.call_args[0][0])
arg = send_mock.call_args[0][0]
if not in_binary:
arg = arg.decode()
resp = loads(arg)
self.assertEqual(200, resp['headers']['status'])
@mock.patch.object(zaqar.transport.websocket.factory, 'ProtocolFactory')

View File

@ -94,7 +94,7 @@ class AuthTest(base.V2Base):
self.protocol.onMessage(req, False)
self.assertEqual(1, msg_mock.call_count)
resp = json.loads(msg_mock.call_args[0][0])
resp = json.loads(msg_mock.call_args[0][0].decode())
self.assertEqual(403, resp['headers']['status'])
def test_failed_auth(self):
@ -104,7 +104,7 @@ class AuthTest(base.V2Base):
self.protocol._auth_in_binary = False
self.protocol._auth_response('401 error', 'Failed')
self.assertEqual(1, msg_mock.call_count)
resp = json.loads(msg_mock.call_args[0][0])
resp = json.loads(msg_mock.call_args[0][0].decode())
self.assertEqual(401, resp['headers']['status'])
self.assertEqual('authenticate', resp['request']['action'])
@ -149,7 +149,7 @@ class AuthTest(base.V2Base):
# request will raise 401 error.
self.protocol.onMessage(req, False)
self.protocol._auth_response('401 error', 'Failed')
resp = json.loads(msg_mock.call_args[0][0])
resp = json.loads(msg_mock.call_args[0][0].decode())
self.assertEqual(401, resp['headers']['status'])
self.assertEqual('authenticate', resp['request']['action'])
@ -162,7 +162,7 @@ class AuthTest(base.V2Base):
self.protocol.onMessage(req, False)
self.protocol._auth_response('200 OK', 'authenticate success')
resp = json.loads(msg_mock.call_args[0][0])
resp = json.loads(msg_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
@ddt.data(True, False)
@ -181,7 +181,10 @@ class AuthTest(base.V2Base):
self.assertEqual(in_binary, self.protocol._auth_in_binary)
self.protocol._auth_response('401 error', 'Failed')
self.assertEqual(1, msg_mock.call_count)
resp = loads(msg_mock.call_args[0][0])
arg = msg_mock.call_args[0][0]
if not in_binary:
arg = arg.decode()
resp = loads(arg)
self.assertEqual(401, resp['headers']['status'])
def test_signed_url(self):
@ -205,7 +208,7 @@ class AuthTest(base.V2Base):
self.protocol.onMessage(req, False)
self.assertEqual(1, send_mock.call_count)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
def test_signed_url_wrong_queue(self):
@ -229,7 +232,7 @@ class AuthTest(base.V2Base):
self.protocol.onMessage(req, False)
self.assertEqual(1, send_mock.call_count)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(403, resp['headers']['status'])
def test_signed_url_wrong_method(self):
@ -254,5 +257,5 @@ class AuthTest(base.V2Base):
self.protocol.onMessage(req, False)
self.assertEqual(1, send_mock.call_count)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(403, resp['headers']['status'])

View File

@ -46,7 +46,7 @@ class ClaimsBaseTest(base.V1_1Base):
with mock.patch.object(self.protocol, 'sendMessage') as msg_mock:
self.protocol.onMessage(req, False)
resp = json.loads(msg_mock.call_args[0][0])
resp = json.loads(msg_mock.call_args[0][0].decode())
self.assertIn(resp['headers']['status'], [201, 204])
action = consts.MESSAGE_POST
@ -70,7 +70,7 @@ class ClaimsBaseTest(base.V1_1Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(201, resp['headers']['status'])
def tearDown(self):
@ -84,7 +84,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
@ddt.data('[', '[]', '.', '"fail"')
@ -97,7 +97,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
action = consts.CLAIM_UPDATE
@ -105,7 +105,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
def test_exceeded_claim(self):
@ -120,7 +120,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
@ddt.data((-1, -1), (59, 60), (60, 59), (60, 43201), (43201, 60))
@ -136,7 +136,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
@ddt.data(-1, 59, 43201)
@ -153,7 +153,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
def test_default_ttl_and_grace(self):
@ -165,7 +165,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(201, resp['headers']['status'])
action = consts.CLAIM_GET
@ -174,7 +174,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual(self.defaults.claim_ttl, resp['body']['ttl'])
@ -191,7 +191,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(201, resp['headers']['status'])
claimed_messages = resp['body']['messages']
claim_id = resp['body']['claim_id']
@ -203,7 +203,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
# Listing messages, by default, won't include claimed, will echo
@ -213,7 +213,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual([], resp['body']['messages'])
@ -224,7 +224,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual([], resp['body']['messages'])
@ -236,7 +236,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual(resp['body']['messages'], [])
@ -253,7 +253,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
# Include claimed messages this time, and echo
@ -264,7 +264,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual(len(claimed_messages), len(resp['body']['messages']))
@ -278,7 +278,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(403, resp['headers']['status'])
# Delete the message and its associated claim
@ -288,7 +288,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
# Try to get it from the wrong project
@ -302,7 +302,7 @@ class ClaimsBaseTest(base.V1_1Base):
"message_id": message_id_2}
req = test_utils.create_request(action, body, headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(404, resp['headers']['status'])
# Get the message
@ -311,7 +311,7 @@ class ClaimsBaseTest(base.V1_1Base):
"message_id": message_id_2}
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
# Update the claim
@ -323,7 +323,7 @@ class ClaimsBaseTest(base.V1_1Base):
"claim_id": claim_id}
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
# Get the claimed messages (again)
@ -333,7 +333,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
query = timeutils.utcnow()
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual(60, resp['body']['ttl'])
@ -352,7 +352,7 @@ class ClaimsBaseTest(base.V1_1Base):
"claim_id": claim_id}
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
# Try to delete a message with an invalid claim ID
@ -363,7 +363,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
# Make sure it wasn't deleted!
@ -372,7 +372,7 @@ class ClaimsBaseTest(base.V1_1Base):
"message_id": message_id_2}
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
# Try to get a claim that doesn't exist
@ -381,7 +381,7 @@ class ClaimsBaseTest(base.V1_1Base):
"claim_id": claim_id}
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(404, resp['headers']['status'])
# Try to update a claim that doesn't exist
@ -392,7 +392,7 @@ class ClaimsBaseTest(base.V1_1Base):
"claim_id": claim_id}
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(404, resp['headers']['status'])
def test_post_claim_nonexistent_queue(self):
@ -406,7 +406,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
def test_get_claim_nonexistent_queue(self):
@ -419,7 +419,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(404, resp['headers']['status'])
def _get_a_claim(self):
@ -433,7 +433,7 @@ class ClaimsBaseTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(201, resp['headers']['status'])
return resp

View File

@ -52,7 +52,7 @@ class MessagesBaseTest(base.V2Base):
with mock.patch.object(self.protocol, 'sendMessage') as msg_mock:
self.protocol.onMessage(req, False)
resp = json.loads(msg_mock.call_args[0][0])
resp = json.loads(msg_mock.call_args[0][0].decode())
self.assertIn(resp['headers']['status'], [201, 204])
def tearDown(self):
@ -67,7 +67,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
def _test_post(self, sample_messages, in_binary=False):
@ -82,8 +82,10 @@ class MessagesBaseTest(base.V2Base):
req = create_req(consts.MESSAGE_POST, body, self.headers)
self.protocol.onMessage(req, in_binary)
resp = loads(send_mock.call_args[0][0])
arg = send_mock.call_args[0][0]
if not in_binary:
arg = arg.decode()
resp = loads(arg)
self.assertEqual(201, resp['headers']['status'])
self.msg_ids = resp['body']['message_ids']
self.assertEqual(len(sample_messages), len(self.msg_ids))
@ -107,16 +109,20 @@ class MessagesBaseTest(base.V2Base):
req = create_req(action, body, headers)
self.protocol.onMessage(req, in_binary)
resp = loads(send_mock.call_args[0][0])
arg = send_mock.call_args[0][0]
if not in_binary:
arg = arg.decode()
resp = loads(arg)
self.assertEqual(404, resp['headers']['status'])
# Correct project ID
req = create_req(action, body, self.headers)
self.protocol.onMessage(req, in_binary)
resp = loads(send_mock.call_args[0][0])
arg = send_mock.call_args[0][0]
if not in_binary:
arg = arg.decode()
resp = loads(arg)
self.assertEqual(200, resp['headers']['status'])
# Check message properties
@ -137,8 +143,10 @@ class MessagesBaseTest(base.V2Base):
req = create_req(action, body, self.headers)
self.protocol.onMessage(req, in_binary)
resp = loads(send_mock.call_args[0][0])
arg = send_mock.call_args[0][0]
if not in_binary:
arg = arg.decode()
resp = loads(arg)
self.assertEqual(200, resp['headers']['status'])
expected_ttls = set(m['ttl'] for m in sample_messages)
actual_ttls = set(m['ttl'] for m in resp['body']['messages'])
@ -163,14 +171,14 @@ class MessagesBaseTest(base.V2Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
# Listing restriction
body['limit'] = 21
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
# Bulk deletion restriction
@ -180,7 +188,7 @@ class MessagesBaseTest(base.V2Base):
"message_ids": del_msg_ids}
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
@ddt.data(True, False)
@ -215,7 +223,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(201, resp['headers']['status'])
msg_id = resp['body']['message_ids'][0]
@ -226,7 +234,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual(self.default_message_ttl,
resp['body']['messages']['ttl'])
@ -274,7 +282,7 @@ class MessagesBaseTest(base.V2Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
self.assertEqual(
'Bad request. The value of the "ttl" field must be a int.',
@ -295,7 +303,7 @@ class MessagesBaseTest(base.V2Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
self.assertEqual(
'Bad request. Missing "body" field.', resp['body']['exception'])
@ -310,7 +318,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual([], resp['body']['messages'])
@ -334,7 +342,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
action = consts.MESSAGE_GET
@ -347,7 +355,7 @@ class MessagesBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
@ddt.data(None, '[', '[]', '{}', '.')
@ -365,7 +373,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
@ddt.data(-1, 59, 1209601)
@ -381,7 +389,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
def test_exceeded_message_posting(self):
@ -400,7 +408,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
@ddt.data('{"overflow": 9223372036854775808}',
@ -419,7 +427,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
def test_delete(self):
@ -437,7 +445,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
# Delete queue
@ -446,7 +454,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
# Get non existent queue
@ -454,7 +462,7 @@ class MessagesBaseTest(base.V2Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(404, resp['headers']['status'])
# Safe to delete non-existing ones
@ -462,7 +470,7 @@ class MessagesBaseTest(base.V2Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
def test_bulk_delete(self):
@ -480,7 +488,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
action = consts.MESSAGE_GET
@ -488,7 +496,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
# Safe to delete non-existing ones
@ -497,7 +505,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
# Even after the queue is gone
@ -506,7 +514,7 @@ class MessagesBaseTest(base.V2Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
action = consts.MESSAGE_DELETE_MANY
@ -515,7 +523,7 @@ class MessagesBaseTest(base.V2Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
def test_pop_delete(self):
@ -531,7 +539,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual(2, len(resp['body']['messages']))
self.assertEqual(239, resp['body']['messages'][0]['body'])
@ -548,7 +556,7 @@ class MessagesBaseTest(base.V2Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(404, resp['headers']['status'])
def test_get_multiple_invalid_messages_404s(self):
@ -562,7 +570,7 @@ class MessagesBaseTest(base.V2Base):
req = test_utils.create_request(action, body, self.headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
def test_delete_multiple_invalid_messages_204s(self):
@ -577,7 +585,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(400, resp['headers']['status'])
def _post_messages(self, queue_name, repeat=1):
@ -594,7 +602,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
return json.loads(send_mock.call_args[0][0])
return json.loads(send_mock.call_args[0][0].decode())
def test_invalid_request(self):
send_mock = mock.Mock()
@ -602,7 +610,7 @@ class MessagesBaseTest(base.V2Base):
self.protocol.onMessage('foo', False)
self.assertEqual(1, send_mock.call_count)
response = json.loads(send_mock.call_args[0][0])
response = json.loads(send_mock.call_args[0][0].decode())
self.assertIn('error', response['body'])
self.assertEqual({'status': 400}, response['headers'])
self.assertEqual(

View File

@ -47,7 +47,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(400, resp['headers']['status'])
with mock.patch.object(self.protocol, 'sendMessage') as msg_mock:
@ -71,7 +71,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(404, resp['headers']['status'])
sender.side_effect = validator
@ -90,7 +90,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(201, resp['headers']['status'])
sender.side_effect = validator
@ -107,7 +107,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual(meta, resp['body'])
@ -120,7 +120,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(200, resp['headers']['status'])
sender.side_effect = validator
@ -132,7 +132,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(204, resp['headers']['status'])
sender.side_effect = validator
@ -144,7 +144,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(404, resp['headers']['status'])
sender.side_effect = validator
@ -172,7 +172,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertIn(resp['headers']['status'], [201, 204])
sender.side_effect = validator
@ -182,7 +182,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(400, resp['headers']['status'])
sender.side_effect = validator
@ -207,7 +207,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(400, resp['headers']['status'])
sender.side_effect = validator
@ -217,7 +217,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertIn(resp['headers']['status'], [201, 204])
sender.side_effect = validator
@ -241,7 +241,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(400, resp['headers']['status'])
sender.side_effect = validator
@ -267,14 +267,14 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertIn(resp['headers']['status'], [201, 204])
sender.side_effect = validator
self.protocol.onMessage(req, False)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(204, resp['headers']['status'])
sender.side_effect = validator
@ -297,7 +297,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(400, resp['headers']['status'])
sender.side_effect = validator
@ -324,7 +324,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(400, resp['headers']['status'])
sender.side_effect = validator
@ -351,7 +351,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(400, resp['headers']['status'])
sender.side_effect = validator
@ -374,7 +374,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(201, resp['headers']['status'])
sender.side_effect = validator
@ -387,7 +387,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(204, resp['headers']['status'])
sender.side_effect = validator
@ -400,7 +400,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(204, resp['headers']['status'])
self.assertEqual(meta1, resp['body'])
@ -415,7 +415,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(204, resp['headers']['status'])
sender.side_effect = validator
@ -428,7 +428,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual(meta2, resp['body'])
@ -461,7 +461,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual([], resp['body']['queues'])
@ -473,7 +473,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(400, resp['headers']['status'])
sender.side_effect = validator
@ -491,7 +491,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, altheaders)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(201, resp['headers']['status'])
sender.side_effect = validator
@ -508,7 +508,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(2, len(resp['body']['queues']))
sender.side_effect = validator
@ -519,7 +519,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(200, resp['headers']['status'])
# Ensure we didn't pick up the queue from the alt project.
self.assertEqual(3, len(resp['body']['queues']))
@ -532,7 +532,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(200, resp['headers']['status'])
sender.side_effect = validator
@ -543,7 +543,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(200, resp['headers']['status'])
self.assertEqual({"node": 31}, resp['body'])
@ -556,7 +556,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(200, resp['headers']['status'])
sender.side_effect = validator
@ -582,7 +582,7 @@ class QueueLifecycleBaseTest(base.V2Base):
req = test_utils.create_request(action, body, headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(503, resp['headers']['status'])
sender.side_effect = validator
@ -616,7 +616,7 @@ class QueueLifecycleBaseTest(base.V2Base):
self.protocol.onMessage(req, False)
return json.loads(send_mock.call_args[0][0])
return json.loads(send_mock.call_args[0][0].decode())
def test_purge(self):
arbitrary_number = 644079696574693
@ -637,14 +637,14 @@ class QueueLifecycleBaseTest(base.V2Base):
body = {"queue_name": queue_name, "message_id": msg_id}
req = test_utils.create_request(action, body, headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(200, resp['headers']['status'])
action = consts.QUEUE_PURGE
body = {"queue_name": queue_name, "resource_types": ["messages"]}
req = test_utils.create_request(action, body, headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(204, resp['headers']['status'])
for msg_id in msg_ids:
@ -652,7 +652,7 @@ class QueueLifecycleBaseTest(base.V2Base):
body = {"queue_name": queue_name, "message_id": msg_id}
req = test_utils.create_request(action, body, headers)
self.protocol.onMessage(req, False)
resp = json.loads(send_mock.call_args[0][0])
resp = json.loads(send_mock.call_args[0][0].decode())
self.assertEqual(404, resp['headers']['status'])

View File

@ -47,7 +47,7 @@ class SubscriptionTest(base.V1_1Base):
body, self.headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertIn(resp['headers']['status'], [201, 204])
with mock.patch.object(self.protocol, 'sendMessage') as msg_mock:
@ -66,7 +66,7 @@ class SubscriptionTest(base.V1_1Base):
body, self.headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(resp['headers']['status'], 204)
sender.side_effect = validator
@ -110,7 +110,7 @@ class SubscriptionTest(base.V1_1Base):
'api': 'v2', 'headers': self.headers}}
self.assertEqual(1, sender.call_count)
self.assertEqual(response, json.loads(sender.call_args[0][0]))
self.assertEqual(response, json.loads(sender.call_args[0][0].decode()))
# Trigger protocol close
self.protocol.onClose(True, 100, None)
@ -180,7 +180,7 @@ class SubscriptionTest(base.V1_1Base):
'subscription_id': str(sub)},
'api': 'v2', 'headers': self.headers}}
self.assertEqual(1, sender.call_count)
self.assertEqual(response, json.loads(sender.call_args[0][0]))
self.assertEqual(response, json.loads(sender.call_args[0][0].decode()))
def test_subscription_create_no_queue(self):
action = consts.SUBSCRIPTION_CREATE
@ -214,7 +214,7 @@ class SubscriptionTest(base.V1_1Base):
'api': 'v2', 'headers': self.headers}}
self.assertEqual(1, sender.call_count)
self.assertEqual(response, json.loads(sender.call_args[0][0]))
self.assertEqual(response, json.loads(sender.call_args[0][0].decode()))
def test_subscription_get(self):
sub = self.boot.storage.subscription_controller.create(
@ -246,7 +246,7 @@ class SubscriptionTest(base.V1_1Base):
'api': 'v2', 'headers': self.headers}}
self.assertEqual(1, sender.call_count)
response = json.loads(sender.call_args[0][0])
response = json.loads(sender.call_args[0][0].decode())
# Get and remove age from the actual response.
actual_sub_age = response['body'].pop('age')
self.assertLessEqual(0, actual_sub_age)
@ -282,7 +282,7 @@ class SubscriptionTest(base.V1_1Base):
'body': {'queue_name': 'kitkat'},
'api': 'v2', 'headers': self.headers}}
self.assertEqual(1, sender.call_count)
response = json.loads(sender.call_args[0][0])
response = json.loads(sender.call_args[0][0].decode())
# Get and remove age from the actual response.
actual_sub_age = response['body']['subscriptions'][0].pop('age')
self.assertLessEqual(0, actual_sub_age)
@ -334,7 +334,8 @@ class SubscriptionTest(base.V1_1Base):
# Check that the server responded in text format to the message
# creation request
message_create_response = json.loads(sender.call_args_list[1][0][0])
message_create_response = json.loads(
sender.call_args_list[1][0][0].decode())
self.assertEqual(201, message_create_response['headers']['status'])
# Fetch webhook notification that was intended to arrive to
@ -368,7 +369,7 @@ class SubscriptionTest(base.V1_1Base):
req = test_utils.create_request(action, body, self.headers)
def validator(resp, isBinary):
resp = json.loads(resp)
resp = json.loads(resp.decode())
self.assertEqual(503, resp['headers']['status'])
sender.side_effect = validator

View File

@ -82,6 +82,8 @@ class MessagingProtocol(websocket.WebSocketServerProtocol):
if isBinary:
payload = msgpack.unpackb(payload, encoding='utf-8')
else:
if isinstance(payload, bytes):
payload = payload.decode()
payload = json.loads(payload)
except Exception:
if isBinary:
@ -207,7 +209,7 @@ class MessagingProtocol(websocket.WebSocketServerProtocol):
self.sendMessage(msgpack.packb(resp.get_response()), True)
else:
pack_name = 'txt'
self.sendMessage(json.dumps(resp.get_response()), False)
self.sendMessage(json.dumps(resp.get_response()).encode(), False)
if LOG.isEnabledFor(logging.INFO):
api = resp._request._api
status = resp._headers['status']