Esempio n. 1
0
    def test_method_validation_not_str(self):
        self.request_params.update({"method": []})
        with self.assertRaises(JSONRPCInvalidRequestException):
            JSONRPCSingleRequest(self.request_params)

        self.request_params.update({"method": {}})
        with self.assertRaises(JSONRPCInvalidRequestException):
            JSONRPCSingleRequest(self.request_params)
Esempio n. 2
0
    def test_incorrect_empty_request(self):
        request = {}

        with self.assertRaises(JSONRPCInvalidRequestException):
            JSONRPCSingleRequest(json.dumps(request))

        with self.assertRaises(JSONRPCInvalidRequestException):
            JSONRPCSingleRequest(request)
Esempio n. 3
0
    def test_id_validation_incorrect(self):
        self.request_params.update({"id": []})
        with self.assertRaises(JSONRPCInvalidRequestException):
            JSONRPCSingleRequest(self.request_params)

        self.request_params.update({"id": ()})
        with self.assertRaises(JSONRPCInvalidRequestException):
            JSONRPCSingleRequest(self.request_params)
Esempio n. 4
0
 def test_batch_request(self):
     request = JSONRPCBatchRequest([
         JSONRPCSingleRequest({"method": "devide", "params": {"num": 1, "denom": 2}, "id": 1, "jsonrpc": "2.0"}),
         JSONRPCSingleRequest({"method": "devide", "params": {"num": 3, "denom": 2}, "id": 2, "jsonrpc": "2.0"}),
     ])
     self.assertEqual(json.loads(request.json), [
         {"method": "devide", "params": {"num": 1, "denom": 2}, "id": 1, "jsonrpc": "2.0"},
         {"method": "devide", "params": {"num": 3, "denom": 2}, "id": 2, "jsonrpc": "2.0"},
     ])
     self.assertTrue(request)
Esempio n. 5
0
 def test_response_iterator(self):
     requests = [
         JSONRPCSingleRequest({"method": "devide", "params": {"num": 1, "denom": 2}, "id": 1, "jsonrpc": "2.0"}),
         JSONRPCSingleRequest({"method": "devide", "params": {"num": 3, "denom": 2}, "id": 2, "jsonrpc": "2.0"}),
     ]
     batch = JSONRPCBatchRequest(requests)
     for request in batch:
         self.assertIsInstance(request, JSONRPCSingleRequest)
         self.assertIsInstance(batch, JSONRPCBatchRequest)
         self.assertEqual(request.method, "devide")
Esempio n. 6
0
    def test_set_unset_notification_keep_id(self):
        r = JSONRPCSingleRequest({"jsonrpc": "2.0", "method": "add", "id": 0})
        r.is_notification = True
        self.assertTrue(r.is_notification)
        self.assertNotIn("id", r.data)

        r.is_notification = False
        self.assertFalse(r.is_notification)
        self.assertTrue("id" in r.data)
        self.assertEqual(r.data["id"], 0)
Esempio n. 7
0
    def test_data_setter(self):
        request = JSONRPCSingleRequest(self.request_params)
        with self.assertRaises(JSONRPCMultipleRequestException):
            request.data = []

        with self.assertRaises(JSONRPCParseException):
            request.data = ""

        with self.assertRaises(JSONRPCInvalidRequestException):
            request.data = None
Esempio n. 8
0
    def test_incorrect_multiple_json(self):
        lst = [
            {"method": "devide", "params": {"num": 1, "denom": 2}, "id": 1, "jsonrpc": "2.0"},
            {"method": "devide", "params": {"num": 3, "denom": 2}, "id": 2, "jsonrpc": "2.0"}
        ]

        with self.assertRaises(JSONRPCMultipleRequestException):
            JSONRPCSingleRequest(json.dumps(lst))

        with self.assertRaises(JSONRPCMultipleRequestException):
            JSONRPCSingleRequest(lst)
Esempio n. 9
0
    def test_method_validation_str_rpc_prefix(self):
        """ Test method SHOULD NOT starts with rpc. """
        self.request_params.update({"method": "rpc."})
        with self.assertRaises(JSONRPCInvalidRequestException):
            JSONRPCSingleRequest(self.request_params)

        self.request_params.update({"method": "rpc.test"})
        with self.assertRaises(JSONRPCInvalidRequestException):
            JSONRPCSingleRequest(self.request_params)

        self.request_params.update({"method": "rpccorrect"})
        JSONRPCSingleRequest(self.request_params)

        self.request_params.update({"method": "rpc"})
        JSONRPCSingleRequest(self.request_params)
