def test_batch_request_serialization(): request = v20.BatchRequest( v20.Request('method0', [], id=None), v20.Request('method1', [1, 2], id=1), v20.Request('method2', {'a': 1, 'b': 2}, id=None), ) actual_dict = request.to_json() expected_dict = [ { 'jsonrpc': '2.0', 'method': 'method0', }, { 'jsonrpc': '2.0', 'id': 1, 'method': 'method1', 'params': [1, 2], }, { 'jsonrpc': '2.0', 'method': 'method2', 'params': {'a': 1, 'b': 2}, }, ] assert actual_dict == expected_dict
def test_request_repr(): request = v20.Request(method='method', params={'a': 1, 'b': 2}) assert str(request) == "method(a=1,b=2)" assert repr(request) == "Request(method='method', params={'a': 1, 'b': 2}, id=None)" request = v20.Request(method='method', params=[1, 2], id=1) assert str(request) == "method(1, 2)" assert repr(request) == "Request(method='method', params=[1, 2], id=1)"
def test_errors(app, json_rpc, path, mocker): request_id = 1 params = (1, 2) method_name = 'test_method' def error_method(*args, **kwargs): raise exc.JsonRpcError(code=1, message='message') mock = mocker.Mock(name=method_name, side_effect=error_method) json_rpc.dispatcher.add(mock, method_name) with app.test_client() as cli: # method not found raw = cli.post(path, json=v20.Request(method='unknown_method', params=params, id=request_id).to_json()) assert raw.status_code == 200 resp = v20.Response.from_json(raw.json) assert resp.id is request_id assert resp.is_error is True assert resp.error == exc.MethodNotFoundError( data="method 'unknown_method' not found") # customer error raw = cli.post(path, json=v20.Request(method=method_name, params=params, id=request_id).to_json()) assert raw.status_code == 200 resp = v20.Response.from_json(raw.json) mock.assert_called_once_with(args=params) assert resp.id == request_id assert resp.is_error is True assert resp.error == exc.JsonRpcError(code=1, message='message') # content type error raw = cli.post(path, data='') assert raw.status_code == 415 # malformed json raw = cli.post(path, headers={'Content-Type': 'application/json'}, data='') assert raw.status_code == 200 resp = v20.Response.from_json(raw.json) assert resp.id is None assert resp.is_error is True assert resp.error == exc.ParseError(data=_) # decoding error raw = cli.post(path, headers={'Content-Type': 'application/json'}, data=b'\xff') assert raw.status_code == 400
def test_batch_request_repr(): request = v20.BatchRequest( v20.Request('method1', [1, 2]), v20.Request('method2', {'a': 1, 'b': 2}, id='2'), ) assert str(request) == "[method1(1, 2), method2(a=1,b=2)]" assert repr(request) == "BatchRequest(Request(method='method1', params=[1, 2], id=None)," \ "Request(method='method2', params={'a': 1, 'b': 2}, id='2'))"
def test_errors(json_rpc, path, mocker): request_id = 1 params = (1, 2) method_name = 'test_method' def error_method(*args, **kwargs): raise exc.JsonRpcError(code=1, message='message') mock = mocker.Mock(name=method_name, side_effect=error_method) json_rpc.dispatcher.add(mock, method_name) cli = werkzeug.test.Client(json_rpc) # method not found body_iter, code, header = cli.post( path, json=v20.Request(method='unknown_method', params=params, id=request_id).to_json(), ) body = b''.join(body_iter) assert code == '200 OK' resp = v20.Response.from_json(json.loads(body)) assert resp.id is request_id assert resp.is_error is True assert resp.error == exc.MethodNotFoundError( data="method 'unknown_method' not found") # customer error body_iter, code, header = cli.post( path, json=v20.Request(method=method_name, params=params, id=request_id).to_json(), ) body = b''.join(body_iter) assert code == '200 OK' resp = v20.Response.from_json(json.loads(body)) mock.assert_called_once_with(args=params) assert resp.id == request_id assert resp.is_error is True assert resp.error == exc.JsonRpcError(code=1, message='message') # malformed json body_iter, code, header = cli.post( path, headers={'Content-Type': 'application/json'}, data='') body = b''.join(body_iter) assert code == '200 OK' resp = v20.Response.from_json(json.loads(body)) assert resp.id is None assert resp.is_error is True assert resp.error == exc.ParseError(data=_)
async def test_context(json_rpc, path, mocker, aiohttp_client): request_id = 1 params = (1, 2) method_name = 'test_method' # test list parameters mock = mocker.Mock(name=method_name, return_value='result') json_rpc.dispatcher.add(mock, method_name, context='request') cli = await aiohttp_client(json_rpc.app) raw = await cli.post(path, json=v20.Request(method=method_name, params=params, id=request_id).to_json()) assert raw.status == 200 mock.assert_called_once() call_args = mock.call_args[1] context, args = call_args['request'], call_args['args'] assert isinstance(context, web.Request) assert args == params # test dict parameters params = {'param1': 1, 'param2': 2} mock.reset_mock() cli = await aiohttp_client(json_rpc.app) raw = await cli.post(path, json=v20.Request(method=method_name, params=params, id=request_id).to_json()) assert raw.status == 200 mock.assert_called_once() call_args = mock.call_args[1] context, kwargs = call_args['request'], call_args['kwargs'] assert isinstance(context, web.Request) assert kwargs == params
def test_notify(json_rpc, path, mocker): params = [1, 2] method_name = 'test_method' mock = mocker.Mock(name=method_name, return_value='result') json_rpc.dispatcher.add(mock, method_name) cli = werkzeug.test.Client(json_rpc) body_iter, code, header = cli.post( path, json=v20.Request(method=method_name, params=params).to_json()) body = b''.join(body_iter) assert code == '200 OK' assert body == b''
def test_request_serialization(id, params): request = v20.Request('method', params, id=id) actual_dict = request.to_json() expected_dict = { 'jsonrpc': '2.0', 'method': 'method', } if id is not None: expected_dict.update(id=id) if params: expected_dict.update(params=params) assert actual_dict == expected_dict
def test_notify(app, json_rpc, path, mocker): params = [1, 2] method_name = 'test_method' mock = mocker.Mock(name=method_name, return_value='result') json_rpc.dispatcher.add(mock, method_name) with app.test_client() as cli: raw = cli.post(path, json=v20.Request(method=method_name, params=params).to_json()) assert raw.status_code == 200 assert raw.is_json is False assert raw.data == b''
async def test_notify(json_rpc, path, mocker, aiohttp_client): params = [1, 2] method_name = 'test_method' mock = mocker.Mock(name=method_name, return_value='result') json_rpc.dispatcher.add(mock, method_name) cli = await aiohttp_client(json_rpc.app) raw = await cli.post(path, json=v20.Request(method=method_name, params=params).to_json()) assert raw.status == 200 assert raw.content_type != 'application/json' assert await raw.read() == b''
def test_request(app, json_rpc, path, mocker, request_id, params, result): method_name = 'test_method' mock = mocker.Mock(name=method_name, return_value=result) json_rpc.dispatcher.add(mock, method_name) with app.test_client() as cli: raw = cli.post(path, json=v20.Request(method=method_name, params=params, id=request_id).to_json()) assert raw.status_code == 200 resp = v20.Response.from_json(raw.json) if isinstance(params, dict): mock.assert_called_once_with(kwargs=params) else: mock.assert_called_once_with(args=params) assert resp.id == request_id assert resp.result == result
def test_batch_request_methods(): request = v20.BatchRequest( v20.Request('method1', [1], id=None), v20.Request('method2', [1], id=None), v20.Request('method3', [1], id=1), ) assert len(request) == 3 assert not request.is_notification request.append(v20.Request('method4', [2], id=2)) assert len(request) == 4 request.extend([ v20.Request('method5', [3], id=3), v20.Request('method6', [4], id=4), ]) assert len(request) == 6 request = v20.BatchRequest( v20.Request(id=None, method='method1'), v20.Request(id=None, method='method2'), ) assert request.is_notification
async def test_request(json_rpc, path, mocker, aiohttp_client, request_id, params, result): method_name = 'test_method' mock = mocker.Mock(name=method_name, return_value=result) json_rpc.dispatcher.add(mock, method_name) cli = await aiohttp_client(json_rpc.app) raw = await cli.post(path, json=v20.Request(method=method_name, params=params, id=request_id).to_json()) assert raw.status == 200 resp = v20.Response.from_json(await raw.json()) if isinstance(params, dict): mock.assert_called_once_with(kwargs=params) else: mock.assert_called_once_with(args=params) assert resp.id == request_id assert resp.result == result
def test_request(json_rpc, path, mocker, request_id, params, result): method_name = 'test_method' mock = mocker.Mock(name=method_name, return_value=result) json_rpc.dispatcher.add(mock, method_name) cli = werkzeug.test.Client(json_rpc) body_iter, code, header = cli.post( path, json=v20.Request(method=method_name, params=params, id=request_id).to_json(), ) body = b''.join(body_iter) assert code == '200 OK' resp = v20.Response.from_json(json.loads(body)) if isinstance(params, dict): mock.assert_called_once_with(kwargs=params) else: mock.assert_called_once_with(args=params) assert resp.id == request_id assert resp.result == result
def test_request_properties(): request = v20.Request('method') assert request.is_notification is True
def test_batch_request_errors(): with pytest.raises(exceptions.IdentityError): v20.BatchRequest( v20.Request(id=1, method='method'), v20.Request(id=1, method='method'), )