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_positionals_with_context(self):
methods = {"square": lambda foo, context=None: context}
req = Request(
{"jsonrpc": "2.0", "method": "square", "params": [FOO], "id": 1},
context=BAR,
)
self.assertEqual(BAR, req.call(methods)["result"])
def test_configuring_http_status(self):
NotificationResponse.http_status = status.HTTP_OK
req = Request({"jsonrpc": "2.0", "method": "foo"}).call([foo])
self.assertEqual(status.HTTP_OK, req.http_status)
NotificationResponse.http_status = status.HTTP_NO_CONTENT
def test_positionals(self):
methods = {"square": lambda x: x * x}
req = Request({"jsonrpc": "2.0", "method": "square", "params": [3], "id": 1})
self.assertEqual(9, req.call(methods)["result"])
def test_methods_functions_with_decorator(self):
methods = Methods()
@methods.add
def foo():
return "bar"
req = Request({"jsonrpc": "2.0", "method": "foo", "id": 1})
self.assertEqual("bar", req.call(methods)["result"])
def test_dict_partials(self):
multiply = lambda x, y: x * y
req = Request({"jsonrpc": "2.0", "method": "baz", "params": [3], "id": 1})
self.assertEqual(6, req.call({"baz": partial(multiply, 2)})["result"])
def test_request_id_notification(self):
req = Request({"jsonrpc": "2.0", "method": "foo"})
self.assertEqual(None, req.request_id)
def test_methods_lambdas(self):
methods = Methods()
methods.add(lambda: "bar", "foo")
req = Request({"jsonrpc": "2.0", "method": "foo", "id": 1})
self.assertEqual("bar", req.call(methods)["result"])
def test_invalid_request(self):
req = Request({"jsonrpc": "2.0"})
self.assertIsInstance(req.response, ErrorResponse)
def test_add_lambda_no_name():
lmb = lambda x, y: x + y
methods = Methods(lmb)
# The lambda's __name__ will be ''!
assert "" in methods.items
def test_safe_call_method_not_found():
response = safe_call(
Request(method="nonexistant", id=1),
Methods(ping),
debug=True,
serialize=default_serialize,
)
assert isinstance(response, MethodNotFoundResponse)