Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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,
serialize=default_serialize,
deserialize=default_deserialize,
)
assert isinstance(response, NotificationResponse)
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,
serialize=default_serialize,
deserialize=default_deserialize,
)
assert isinstance(response, NotificationResponse)
# Second example
response = dispatch_pure(
'{"jsonrpc": "2.0", "method": "foobar"}',
methods,
convert_camel_case=False,
context=NOCONTEXT,
debug=True,
serialize=default_serialize,
deserialize=default_deserialize,
)
assert isinstance(response, NotificationResponse)
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)
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,
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,
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}'
)
def test_dispatch_pure_notification_invalid_jsonrpc():
response = dispatch_pure(
'{"jsonrpc": "0", "method": "notify"}',
Methods(ping),
convert_camel_case=False,
context=NOCONTEXT,
debug=True,
serialize=default_serialize,
deserialize=default_deserialize,
)
assert isinstance(response, ErrorResponse)
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,
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}'
)
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),
convert_camel_case=False,
context=NOCONTEXT,
debug=True,
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}'
)
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,