Exemplo n.º 1
0
async def test_netstringrpc_handle_request_with_params(event_loop):
    r = MockStream()
    w = MockStream()

    rpc = netstringrpc.NetstringRPC(r, w, event_loop)

    async def handler(params):
        return {
            "result": params['the_answer'],
        }

    rpc.register_handler("the_answer", handler)

    proto.write_message(
        r, {
            "jsonrpc": "2.0",
            "id": 0,
            "method": "the_answer",
            "params": {
                "the_answer": 43
            }
        })

    async def check_the_reply():
        message = await proto.read_message(w)
        r.close()
        assert message["id"] == 0
        assert message["result"] == 43
        assert message['jsonrpc'] == "2.0"

    try:
        await asyncio.gather(check_the_reply(), rpc.loop())
    except asyncio.streams.IncompleteReadError:
        pass  # the connection's closed
Exemplo n.º 2
0
    async def check_the_reply():
        # Make sure that the plugins were registered correctly
        assert (await proto.read_message(w1))['result'] == 'success'
        assert (await proto.read_message(w2))['result'] == 'success'

        # Register the listener
        proto.write_message(r2, subscribe_message)
        assert (await proto.read_message(w2))['result'] == 'success'

        # Emit the event
        proto.write_message(
            r1, {
                "jsonrpc": "2.0",
                "id": 1,
                "method": "emit",
                "params": {
                    "name": "ExampleEvent"
                }
            })
        assert (await proto.read_message(w1))['result'] == 'success'
        assert (await proto.read_message(w2)) == {
            'method': 'event',
            'params': {
                'name': 'ExampleEvent',
                'sender': INIT_MESSAGE['params']['name']
            },
            'jsonrpc': '2.0'
        }

        r1.close()
        r2.close()
Exemplo n.º 3
0
    async def check_the_reply(server, r, w):
        message = await proto.read_message(w)
        assert message["result"] == "success"

        proto.write_message(r, subscribe_message)
        message = await proto.read_message(w)
        assert message["result"] == "success"

        added_plugin = False
        for call in sub_list.__setitem__.call_args_list:
            if call[0][0] == "TestName":
                added_plugin = True

        msg = deepcopy(subscribe_message)
        msg["method"] = "unsubscribe"
        msg["id"] = 2
        proto.write_message(r, msg)

        message = await proto.read_message(w)
        assert message["result"] == "success"

        removed_plugin = False
        for call in sub_list.pop.call_args_list:
            if call[0][0] == "TestName":
                removed_plugin = True

        r.close()
        assert added_plugin and removed_plugin
Exemplo n.º 4
0
async def test_emit_receive_event(event_loop):
    # Create the event emitter plugin
    r1 = utils.MockStream()
    w1 = utils.MockStream()
    m1 = INIT_MESSAGE

    # Create the event listener plugin
    r2 = utils.MockStream()
    w2 = utils.MockStream()
    m2 = deepcopy(INIT_MESSAGE)
    m2['params']['name'] = "TestReceiver"

    # Register the plugins
    proto.write_message(r1, m1)
    proto.write_message(r2, m2)

    async def check_the_reply():
        # Make sure that the plugins were registered correctly
        assert (await proto.read_message(w1))['result'] == 'success'
        assert (await proto.read_message(w2))['result'] == 'success'

        # Register the listener
        proto.write_message(r2, subscribe_message)
        assert (await proto.read_message(w2))['result'] == 'success'

        # Emit the event
        proto.write_message(
            r1, {
                "jsonrpc": "2.0",
                "id": 1,
                "method": "emit",
                "params": {
                    "name": "ExampleEvent"
                }
            })
        assert (await proto.read_message(w1))['result'] == 'success'
        assert (await proto.read_message(w2)) == {
            'method': 'event',
            'params': {
                'name': 'ExampleEvent',
                'sender': INIT_MESSAGE['params']['name']
            },
            'jsonrpc': '2.0'
        }

        r1.close()
        r2.close()

    # create the server
    server = main.IMClient(event_loop)

    # run the server and the client asynchronously
    try:
        await asyncio.gather(check_the_reply(), server.accept(r1, w1),
                             server.accept(r2, w2))
    except asyncio.streams.IncompleteReadError:  # the connection's closed
        pass
