def get_methods(registry: Union[RpcRegistry, object]) -> Dict[str, Callable]: methods: Dict[str, Callable] = {} for fn in RestRpcExecutor.iter_handler(registry): if hasattr(fn, "__rpc_name__"): name = getattr(fn, "__rpc_name__") if name in methods: raise UserWarning("Method %s duplicated" "" % name) methods[name] = fn return methods
async def test_error() -> None: reg = RpcRegistry() @reg.method() async def echo(text: str) -> str: raise Exception('Ex') ex = RestRpcExecutor(reg, get_app()) res, status = await ex.exec(b'{"text": "123"}', method_name='echo') assert json.loads(res) == {'error': {'code': 500, 'message': 'Ex'}} assert status == 500
async def test_success_by_pos_default() -> None: reg = RpcRegistry() @reg.method() async def sum(a: int, b: int, c: int = 3) -> Dict[str, int]: return {'sum': a + b + c} ex = RestRpcExecutor(reg, get_app()) res, status = await ex.exec(b'{"a": 1,"b": 2}', method_name='sum') assert json.loads(res) == {"sum": 6} assert status == 200
async def test_success_by_name() -> None: reg = RpcRegistry() @reg.method() async def echo(text: str) -> Dict[str, str]: return {'echo': text} ex = RestRpcExecutor(reg, get_app()) res, status = await ex.exec(b' {"text": "123"}', method_name='echo') assert json.loads(res) == {'echo': '123'} assert status == 200
async def test_error_method() -> None: reg = RpcRegistry() ex = RestRpcExecutor(reg, get_app()) res, status = await ex.exec(b'{"text": "123"}', method_name='echo') assert json.loads(res) == { 'error': { 'code': 404, 'message': 'Method not found' } } assert status == 404
async def test_error_params() -> None: reg = RpcRegistry() @reg.method() async def echo(text: str, a: int) -> str: raise Exception('Ex') ex = RestRpcExecutor(reg, get_app()) res, status = await ex.exec(b'', method_name='echo') assert json.loads(res) == { 'error': { 'code': 400, 'message': 'Bad Request' } } assert status == 400
async def test_err_parse_1() -> None: reg = RpcRegistry() @reg.method() async def sum(a: int, b: int, c: int = 3) -> Dict[str, int]: return {'sum': a + b + c} ex = RestRpcExecutor(reg, get_app()) res, status = await ex.exec(b'eeeee', method_name='sum') assert json.loads(res) == { 'error': { 'code': 400, 'message': 'Bad Request' } } assert status == 400
async def test_error_params_by_pos() -> None: reg = RpcRegistry() @reg.method() async def echo(text: str, a: int) -> str: raise Exception('Ex') ex = RestRpcExecutor(reg, get_app()) res, status = await ex.exec(b'{}', method_name='echo') assert json.loads(res) == { 'error': { 'code': 400, 'data': 'Missing required params in request', 'message': 'Invalid params', } } assert status == 400
async def test_err_invalid_req_3() -> None: # TODO: fix typing error reg = RpcRegistry() @reg.method() async def sum(a: int, b: int, c: int = 3) -> Dict[str, int]: return {'sum': a + b + c} ex = RestRpcExecutor(reg, get_app()) res, status = await ex.exec(b'{"a": string,"b": 2}', method_name='sum') assert json.loads(res) == { 'error': { 'code': 400, 'message': 'Bad Request' } } assert status == 400
async def test_error_unexpected_params_by_pos() -> None: reg = RpcRegistry() @reg.method() async def echo(a: int, b: int = 2) -> str: raise Exception('Ex') ex = RestRpcExecutor(reg, get_app()) res, status = await ex.exec(b'{"a": 1,"b": 2, "d": 5}', method_name='echo') assert json.loads(res) == { 'error': { 'code': 400, 'data': { 'info': 'Got an unexpected argument: d' }, 'message': 'Invalid params', } } assert status == 400
async def test_err_invalid_req_2() -> None: reg = RpcRegistry() @reg.method() async def sum(a: int, b: int, c: int = 3) -> Dict[str, int]: return {'sum': a + b + c} ex = RestRpcExecutor(reg, get_app()) res, status = await ex.exec(b'{"a": 1}', method_name='sum') assert json.loads(res) == { 'error': { 'code': 400, 'data': { 'info': 'Missing 1 required argument(s): b' }, 'message': 'Invalid params', } } assert status == 400
async def transport(request: bytes, method_name: str, timeout: Optional[float]) -> bytes: ex = RestRpcExecutor(reg, app) res, status = await ex.exec(request, method_name) return res