Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def prot():
from tinyrpc.protocols.jsonrpc import JSONRPCProtocol
return JSONRPCProtocol()
def protocol(request):
if 'jsonrpc':
return JSONRPCProtocol()
raise RuntimeError('Bad protocol name in test case')
def test_missing_jsonrpc_version_on_reply(prot):
with pytest.raises(InvalidReplyError):
prot.parse_reply('{"result": 7, "id": "1"}')
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pytest
import zmq
import zmq.green
from tinyrpc.transports import ServerTransport, ClientTransport
from tinyrpc.transports.zmq import ZmqServerTransport, ZmqClientTransport
class DummyServerTransport(ServerTransport):
def __init__(self):
self.messages = []
self.clients = {}
def receive_message(self):
return self.messages.pop()
def send_reply(self, context, message):
if not isinstance(message, str):
raise TypeError('Message must be str().')
self.clients[context].messages.append(message)
class DummyClientTransport(ClientTransport):
def __init__(self, server):
self.server = server
def dispatcher(response):
dispatcher = Mock(RPCDispatcher)
dispatcher.dispatch = Mock(return_value=response)
return dispatcher
def dispatch():
return RPCDispatcher()
def subdispatch():
return RPCDispatcher()
def test_client_raises_error_replies(client, mock_protocol, method_name,
method_args, method_kwargs,
one_way_setting):
error_response = RPCErrorResponse()
error_response.error = 'foo'
mock_protocol.parse_reply = Mock(return_value=error_response)
with pytest.raises(RPCError):
client.call(method_name, method_args, method_kwargs, one_way_setting)
def test_jsonrpc_spec_v2_example8(prot):
try:
prot.parse_request("""[]""")
assert False
except JSONRPCInvalidRequestError as error:
e = error
response = e.error_respond()
assert _json_equal("""{"jsonrpc": "2.0", "error": {"code": -32600,
"message": "Invalid Request"}, "id": null}""",
response.serialize())
(JSONRPCInvalidRequestError, -32600, 'Invalid Request'),
(JSONRPCMethodNotFoundError, -32601, 'Method not found'),
(JSONRPCInvalidParamsError, -32602, 'Invalid params'),
(JSONRPCInternalError, -32603, 'Internal error'),
# generic errors
#(InvalidRequestError, -32600, 'Invalid Request'),
#(MethodNotFoundError, -32601, 'Method not found'),
#(ServerError, -32603, 'Internal error'),
])
def test_proper_construction_of_error_codes(prot, exc, code, message):
request = prot.parse_request(
"""{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4],
"id": "1"}"""
)
reply = exc().error_respond().serialize()
assert isinstance(reply, bytes)