Skip to content

Commit

Permalink
Merge pull request #38 from SomberNight/202103_json_strip_whitespaces
Browse files Browse the repository at this point in the history
save some bandwidth: strip redundant whitespaces from json encoding
  • Loading branch information
Neil committed Mar 20, 2021
2 parents fee8e7a + dd54af5 commit 8b48eb3
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
2 changes: 1 addition & 1 deletion aiorpcx/jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ def batch_message_from_parts(cls, messages):
def encode_payload(cls, payload):
'''Encode a Python object as JSON and convert it to bytes.'''
try:
return json.dumps(payload).encode()
return json.dumps(payload, separators=(',', ':')).encode()
except TypeError:
msg = f'JSON payload encoding error: {payload}'
raise ProtocolError(cls.INTERNAL_ERROR, msg) from None
Expand Down
22 changes: 11 additions & 11 deletions tests/test_jsonrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,23 @@ def test_parse_errors(protocol_no_auto):
protocol.message_to_item(message)
assert e.value.code == JSONRPC.PARSE_ERROR
assert 'messages must be encoded in UTF-8' in e.value.message
assert b'"id": null' in e.value.error_message
assert b'"id":null' in e.value.error_message

# Bad JSON
message = b'{"foo", }'
message = b'{"foo",}'
with pytest.raises(ProtocolError) as e:
protocol.message_to_item(message)
assert e.value.code == JSONRPC.PARSE_ERROR
assert 'invalid JSON' in e.value.message
assert b'"id": null' in e.value.error_message
assert b'"id":null' in e.value.error_message

messages = [b'2', b'"foo"', b'2.78']
for message in messages:
with pytest.raises(ProtocolError) as e:
protocol.message_to_item(message)
assert e.value.code == JSONRPC.INVALID_REQUEST
assert 'must be a dictionary' in e.value.message
assert b'"id": null' in e.value.error_message
assert b'"id":null' in e.value.error_message


# Requests
Expand Down Expand Up @@ -200,7 +200,7 @@ def test_JSONRPCv1_ill_formed():
protocol.message_to_item(message)
assert e.value.code == JSONRPC.INVALID_REQUEST
assert 'no "id"' in e.value.message
assert b'"id": null' in e.value.error_message
assert b'"id":null' in e.value.error_message


def test_bad_requests(protocol_no_auto):
Expand All @@ -211,7 +211,7 @@ def test_bad_requests(protocol_no_auto):
payload_to_item(protocol, payload)
assert e.value.code == JSONRPC.INVALID_ARGS
assert 'invalid request arguments' in e.value.message
assert b'"id": 0' in e.value.error_message
assert b'"id":0' in e.value.error_message


def test_good_requests(protocol_no_auto):
Expand Down Expand Up @@ -421,7 +421,7 @@ def test_JSONRPCv2_required_jsonrpc():
assert e.value.code == JSONRPC.INVALID_REQUEST
assert 'jsonrpc' in e.value.message
# Respond to ill-formed "notification"
assert b'"id": null' in e.value.error_message
assert b'"id":null' in e.value.error_message

payload = {"method": "f", "id": 0}
with pytest.raises(ProtocolError) as e:
Expand All @@ -430,7 +430,7 @@ def test_JSONRPCv2_required_jsonrpc():
assert e.value.code == JSONRPC.INVALID_REQUEST
assert 'jsonrpc' in e.value.message
assert b'jsonrpc' in e.value.error_message
assert b'"id": 0' in e.value.error_message
assert b'"id":0' in e.value.error_message


def test_JSONRPCv1_errors():
Expand Down Expand Up @@ -501,7 +501,7 @@ def test_batch_not_allowed(protocol):
protocol.message_to_item(b'[]')
assert e.value.code == JSONRPC.INVALID_REQUEST
assert 'dictionary' in e.value.message
assert b'"id": null' in e.value.error_message
assert b'"id":null' in e.value.error_message

batch = Batch([Request('', [])])
with pytest.raises(ProtocolError) as e:
Expand Down Expand Up @@ -535,8 +535,8 @@ def test_batch_message_from_parts(protocol):

def test_encode_payload(protocol):
assert protocol.encode_payload(2) == b'2'
assert protocol.encode_payload([2, 3]) == b'[2, 3]'
assert protocol.encode_payload({"a": 1}) == b'{"a": 1}'
assert protocol.encode_payload([2, 3]) == b'[2,3]'
assert protocol.encode_payload({"a": 1}) == b'{"a":1}'
assert protocol.encode_payload(True) == b'true'
assert protocol.encode_payload(False) == b'false'
assert protocol.encode_payload(None) == b'null'
Expand Down
4 changes: 2 additions & 2 deletions tests/test_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -568,11 +568,11 @@ async def test_log_me(self, server_port, caplog):
with caplog.at_level(logging.INFO):
assert server.log_me is False
await session.send_request('echo', ['ping'])
assert caplog_count(caplog, '"method": "echo"') == 0
assert caplog_count(caplog, '"method":"echo"') == 0

server.log_me = True
await session.send_request('echo', ['ping'])
assert caplog_count(caplog, '"method": "echo"') == 1
assert caplog_count(caplog, '"method":"echo"') == 1


class WireRPCSession(RPCSession):
Expand Down

0 comments on commit 8b48eb3

Please sign in to comment.