jsonrpc-base

Pure python standard library JSONRPC data interface
git clone git://git.defalsify.org/python-jsonrpc-base.git
Info | Log | Files | Refs | LICENSE

test_parse.py (2961B)


      1 # standard imports
      2 import unittest
      3 import io
      4 
      5 # local imports
      6 from jsonrpc_std.parse import *
      7 from jsonrpc_std.error import (
      8         JSONRPCParseError,
      9         )
     10 
     11 
     12 class TestParse(unittest.TestCase):
     13 
     14 
     15     def setUp(self):
     16         self.valid_o = {
     17         'jsonrpc': '2.0',
     18         'id': 42,
     19         'method': 'foo_bazBaz',
     20         'params': [13, {
     21             'xyzzy': 666,
     22             'inky': ['pinky', 'blinky', 'clyde'],
     23             },
     24             ],
     25         }
     26 
     27     def test_from_dict(self):
     28         r = jsonrpc_from_dict(self.valid_o)
     29         self.assertEqual(r['id'], self.valid_o['id'])
     30         self.assertEqual(r['params'], self.valid_o['params'])
     31         self.assertEqual(r['method'], self.valid_o['method'])
     32 
     33         s = json.dumps(self.valid_o)
     34         r = jsonrpc_from_str(s)
     35         self.assertEqual(r['id'], self.valid_o['id'])
     36         self.assertEqual(r['params'], self.valid_o['params'])
     37         self.assertEqual(r['method'], self.valid_o['method'])
     38 
     39         r = jsonrpc_from_file(io.BytesIO(s.encode('utf-8')))
     40         self.assertEqual(r['id'], self.valid_o['id'])
     41         self.assertEqual(r['params'], self.valid_o['params'])
     42         self.assertEqual(r['method'], self.valid_o['method'])
     43 
     44 
     45 
     46     def test_missing_version(self):
     47         o = self.valid_o
     48         del o['jsonrpc']
     49         with self.assertRaises(JSONRPCParseError):
     50             jsonrpc_from_dict(o)
     51 
     52 
     53     def test_id(self):
     54         o = self.valid_o
     55         del o['id']
     56         with self.assertRaises(JSONRPCParseError):
     57             jsonrpc_from_dict(o)
     58 
     59         o['id'] = None
     60         with self.assertRaises(JSONRPCParseError):
     61             jsonrpc_from_dict(o)
     62 
     63         o['id'] = {}
     64         with self.assertRaises(JSONRPCInvalidRequestError):
     65             jsonrpc_from_dict(o)
     66 
     67         o['id'] = JSONRPCBase()
     68         with self.assertRaises(JSONRPCInvalidRequestError):
     69             jsonrpc_from_dict(o)
     70 
     71 
     72     def test_method(self):
     73         o = self.valid_o
     74         del o['method']
     75 
     76         with self.assertRaises(JSONRPCParseError):
     77             jsonrpc_from_dict(o)
     78 
     79         o['method'] = None
     80         with self.assertRaises(JSONRPCParseError):
     81             jsonrpc_from_dict(o)
     82 
     83         o['method'] = 42
     84         with self.assertRaises(JSONRPCInvalidRequestError):
     85             jsonrpc_from_dict(o)
     86 
     87 
     88         o['method'] = {}
     89         with self.assertRaises(JSONRPCInvalidRequestError):
     90             jsonrpc_from_dict(o)
     91 
     92         o['method'] = JSONRPCBase()
     93         with self.assertRaises(JSONRPCInvalidRequestError):
     94             jsonrpc_from_dict(o)
     95 
     96 
     97     def test_params(self):
     98         o = self.valid_o
     99         del o['params']
    100 
    101         with self.assertRaises(JSONRPCParseError):
    102             jsonrpc_from_dict(o)
    103 
    104         o['params'] = None
    105         with self.assertRaises(JSONRPCParseError):
    106             jsonrpc_from_dict(o)
    107 
    108         o['params'] = {}
    109         with self.assertRaises(JSONRPCParseError):
    110             jsonrpc_from_dict(o)
    111 
    112 
    113 if __name__ == '__main__':
    114     unittest.main()