def test_batch_response(self): response = JSONRPCBatchResponse([ JSONRPCSingleResponse("pong", request=JSONRPCSingleRequest({"method": "ping", "jsonrpc": "2.0", "id": 1})), JSONRPCSingleResponse({"code": 0, "message": ""}, error=True), ]) self.assertEqual(json.loads(response.json), [ {"result": "pong", "id": 1, "jsonrpc": "2.0"}, {"error": {"code": 0, "message": ""}, "id": None, "jsonrpc": "2.0"}, ])
def test_response_iterator(self): request_1 = JSONRPCSingleRequest({"method": "add", "params": [1, 2], "jsonrpc": "2.0", "id": 1}) request_2 = JSONRPCSingleRequest({"method": "mul", "params": [7, 3], "jsonrpc": "2.0", "id": 2}) request_3 = JSONRPCSingleRequest({"method": "ping", "jsonrpc": "2.0"}) result_1 = 2 result_2 = 21 result_3 = "pong" responses = JSONRPCBatchResponse([ JSONRPCSingleResponse(result_1, request=request_1), JSONRPCSingleResponse(result_2, request=request_2), JSONRPCSingleResponse(result_3, request=request_3) ]) for response in responses: self.assertIsInstance(response, JSONRPCSingleResponse)
def process(self, dispatcher): """ Process request with method taken from dispatcher registry :type dispatcher: Dispatcher :rtype: JSONRPCSingleResponse or None """ output = None try: method = dispatcher[self.method] except KeyError: output = JSONRPCMethodNotFound().as_response() else: try: result = method(*self.args, **self.kwargs) except TypeError: output = JSONRPCInvalidParams().as_response() except Exception as e: data = {'type': e.__class__.__name__, 'message': str(e)} output = JSONRPCServerError(data=data).as_response() else: output = JSONRPCSingleResponse( result, request=self, serialize_hook=self.serialize_hook, deserialize_hook=self.deserialize_hook) finally: if not self.is_notification: return output
def test_container_result(self): r = JSONRPCSingleResponse(self.result, self.request) self.assertEqual(json.loads(r.json), r.container) self.assertEqual(r.container, { "jsonrpc": "2.0", "result": 3, "id": 1, })
def test_container_error(self): r = JSONRPCSingleResponse(self.error_data, error=True) self.assertEqual(json.loads(r.json), r.container) self.assertEqual(r.container, { "jsonrpc": "2.0", "error": { "code": 1, "message": "error", }, "id": None, })
def test_batch_response_from_single(self): request = JSONRPCSingleRequest({"method": "ping", "jsonrpc": "2.0", "id": 1}) result = "pong" response = JSONRPCSingleResponse(result, request=request) batch_with_list = JSONRPCBatchResponse([response]) batch_without_list = JSONRPCBatchResponse(response) self.assertTrue(isjsonequal(batch_with_list.json, batch_without_list.json)) iter(batch_with_list) iter(batch_without_list) iter(response) self.assertEqual(len(batch_with_list), 1) self.assertEqual(len(batch_without_list), 1) self.assertTrue(isjsonequal(response.json, batch_with_list[0].json)) self.assertTrue(isjsonequal(response.json, batch_without_list[0].json))
def test_validation_error_incorrect_message_not_str(self): d = deepcopy(self.error_data) d.update({"message": 0}) with self.assertRaises(ValueError): JSONRPCSingleResponse(d, error=True)
def test_validation_id(self): response = JSONRPCSingleResponse(self.result, request=self.request) self.assertEqual(response.id, self.request.id)
def test_validation_error_incorrect_no_message(self): d = deepcopy(self.error_data) del d["message"] with self.assertRaises(ValueError): JSONRPCSingleResponse(d, error=True)
def test_validation_error_payload_type(self): with self.assertRaises(TypeError): JSONRPCSingleResponse(None, error=True)
def test_validation_error_incorrect(self): d = deepcopy(self.error_data) d.update({"code": "str"}) with self.assertRaises(ValueError): JSONRPCSingleResponse(d, error=True)
def test_validation_error_correct(self): JSONRPCSingleResponse(self.error_data, error=True)
def test_validation_incorrect_result_and_error(self): with self.assertRaises(ValueError): JSONRPCSingleResponse(self.error_data, error=False)
def test_validation_incorrect_no_parameters(self): with self.assertRaises(TypeError): JSONRPCSingleResponse()
def test_correct_init(self): """ Test object is created.""" JSONRPCSingleResponse(self.result, request=self.request)