Esempio n. 10
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. 11
0
    def test_from_json_invalid_request_method(self):
        str_json = json.dumps({
            "jsonrpc": "2.0",
        })

        with self.assertRaises(JSONRPCInvalidRequestException):
            JSONRPCSingleRequest(str_json)
Esempio n. 12
0
 def test_serialize_method_1(self):
     r = JSONRPCSingleRequest({"jsonrpc": "2.0", "method": "add"})
     self.assertEqual(
         {
             "jsonrpc": "2.0",
             "method": "add",
         }, json.loads(r.json))
Esempio n. 13
0
 def test_data_id_3(self):
     r = JSONRPCSingleRequest({"jsonrpc": "2.0", "method": "add", "id": "id"})
     self.assertEqual(r.data, {
         "jsonrpc": "2.0",
         "method": "add",
         "id": "id",
     })
Esempio n. 14
0
    def handle(self, request_string, dispatcher):
        """
        Method brings syntactic sugar into library.
        Given dispatcher it handles request (both single and batch) and handles errors.
        Request could be handled in parallel, it is server responsibility.

        :param request_string: JSON string.
            Will be converted into JSONRPCSingleRequest or JSONRPCBatchRequest
        :type request_string: str
        :type dispatcher: Dispatcher or dict
        :rtype: JSONRPCSingleResponse or JSONRPCBatchResponse
        """

        try:
            data = self.deserialize(request_string)
            if isinstance(data, list):
                request = JSONRPCBatchRequest(
                    data, serialize_hook=self.serialize_hook)
            elif isinstance(data, dict):
                request = JSONRPCSingleRequest(
                    data, serialize_hook=self.serialize_hook)
            else:
                raise JSONRPCInvalidRequestException
        except (TypeError, ValueError, JSONRPCParseException):
            return JSONRPCParseError().as_response()
        except JSONRPCInvalidRequestException:
            return JSONRPCInvalidRequest().as_response()
        else:
            return request.process(dispatcher)
Esempio n. 15
0
 def test_data_params_6(self):
     r = JSONRPCSingleRequest({"jsonrpc": "2.0", "method": "add", "params": {"a": 0}, "id": 1})
     self.assertEqual(r.data, {
         "jsonrpc": "2.0",
         "method": "add",
         "params": {"a": 0},
         "id": 1,
     })
Esempio n. 16
0
    def test_from_json_invalid_request_extra_data(self):
        str_json = json.dumps({
            "jsonrpc": "2.0",
            "method": "add",
            "is_notification": True,
        })

        with self.assertRaises(JSONRPCInvalidRequestException):
            JSONRPCSingleRequest(str_json)
Esempio n. 17
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"},
     ])
 def test_server_error(self):
     request = JSONRPCSingleRequest({'jsonrpc': '2.0', 'method': 'error', 'id': 0})
     response = self.manager.handle(request.json, self.dispatcher)
     self.assertIsInstance(response, JSONRPCSingleResponse)
     self.assertEqual(response.error["message"], "Server error")
     self.assertEqual(response.error["code"], -32000)
     self.assertEqual(response.error["data"], {
         "type": "Exception",
         "message": 'error_explanation',
     })
Esempio n. 19
0
 def test_serialize_params_5(self):
     r = JSONRPCSingleRequest({"jsonrpc": "2.0", "method": "add", "params": {"a": 0}, "id": 1})
     self.assertEqual(
         {
             "jsonrpc": "2.0",
             "method": "add",
             "params": {"a": 0},
             "id": 1,
         },
         json.loads(r.json)
     )
Esempio n. 20
0
    def test_from_json_request_no_params(self):
        str_json = json.dumps({
            "method": "add",
            "jsonrpc": "2.0",
        })

        request = JSONRPCSingleRequest(str_json)
        self.assertIsInstance(request, JSONRPCSingleRequest)
        self.assertEqual(request.method, "add")
        self.assertEqual(request.params, None)
        self.assertEqual(request.id, None)
        self.assertTrue(request.is_notification)
