Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_success_response_null_id():
# OK - any type of id is acceptable
response = SuccessResponse("foo", id=None)
assert str(response) == '{"jsonrpc": "2.0", "result": "foo", "id": null}'
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,
serialize=default_serialize,
deserialize=default_deserialize,
)
assert isinstance(response, SuccessResponse)
assert response.result == "pong"
assert response.id == 1
def test_success_response():
response = SuccessResponse("foo", id=1)
assert response.wanted == True
assert response.result == "foo"
assert str(response) == '{"jsonrpc": "2.0", "result": "foo", "id": 1}'
def test_batch_response():
response = BatchResponse(
{SuccessResponse("foo", id=1), SuccessResponse("bar", id=2)}
)
expected = [
{"jsonrpc": "2.0", "result": "foo", "id": 1},
{"jsonrpc": "2.0", "result": "bar", "id": 2},
]
assert response.wanted == True
for r in response.deserialized():
assert r in expected
def test_success_response_str():
response = SuccessResponse("foo", id=1)
assert str(response) == '{"jsonrpc": "2.0", "result": "foo", "id": 1}'
def test_success_response_null_result():
# Perfectly fine.
response = SuccessResponse(None, id=1)
assert str(response) == '{"jsonrpc": "2.0", "result": null, "id": 1}'
Args:
request: The Request object.
methods: The list of methods that can be called.
debug: Include more information in error responses.
serialize: Function that is used to serialize data.
Returns:
A Response object.
"""
with handle_exceptions(request, debug) as handler:
result = call(lookup(methods, request.method), *request.args, **request.kwargs)
# Ensure value returned from the method is JSON-serializable. If not,
# handle_exception will set handler.response to an ExceptionResponse
serialize(result)
handler.response = SuccessResponse(
result=result, id=request.id, serialize_func=serialize
)
return handler.response
async def safe_call(
request: Request, methods: Methods, *, debug: bool, serialize: Callable
) -> Response:
with handle_exceptions(request, debug) as handler:
result = await call(
lookup(methods, request.method), *request.args, **request.kwargs
)
# Ensure value returned from the method is JSON-serializable. If not,
# handle_exception will set handler.response to an ExceptionResponse
serialize(result)
handler.response = SuccessResponse(
result=result, id=request.id, serialize_func=serialize
)
return handler.response