Exemplo n.º 1
0
def test_examples_nameds():
    def subtract(context: Context, **kwargs):
        return SuccessResponse(
            kwargs["minuend"] - kwargs["subtrahend"], id=context.request.id
        )

    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}',
        Methods(subtract),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == 19

    # Second example
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}',
        Methods(subtract),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == 19
Exemplo n.º 2
0
def test_examples_nameds():
    def subtract(**kwargs):
        return kwargs["minuend"] - kwargs["subtrahend"]

    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "subtract", "params": {"subtrahend": 23, "minuend": 42}, "id": 3}',
        Methods(subtract),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == 19

    # Second example
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "subtract", "params": {"minuend": 42, "subtrahend": 23}, "id": 4}',
        Methods(subtract),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == 19
Exemplo n.º 3
0
def test_examples_positionals():
    def subtract(minuend, subtrahend):
        return minuend - subtrahend

    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}',
        Methods(subtract),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == 19

    # Second example
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}',
        Methods(subtract),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == -19
def test_dispatch_pure_notification():
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "ping"}',
        Methods(ping),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
    )
    assert isinstance(response, NotificationResponse)
def test_dispatch_pure_notification_invalid_jsonrpc():
    response = dispatch_pure(
        '{"jsonrpc": "0", "method": "notify"}',
        Methods(ping),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
    )
    assert isinstance(response, ErrorResponse)
Exemplo n.º 6
0
def test_dispatch_pure_notification_invalid_jsonrpc():
    response = dispatch_pure(
        '{"jsonrpc": "0", "method": "notify"}',
        Methods(ping),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, ErrorResponse)
Exemplo n.º 7
0
def test_dispatch_pure_notification():
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "ping"}',
        Methods(ping),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, NotificationResponse)
Exemplo n.º 8
0
def test_examples_notification():
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "update", "params": [1, 2, 3, 4, 5]}',
        Methods(update=lambda: None, foobar=lambda: None),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, NotificationResponse)

    # Second example
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "foobar"}',
        Methods(update=lambda: None, foobar=lambda: None),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, NotificationResponse)
def test_examples_empty_array():
    # This is an invalid JSON-RPC request, should return an error.
    response = dispatch_pure(
        "[]", Methods(ping), convert_camel_case=False, context=NOCONTEXT, debug=True
    )
    assert isinstance(response, ErrorResponse)
    assert (
        str(response)
        == '{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid JSON-RPC", "data": null}, "id": null}'
    )
Exemplo n.º 10
0
def test_dispatch_pure_invalid_jsonrpc():
    """Invalid JSON-RPC, must return an error. (impossible to determine if notification)"""
    response = dispatch_pure(
        "{}",
        Methods(ping),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, InvalidJSONRPCResponse)
Exemplo n.º 11
0
def test_dispatch_pure_invalid_json():
    """Unable to parse, must return an error"""
    response = dispatch_pure(
        "{",
        Methods(ping),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, InvalidJSONResponse)
Exemplo n.º 12
0
def test_dispatch_pure_request():
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "ping", "id": 1}',
        Methods(ping),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == "pong"
    assert response.id == 1
Exemplo n.º 13
0
def test_examples_notification():
    methods = {"update": lambda: None, "foobar": lambda: None}
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "update", "params": [1, 2, 3, 4, 5]}',
        methods,
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
    )
    assert isinstance(response, NotificationResponse)

    # Second example
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "foobar"}',
        methods,
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
    )
    assert isinstance(response, NotificationResponse)
Exemplo n.º 14
0
def test_dispatch_pure_request():
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "ping", "id": 1}',
        Methods(ping),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == "pong"
    assert response.id == 1
Exemplo n.º 15
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 = Methods(
        sum=lambda ctx, *args: SuccessResponse(sum(args), id=ctx.request.id),
        notify_hello=lambda ctx, *args: SuccessResponse(19, id=ctx.request.id),
        subtract=lambda ctx, *args: SuccessResponse(
            args[0] - sum(args[1:]), id=ctx.request.id
        ),
        get_data=lambda ctx: SuccessResponse(["hello", 5], id=ctx.request.id),
    )
    requests = serialize(
        [
            {"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_pure(
        requests,
        methods,
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    expected = [
        {"jsonrpc": "2.0", "result": 7, "id": "1"},
        {"jsonrpc": "2.0", "result": 19, "id": "2"},
        {
            "jsonrpc": "2.0",
            "error": {"code": -32601, "message": "Method not found", "data": "foo.get"},
            "id": "5",
        },
        {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"},
    ]
    assert isinstance(response, BatchResponse)
    print(response.deserialized())
    for r in response.deserialized():
        assert r in expected
Exemplo n.º 16
0
def test_dispatch_pure_invalid_json():
    """Unable to parse, must return an error"""
    response = dispatch_pure(
        "{",
        Methods(ping),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, InvalidJSONResponse)
Exemplo n.º 17
0
def test_dispatch_pure_invalid_jsonrpc():
    """Invalid JSON-RPC, must return an error. (impossible to determine if notification)"""
    response = dispatch_pure(
        "{}",
        Methods(ping),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, InvalidJSONRPCResponse)
Exemplo n.º 18
0
def test_dispatch_pure_invalid_params():
    def foo(colour):
        assert colour in ("orange", "red", "yellow"), "Invalid colour"

    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "foo", "params": ["blue"], "id": 1}',
        Methods(foo),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
    )
    assert isinstance(response, InvalidParamsResponse)
Exemplo n.º 19
0
def test_dispatch_pure_invalid_params_count():
    def foo(colour: str, size: str):
        pass

    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "foo", "params": {"colour":"blue"}, "id": 1}',
        Methods(foo),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
    )
    assert isinstance(response, InvalidParamsResponse)
    assert response.data == 'missing a required argument: \'size\''
