Esempio n. 1
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)
    assert isinstance(response, InvalidJSONRPCResponse)
Esempio n. 2
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)
    assert isinstance(response, InvalidJSONResponse)
Esempio n. 3
0
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)
Esempio n. 4
0
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)
Esempio n. 5
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)
Esempio n. 6
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
Esempio n. 7
0
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}'
    )
Esempio n. 8
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)
Esempio n. 9
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}'
    )
Esempio n. 10
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}'
    )
Esempio n. 11
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,
    )
    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,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == 19
Esempio n. 12
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,
    )
    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,
    )
    assert isinstance(response, SuccessResponse)
    assert response.result == -19
Esempio n. 13
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