class JSONRPCTestValidateParameters(unittest.TestCase): def setUp(self): api = getapi() self.jsonrpc = SLOJSONRPC(sessionfaker()) self.api = api() self.jsonrpc.register(api()) def test_invalid_method(self): self.assertEqual(-32601, self.jsonrpc.handle_request({"jsonrpc": "2.0", "method": "fdsa"})["error"]["code"]) def test_too_many_params(self): self.assertEqual( -32602, self.jsonrpc.handle_request( {"jsonrpc": "2.0", "method": "ping", "params": {"a": 1, "b": 1, "c": 2}, "id": 1} )["error"]["code"], ) def test_too_many_params_no_param_method(self): self.assertEqual( -32602, self.jsonrpc.handle_request({"jsonrpc": "2.0", "method": "ping", "params": {"a": 1}, "id": 1})["error"][ "code" ], ) def test_not_enough_params(self): self.assertEqual( -32602, self.jsonrpc.handle_request({"jsonrpc": "2.0", "method": "pong", "params": {"a": 1}, "id": 1})["error"][ "code" ], ) def test_no_params(self): self.assertEqual( -32602, self.jsonrpc.handle_request({"jsonrpc": "2.0", "method": "pong", "id": 1})["error"]["code"] ) def test_invalid_params(self): self.assertEqual( -32602, self.jsonrpc.handle_request({"jsonrpc": "2.0", "method": "pong", "params": 1, "id": 1})["error"]["code"], ) def test_id_in_notification(self): self.assertEqual( -32602, self.jsonrpc.handle_request({"jsonrpc": "2.0", "method": "do", "id": 1})["error"]["code"] ) def test_no_id_in_non_notification(self): self.assertEqual(-32602, self.jsonrpc.handle_request({"jsonrpc": "2.0", "method": "ping"})["error"]["code"])
class JSONRPCTestString(unittest.TestCase): def setUp(self): api = getapi() self.jsonrpc = SLOJSONRPC(sessionfaker()) self.api = api() self.jsonrpc.register(api()) def test_request(self): self.assertEqual( json.loads('{"jsonrpc": "2.0", "id": 1, "result": "pong"}'), json.loads(self.jsonrpc.handle_string('{"jsonrpc": "2.0", "method": "ping", "id": 1}')) ) def test_invalidjson(self): self.assertEqual( json.loads('{"jsonrpc": "2.0", "id": null, "error": {"message": "Parse error", "code": -32700}}'), json.loads(self.jsonrpc.handle_string('"jsonrpc": "2.0", "method": "ping", "id": 1')) ) def test_multiple_requests(self): self.assertEqual( json.loads('[{"jsonrpc": "2.0", "id": 1, "result": "pong"},{"jsonrpc": "2.0", "id": 2, "result": "2"}]'), json.loads(self.jsonrpc.handle_string('[{"jsonrpc": "2.0", "method": "ping", "id": 1},' + '{"jsonrpc": "2.0", "method": "one", "id": 2, "params": "2"}]')) ) def test_multiple_requests_one_nodict(self): self.assertEqual( json.loads('{"jsonrpc": "2.0", "id": null, "error":{"message": "Parse error", "code": -32700}}'), json.loads(self.jsonrpc.handle_string('[{"jsonrpc": "2.0", "method": "ping", "id": 1},[]]')) ) def test_multiple_requests_one_invalid(self): self.assertEqual( json.loads('{"jsonrpc": "2.0", "id": null, "error": {"message": "Invalid Request", "code": -32600}}'), json.loads(self.jsonrpc.handle_string('[{"jsonrpc": "2.0", "method": "ping", "id": 1},{"method": "bla"}]')) ) def test_invalidtype(self): self.assertEqual( json.loads('{"jsonrpc": "2.0", "id": null, "error": {"message": "Parse error", "code": -32700}}'), json.loads(self.jsonrpc.handle_string('2')) )
class JSONRPCTestValidateFormat(unittest.TestCase): def setUp(self): self.jsonrpc = SLOJSONRPC(sessionfaker()) def test_no_jsonrpc(self): try: self.jsonrpc._validate_format({'method': 'do'}) except SLOJSONRPCError as err: self.assertEqual(err.errorcode, -32600) else: self.fail('Expected Error') def test_no_method(self): try: self.jsonrpc._validate_format({'jsonrpc': '2.0'}) except SLOJSONRPCError as err: self.assertEqual(err.errorcode, -32600) else: self.fail('Expected Error') def test_wrong_version(self): try: self.jsonrpc._validate_format({'jsonrpc': '2.1', 'method': 'do'}) except SLOJSONRPCError as err: self.assertEqual(err.errorcode, -32600) else: self.fail('Expected Error') def test_invalid_key(self): try: self.jsonrpc._validate_format({'jsonrpc': '2.1', 'method': 'do', 'fd': None}) except SLOJSONRPCError as err: self.assertEqual(err.errorcode, -32600) else: self.fail('Expected Error')
def test_integrated_ping(self): self.jsonrpc = SLOJSONRPC(sessionfaker()) self.assertEqual('pong', self.jsonrpc.handle_request({ 'jsonrpc': '2.0', 'method': 'ping', 'id': 1 })['result'])
def setUp(self): api = getapi() self.jsonrpc = SLOJSONRPC(sessionfaker()) self.api = api() self.jsonrpc.register(api())
class JSONRPCTest(unittest.TestCase): def setUp(self): api = getapi() self.jsonrpc = SLOJSONRPC(sessionfaker()) self.api = api() self.jsonrpc.register(api()) def test_notification(self): self.assertEqual(None, self.jsonrpc.handle_request({ 'jsonrpc': '2.0', 'method': 'do' })) def test_noparam(self): self.assertEqual('pong', self.jsonrpc.handle_request({ 'jsonrpc': '2.0', 'method': 'ping', 'id': 1 })['result']) def test_oneparam(self): self.assertEqual('pang', self.jsonrpc.handle_request({ 'jsonrpc': '2.0', 'method': 'one', 'id': 1, 'params': 'pang' })['result']) def test_multiparam(self): self.assertEqual('abcd', self.jsonrpc.handle_request({ 'jsonrpc': '2.0', 'method': 'pong', 'id': 1, 'params': {'a': 'ab', 'b': 'cd'} })['result']) def test_multiparam_defaults(self): self.assertEqual('aba', self.jsonrpc.handle_request({ 'jsonrpc': '2.0', 'method': 'defping', 'id': 1, 'params': {'a': 'ab'} })['result']) def test_integrated_ping(self): self.jsonrpc = SLOJSONRPC(sessionfaker()) self.assertEqual('pong', self.jsonrpc.handle_request({ 'jsonrpc': '2.0', 'method': 'ping', 'id': 1 })['result']) def test_invalid_error(self): self.assertEqual(-32603, self.jsonrpc.handle_request({ 'jsonrpc': '2.0', 'method': 'invalid_error', 'id': 1 })['error']['code']) def test_internal_error(self): self.assertEqual(-32001, self.jsonrpc.handle_request({ 'jsonrpc': '2.0', 'method': 'internal_error', 'id': 1 })['error']['code']) def test_uncaught_exception(self): self.assertEqual(-32603, self.jsonrpc.handle_request({ 'jsonrpc': '2.0', 'method': 'uncaught_exception', 'id': 1 })['error']['code'])
def setUp(self): self.jsonrpc = SLOJSONRPC(sessionfaker())