Exemplo n.º 20
0
def test_dispatch_pure_invalid_params():
    def foo(context: Context, colour: str):
        if colour not in ("orange", "red", "yellow"):
            return InvalidParamsResponse(id=context.request.id)

    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "foo", "params": ["blue"], "id": 1}',
        Methods(foo),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, InvalidParamsResponse)
Exemplo n.º 21
0
def test_examples_invalid_jsonrpc_batch():
    """
    We break the spec here, by not validating each request in the batch individually.
    The examples are expecting a batch response full of error responses.
    """
    response = dispatch_pure(
        "[1]", Methods(ping), convert_camel_case=False, context=NOCONTEXT, debug=True
    )
    assert isinstance(response, InvalidJSONRPCResponse)
    assert (
        str(response)
        == '{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid JSON-RPC", "data": null}, "id": null}'
    )
Exemplo n.º 22
0
def test_dispatch_pure_invalid_params_count():
    def foo(context: Context, colour: str, size: str):
        pass

    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "foo", "params": {"colour":"blue"}, "id": 1}',
        Methods(foo),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, InvalidParamsResponse)
    assert response.data == "missing a required argument: 'size'"
Exemplo n.º 23
0
def test_examples_invalid_json():
    response = dispatch_pure(
        '[{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method"]',
        Methods(ping),
        convert_camel_case=False,
        context=NOCONTEXT,
        debug=True,
    )
    assert isinstance(response, ErrorResponse)
    assert (
        str(response)
        == '{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Invalid JSON", "data": "Expecting \':\' delimiter: line 1 column 96 (char 95)"}, "id": null}'
    )
Exemplo n.º 24
0
def test_examples_invalid_json():
    response = dispatch_pure(
        '[{"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"}, {"jsonrpc": "2.0", "method"]',
        Methods(ping),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, ErrorResponse)
    assert (
        str(response)
        == '{"jsonrpc": "2.0", "error": {"code": -32700, "message": "Invalid JSON", "data": "Expecting \':\' delimiter: line 1 column 96 (char 95)"}, "id": null}'
    )
Exemplo n.º 25
0
def test_examples_empty_array():
    # This is an invalid JSON-RPC request, should return an error.
    response = dispatch_pure(
        "[]",
        Methods(ping),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, ErrorResponse)
    assert (
        str(response)
        == '{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid JSON-RPC", "data": null}, "id": null}'
    )
Exemplo n.º 26
0
def test_examples_positionals():
    def subtract(context: Context, minuend, subtrahend):
        return SuccessResponse(minuend - subtrahend, id=context.request.id)

    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "subtract", "params": [42, 23], "id": 1}',
        Methods(subtract),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == 19

    # Second example
    response = dispatch_pure(
        '{"jsonrpc": "2.0", "method": "subtract", "params": [23, 42], "id": 2}',
        Methods(subtract),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == -19
Exemplo n.º 27
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 = Methods(
        **{
            "sum": lambda *args: sum(args),
            "notify_hello": lambda *args: 19,
            "subtract": lambda *args: args[0] - sum(args[1:]),
            "get_data": lambda: ["hello", 5],
        }
    )
    requests = serialize(
        [
            {"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_pure(
        requests, methods, convert_camel_case=False, context=NOCONTEXT, debug=True
    )
    expected = [
        {"jsonrpc": "2.0", "result": 7, "id": "1"},
        {"jsonrpc": "2.0", "result": 19, "id": "2"},
        {
            "jsonrpc": "2.0",
            "error": {"code": -32601, "message": "Method not found", "data": "foo.get"},
            "id": "5",
        },
        {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"},
    ]
    assert isinstance(response, BatchResponse)
    for r in response.deserialized():
        assert r in expected
Exemplo n.º 28
0
def test_examples_multiple_invalid_jsonrpc():
    """
    We break the spec here, by not validating each request in the batch individually.
    The examples are expecting a batch response full of error responses.
    """
    response = dispatch_pure(
        "[1, 2, 3]",
        Methods(ping),
        extra=None,
        serialize=default_serialize,
        deserialize=default_deserialize,
    )
    assert isinstance(response, ErrorResponse)
    assert (
        str(response)
        == '{"jsonrpc": "2.0", "error": {"code": -32600, "message": "Invalid JSON-RPC", "data": null}, "id": null}'
    )