示例#1
0
def main():
    server = jsonrpclib.Server('http://localhost:8080')

    for _ in range(1):
        print server.add(a=30, b=6)
        print server.sub(30, 6)
        print server.mul(a=30, b=6)
        print server.div(30, 6)

    if 0:
        print jsonrpclib.history.request
        print jsonrpclib.history.response

    if 1:
        server.trythisone()

    if 0:
        batch = jsonrpclib.MultiCall(server)
        batch.add(5, 6)
        batch.ping({'key': 'value'})
        results = batch()
        print([x for x in results])

    if 0:
        print server.printme()
        print server.printme(verbose=True)

    if 0:
        server.givememoney()
示例#2
0
 def test_batch_notifications(self):
     """ Tests batch notifications """
     multicall = jsonrpclib.MultiCall(self.client)
     multicall._notify.notify_sum(1, 2, 4)
     multicall._notify.notify_hello(7)
     result = multicall()
     self.assertTrue(len(result) == 0)
     valid_request = json.loads(
         '[{"jsonrpc": "2.0", "method": "notify_sum", ' +
         '"params": [1,2,4]},{"jsonrpc": "2.0", ' +
         '"method": "notify_hello", "params": [7]}]')
     request = json.loads(self.history.request)
     self.assertTrue(len(request) == len(valid_request))
     for req, valid_req in zip(request, valid_request):
         self.assertTrue(req == valid_req)
     self.assertTrue(self.history.response == "")
示例#3
0
 def test_batch_notifications(self):
     multicall = jsonrpclib.MultiCall(self.client)
     multicall._notify.notify_sum(1, 2, 4)
     multicall._notify.notify_hello(7)
     result = multicall()
     self.assertTrue(len(result) == 0)
     valid_request = json.loads(
         '[{"jsonrpc": "2.0", "method": "notify_sum", ' +
         '"params": [1,2,4]},{"jsonrpc": "2.0", ' +
         '"method": "notify_hello", "params": [7]}]')
     request = json.loads(self.history.request)
     self.assertTrue(len(request) == len(valid_request))
     for i in range(len(request)):
         req = request[i]
         valid_req = valid_request[i]
         self.assertTrue(req == valid_req)
     self.assertTrue(self.history.response == '')
示例#4
0
 def get_multicall_client(self):
     server = self.get_client()
     return jsonrpclib.MultiCall(server)
示例#5
0
    def test_batch(self):
        """ Tests batch call """
        multicall = jsonrpclib.MultiCall(self.client)
        multicall.sum(1, 2, 4)
        multicall._notify.notify_hello(7)
        multicall.subtract(42, 23)
        multicall.foo.get(name="myself")
        multicall.get_data()
        job_requests = [j.request() for j in multicall._job_list]
        job_requests.insert(3, '{"foo": "boo"}')
        json_requests = "[%s]" % ",".join(job_requests)
        requests = json.loads(json_requests)
        responses = self.client._run_request(json_requests)

        verify_requests = json.loads(
            """[
            {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
            {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
            {"jsonrpc": "2.0", "method": "subtract",
             "params": [42,23], "id": "2"},
            {"foo": "boo"},
            {"jsonrpc": "2.0", "method": "foo.get",
             "params": {"name": "myself"}, "id": "5"},
            {"jsonrpc": "2.0", "method": "get_data", "id": "9"}
        ]"""
        )

        # Thankfully, these are in order so testing is pretty simple.
        verify_responses = json.loads(
            """[
            {"jsonrpc": "2.0", "result": 7, "id": "1"},
            {"jsonrpc": "2.0", "result": 19, "id": "2"},
            {"jsonrpc": "2.0",
             "error": {"code": -32600, "message": "Invalid Request."},
             "id": null},
            {"jsonrpc": "2.0",
             "error": {"code": -32601, "message": "Method not found."},
             "id": "5"},
            {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
        ]"""
        )

        self.assertTrue(len(requests) == len(verify_requests))
        self.assertTrue(len(responses) == len(verify_responses))

        responses_by_id = {}
        response_i = 0

        for verify_request, request in zip(verify_requests, requests):
            response = None
            if request.get("method") != "notify_hello":
                req_id = request.get("id")
                if "id" in verify_request:
                    verify_request["id"] = req_id
                verify_response = verify_responses[response_i]
                verify_response["id"] = req_id
                responses_by_id[req_id] = verify_response
                response_i += 1
                response = verify_response
            self.assertTrue(request == verify_request)

        for response in responses:
            verify_response = responses_by_id.get(response.get("id"))
            if "error" in verify_response:
                verify_response["error"]["message"] = response["error"][
                    "message"
                ]
            self.assertTrue(response == verify_response)
示例#6
0
    def test_batch(self):
        multicall = jsonrpclib.MultiCall(self.client)
        multicall.sum(1, 2, 4)
        multicall._notify.notify_hello(7)
        multicall.subtract(42, 23)
        multicall.foo.get(name='myself')
        multicall.get_data()
        job_requests = [j.request() for j in multicall._job_list]
        job_requests.insert(3, '{"foo": "boo"}')
        json_requests = '[%s]' % ','.join(job_requests)
        requests = json.loads(json_requests)
        responses = self.client._run_request(json_requests)

        verify_requests = json.loads("""[
            {"jsonrpc": "2.0", "method": "sum", "params": [1,2,4], "id": "1"},
            {"jsonrpc": "2.0", "method": "notify_hello", "params": [7]},
            {"jsonrpc": "2.0", "method": "subtract",
             "params": [42,23], "id": "2"},
            {"foo": "boo"},
            {"jsonrpc": "2.0", "method": "foo.get",
             "params": {"name": "myself"}, "id": "5"},
            {"jsonrpc": "2.0", "method": "get_data", "id": "9"}
        ]""")

        # Thankfully, these are in order so testing is pretty simple.
        verify_responses = json.loads("""[
            {"jsonrpc": "2.0", "result": 7, "id": "1"},
            {"jsonrpc": "2.0", "result": 19, "id": "2"},
            {"jsonrpc": "2.0",
             "error": {"code": -32600, "message": "Invalid Request."},
             "id": null},
            {"jsonrpc": "2.0",
             "error": {"code": -32601, "message": "Method not found."},
             "id": "5"},
            {"jsonrpc": "2.0", "result": ["hello", 5], "id": "9"}
        ]""")

        self.assertTrue(len(requests) == len(verify_requests))
        self.assertTrue(len(responses) == len(verify_responses))

        responses_by_id = {}
        response_i = 0

        for i in range(len(requests)):
            verify_request = verify_requests[i]
            request = requests[i]
            response = None
            if request.get('method') != 'notify_hello':
                req_id = request.get('id')
                if 'id' in verify_request:
                    verify_request['id'] = req_id
                verify_response = verify_responses[response_i]
                verify_response['id'] = req_id
                responses_by_id[req_id] = verify_response
                response_i += 1
                response = verify_response
            self.assertTrue(request == verify_request)

        for response in responses:
            verify_response = responses_by_id.get(response.get('id'))
            if 'error' in verify_response:
                verify_response['error']['message'] = \
                    response['error']['message']
            self.assertTrue(response == verify_response)
示例#7
0
 def get_multicall_client(self):
     """
     Utility method to get a multi-call proxy to the test server
     """
     server = self.get_client()
     return jsonrpclib.MultiCall(server)