Exemplo n.º 5
0
    async def check_the_reply(server, r, w):
        message = await proto.read_message(w)
        assert message["result"] == "success"

        proto.write_message(r, subscribe_message)
        message = await proto.read_message(w)
        assert message["result"] == "success"

        added_plugin = False
        for call in sub_list.__setitem__.call_args_list:
            if call[0][0] == "TestName":
                added_plugin = True

        r.close()
        assert added_plugin
Exemplo n.º 6
0
    async def check_the_reply(server, r, w):
        message = await proto.read_message(w)
        assert message["result"] == "success"

        proto.write_message(r, subscribe_message)
        message = await proto.read_message(w)
        assert message["result"] == "success"

        added_subscriber_list = False
        for call in server.subscriptions.__setitem__.call_args_list:
            # call[0] contains the positional args and we want the first one
            if call[0][0] == "ExampleEvent":
                added_subscriber_list = True

        r.close()
        assert added_subscriber_list
Exemplo n.º 7
0
async def test_netstringrpc_make_request_with_params(event_loop):
    r = MockStream()
    w = MockStream()

    rpc = netstringrpc.NetstringRPC(r, w, event_loop)
    proto.write_message(r, {
        "jsonrpc": "2.0",
        "result": "ThisIsATest",
        "id": rpc.last_id
    })
    r.close()

    rpc.test.method([1, 2, 3])
    try:
        await rpc.loop()
    except asyncio.streams.IncompleteReadError:
        # connection closed
        assert (await proto.read_message(w))['params'] == [1, 2, 3]
Exemplo n.º 8
0
async def test_netstringrpc_make_request(event_loop):
    r = MockStream()
    w = MockStream()

    rpc = netstringrpc.NetstringRPC(r, w, event_loop)
    proto.write_message(r, {
        "jsonrpc": "2.0",
        "result": "ThisIsATest",
        "id": rpc.last_id
    })
    r.close()

    future = rpc.test.method()
    try:
        await rpc.loop()
    except asyncio.streams.IncompleteReadError:
        # connection closed
        response = await future
        assert response['result'] == "ThisIsATest"
Exemplo n.º 9
0
async def create_server(event_loop, messages=None, setup=None, check=None):
    r = MockStream()
    w = MockStream()
    if messages is not None:
        if isinstance(messages, dict):
            messages = [messages]
        if isinstance(messages, list):
            for message in messages:
                proto.write_message(r, message)

    server = IMClient(event_loop)
    if setup is not None:
        await setup(server, r, w)

    if check is not None:
        try:
            await asyncio.gather(check(server, r, w), server.accept(r, w))
        except asyncio.streams.IncompleteReadError:
            pass  # the connection's closed

    return server, r, w
Exemplo n.º 10
0
    async def check_the_reply(server, r, w):
        # Make sure that the plugin was registered correctly
        assert (await proto.read_message(w))['result'] == 'success'

        # Emit the event
        proto.write_message(
            r, {
                "jsonrpc": "2.0",
                "id": 1,
                "method": "emit",
                "params": {
                    "name": "MESSAGE",
                    "from": "whoever",
                    "to": "wherever",
                    "text": "whatever"
                }
            })

        assert (await proto.read_message(w))['result'] == 'success'

        r.close()
Exemplo n.º 11
0
    async def check_the_reply(server, r, w):
        # Make sure that the plugin was registered correctly
        assert (await proto.read_message(w))['result'] == 'success'

        # Emit the event
        proto.write_message(
            r, {
                "jsonrpc": "2.0",
                "id": 1,
                "method": "emit",
                "params": {
                    "name": "MESSAGE"
                }
            })

        assert (await proto.read_message(w))['error'] == {
            'code': 110,
            'message': 'Event invalid.'
        }

        r.close()
Exemplo n.º 12
0
async def test_write_message():
    s = utils.MockStream()
    obj = {"a": 1, "b": 2}
    proto.write_message(s, obj)
    assert s.get_bytes() == proto.netstring_encode(json.dumps(obj))