Esempio n. 1
0
 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"},
     ])
Esempio n. 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)
Esempio n. 3
0
 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
Esempio n. 4
0
 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,
     })
Esempio n. 5
0
 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,
     })
Esempio n. 6
0
    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))
Esempio n. 7
0
 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)
Esempio n. 8
0
 def test_validation_id(self):
     response = JSONRPCSingleResponse(self.result, request=self.request)
     self.assertEqual(response.id, self.request.id)
Esempio n. 9
0
 def test_validation_error_incorrect_no_message(self):
     d = deepcopy(self.error_data)
     del d["message"]
     with self.assertRaises(ValueError):
         JSONRPCSingleResponse(d, error=True)
Esempio n. 10
0
 def test_validation_error_payload_type(self):
     with self.assertRaises(TypeError):
         JSONRPCSingleResponse(None, error=True)
Esempio n. 11
0
 def test_validation_error_incorrect(self):
     d = deepcopy(self.error_data)
     d.update({"code": "str"})
     with self.assertRaises(ValueError):
         JSONRPCSingleResponse(d, error=True)
Esempio n. 12
0
 def test_validation_error_correct(self):
     JSONRPCSingleResponse(self.error_data, error=True)
Esempio n. 13
0
 def test_validation_incorrect_result_and_error(self):
     with self.assertRaises(ValueError):
         JSONRPCSingleResponse(self.error_data, error=False)
Esempio n. 14
0
 def test_validation_incorrect_no_parameters(self):
     with self.assertRaises(TypeError):
         JSONRPCSingleResponse()
Esempio n. 15
0
 def test_correct_init(self):
     """ Test object is created."""
     JSONRPCSingleResponse(self.result, request=self.request)