async def test_dispatch_request():
    request = Request("ping", [], 1)
    assert await dispatch_request({"ping": ping}, NOCONTEXT, request) == (
        request,
        Right(SuccessResult("pong")),
    )
Example #2
0
def test_Success():
    assert Success() == Right(SuccessResult(None))
async def test_call():
    assert await call(Request("ping", [], 1), NOCONTEXT,
                      ping) == Right(SuccessResult("pong"))
Example #4
0
def test_SuccessResult():
    assert SuccessResult(None).result is None
Example #5
0
def test_SuccessResult_repr():
    assert repr(SuccessResult(None)) == "SuccessResult(None)"
Example #6
0
def test_examples_mixed_requests_and_notifications():
    """
    We break the spec here. The examples put an invalid jsonrpc request in the
    mix here.  but it's removed to test the rest, because we're not validating
    each request individually. Any invalid jsonrpc will respond with a single
    error message.

    The spec example includes this which invalidates the entire request:
        {"foo": "boo"},
    """
    methods = {
        "sum": lambda *args: Right(SuccessResult(sum(args))),
        "notify_hello": lambda *args: Right(SuccessResult(19)),
        "subtract": lambda *args: Right(SuccessResult(args[0] - sum(args[1:]))),
        "get_data": lambda: Right(SuccessResult(["hello", 5])),
    }
    requests = json.dumps(
        [
            {"jsonrpc": "2.0", "method": "sum", "params": [1, 2, 4], "id": "1"},
            {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
            {"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": "2"},
            {
                "jsonrpc": "2.0",
                "method": "foo.get",
                "params": {"name": "myself"},
                "id": "5",
            },
            {"jsonrpc": "2.0", "method": "get_data", "id": "9"},
        ]
    )
    response = dispatch_to_response_pure(
        deserializer=default_deserializer,
        validator=default_validator,
        post_process=identity,
        context=NOCONTEXT,
        methods=methods,
        request=requests,
    )
    expected = [
        Right(
            SuccessResponse(result=7, id="1")
        ),  # {"jsonrpc": "2.0", "result": 7, "id": "1"},
        Right(
            SuccessResponse(result=19, id="2")
        ),  # {"jsonrpc": "2.0", "result": 19, "id": "2"},
        Left(
            ErrorResponse(
                code=-32601, message="Method not found", data="foo.get", id="5"
            )
        ),
        # {
        #     "jsonrpc": "2.0",
        #     "error": {"code": -32601, "message": "Method not found", "data": "foo.get"},
        #     "id": "5",
        # },
        Right(
            SuccessResponse(result=["hello", 5], id="9")
        ),  # {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"},
    ]
    # assert isinstance(response, Iterable)
    for r in response:
        assert r in expected
Example #7
0
def test_to_response_SuccessResult():
    assert to_response(
        Request("ping", [], sentinel.id), Right(SuccessResult(sentinel.result))
    ) == Right(SuccessResponse(sentinel.result, sentinel.id))
Example #8
0
def test_not_notification_false():
    assert not_notification((Request("ping", [], NOID), SuccessResult("pong"))) == False
Example #9
0
def test_not_notification():
    assert not_notification((Request("ping", [], 1), SuccessResult("pong"))) == True
Example #10
0
def test_to_response_notification():
    with pytest.raises(AssertionError):
        to_response(Request("ping", [], NOID), SuccessResult(result=sentinel.result))