Esempio n. 21
0
    def test_from_json_batch_one(self):
        req_data = {"method": "add", "params": [1, 2], "jsonrpc": "2.0", "id": 1}

        request = JSONRPCSingleRequest(req_data)
        requests = JSONRPCBatchRequest(request)
        self.assertIsInstance(requests, JSONRPCBatchRequest)
        self.assertEqual(len(requests), 1)
        r = requests[0]
        self.assertIsInstance(r, JSONRPCSingleRequest)
        self.assertEqual(r.method, "add")
        self.assertEqual(r.params, [1, 2])
        self.assertEqual(r.id, 1)
        self.assertFalse(r.is_notification)
Esempio n. 22
0
    def test_from_json_request(self):
        str_json = json.dumps({
            "method": "add",
            "params": [0, 1],
            "jsonrpc": "2.0",
            "id": "id",
        })

        request = JSONRPCSingleRequest(str_json)
        self.assertIsInstance(request, JSONRPCSingleRequest)
        self.assertEqual(request.method, "add")
        self.assertEqual(request.params, [0, 1])
        self.assertEqual(request.id, "id")
        self.assertFalse(request.is_notification)
Esempio n. 23
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. 24
0
    def test_is_notification(self):
        r = JSONRPCSingleRequest({"jsonrpc": "2.0", "method": "add"})
        self.assertTrue(r.is_notification)

        r = JSONRPCSingleRequest({"jsonrpc": "2.0", "method": "add", "id": 1})
        self.assertFalse(r.is_notification)

        r = JSONRPCSingleRequest({"jsonrpc": "2.0", "method": "add", "id": 'null'})
        self.assertFalse(r.is_notification)

        r = JSONRPCSingleRequest({"jsonrpc": "2.0", "method": "add", "id": 0})
        self.assertFalse(r.is_notification)

        r = JSONRPCSingleRequest({"jsonrpc": "2.0", "method": "add", "id": 1})
        r.is_notification = True
        self.assertTrue(r.is_notification)
        self.assertNotIn("id", r.data)

        r = JSONRPCSingleRequest({"jsonrpc": "2.0", "method": "add", "id": 0})
        r.is_notification = True
        self.assertTrue(r.is_notification)
        self.assertNotIn("id", r.data)
Esempio n. 25
0
 def test_request_args(self):
     req_data = {"jsonrpc": "2.0", "method": "add", "id": 1}
     self.assertEqual(JSONRPCSingleRequest(req_data).args, ())
     self.assertEqual(JSONRPCSingleRequest(dict(req_data, params=[])).args, ())
     self.assertEqual(JSONRPCSingleRequest(dict(req_data, params={"a": 1})).args, ())
     self.assertEqual(JSONRPCSingleRequest(dict(req_data, params=[1, 2])).args, (1, 2))
Esempio n. 26
0
 def test_params_validation_incorrect(self):
     self.request_params.update({"params": "str"})
     with self.assertRaises(JSONRPCInvalidRequestException):
         JSONRPCSingleRequest(self.request_params)
Esempio n. 27
0
 def test_params_validation_none(self):
     self.request_params.update({"params": None})
     with self.assertRaises(JSONRPCInvalidRequestException):
         JSONRPCSingleRequest(self.request_params)
Esempio n. 28
0
    def test_params_validation_dict(self):
        self.request_params.update({"params": {}})
        JSONRPCSingleRequest(self.request_params)

        self.request_params.update({"params": {"a": 0}})
        JSONRPCSingleRequest(self.request_params)
Esempio n. 29
0
    def test_params_validation_tuple(self):
        self.request_params.update({"params": ()})
        JSONRPCSingleRequest(self.request_params)

        self.request_params.update({"params": tuple([0])})
        JSONRPCSingleRequest(self.request_params)
Esempio n. 30
0
    def test_params_validation_list(self):
        self.request_params.update({"params": []})
        JSONRPCSingleRequest(self.request_params)

        self.request_params.update({"params": [0]})
        JSONRPCSingleRequest(self.request_params)
Esempio n. 31
0
 def setUpClass(cls):
     cls.request = JSONRPCSingleRequest({"method": "add", "params": [1, 2], "jsonrpc": "2.0", "id": 1})
     cls.result = 3
     cls.error_data = {"code": 1, "message": "error"}