How to use tinyrpc - 10 common examples

To help you get started, we’ve selected a few tinyrpc examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github mbr / tinyrpc / tests / test_jsonrpc.py View on Github external
def prot():
    from tinyrpc.protocols.jsonrpc import JSONRPCProtocol

    return JSONRPCProtocol()
github mbr / tinyrpc / tests / test_protocols.py View on Github external
def protocol(request):
    if 'jsonrpc':
        return JSONRPCProtocol()

    raise RuntimeError('Bad protocol name in test case')
github mbr / tinyrpc / tests / test_jsonrpc.py View on Github external
def test_missing_jsonrpc_version_on_reply(prot):
    with pytest.raises(InvalidReplyError):
        prot.parse_reply('{"result": 7, "id": "1"}')
github mbr / tinyrpc / tests / test_transport.py View on Github external
#!/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
github mbr / tinyrpc / tests / test_server.py View on Github external
def dispatcher(response):
    dispatcher = Mock(RPCDispatcher)
    dispatcher.dispatch = Mock(return_value=response)
    return dispatcher
github mbr / tinyrpc / tests / test_dispatch.py View on Github external
def dispatch():
    return RPCDispatcher()
github mbr / tinyrpc / tests / test_dispatch.py View on Github external
def subdispatch():
    return RPCDispatcher()
github mbr / tinyrpc / tests / test_client.py View on Github external
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)
github mbr / tinyrpc / tests / test_jsonrpc.py View on Github external
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())
github mbr / tinyrpc / tests / test_jsonrpc.py View on Github external
    (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)