Esempio n. 1
0
class JSONPRCApplicationTestSuite(TestCase):

    def setUp(self):
        super(JSONPRCApplicationTestSuite, self).setUp()

        def adder(*args):
            return sum(args)

        class MyTestException(Exception):
            pass

        def blow_up(*args, **kwargs):
            raise MyTestException('Blowing up on command')

        self.app = JSONPRCApplication(JSONRPC20Serializer)
        self.app.register_function(adder)
        self.app.register_function(blow_up)

    def test_process_requests(self):

        request1 = JSONRPC20Serializer.assemble_request(
            'adder',
            (2, 3)
        )
        request2 = JSONRPC20Serializer.assemble_request(
            'adder',
            (4, 3)
        )
        request3 = JSONRPC20Serializer.assemble_request(
            'adder' # note: no args. 'adder' can take it. JSONRpc app must too
        )

        assert len({request1['id'], request2['id'], request3['id']}) == 3

        requests, is_batch_mode = JSONRPC20Serializer.parse_request(
            JSONRPC20Serializer.json_dumps([request1, request2, request3])
        )

        responses = self.app.process_requests(requests)

        assert len(responses) == 3

        response_json = responses[0]
        assert 'error' not in response_json
        assert response_json['id'] == request1['id']
        assert response_json['result'] == 5

        response_json = responses[1]
        assert 'error' not in response_json
        assert response_json['id'] == request2['id']
        assert response_json['result'] == 7

        response_json = responses[2]
        assert 'error' not in response_json
        assert response_json['id'] == request3['id']
        assert response_json['result'] == 0

    def test_process_requests_with_errors(self):

        request1 = JSONRPC20Serializer.assemble_request(
            'blow_up',
            (2, 3)
        )
        request2 = JSONRPC20Serializer.assemble_request(
            'adder',
            (4, 3)
        )
        requests, is_batch_mode = JSONRPC20Serializer.parse_request(
            JSONRPC20Serializer.json_dumps([request1, request2])
        )

        responses = self.app.process_requests(requests)

        assert len(responses) == 2

        response_json = responses[0]
        assert 'error' in response_json
        assert set(response_json['error'].keys()) == {'code', 'message', 'data'}
        assert response_json['error']['message'] == 'Blowing up on command' # actual .message from the raised error
        assert response_json['id'] == request1['id']

        response_json = responses[1]
        assert 'error' not in response_json
        assert response_json['id'] == request2['id']
        assert response_json['result'] == 7

    def test_handle_request_string(self):

        request1 = JSONRPC20Serializer.assemble_request(
            'adder',
            (2, 3)
        )
        request2 = JSONRPC20Serializer.assemble_request(
            'adder',
            (4, 3)
        )
        requests_string = JSONRPC20Serializer.json_dumps([request1, request2])

        response_string = self.app.handle_request_string(requests_string)

        responses_data = JSONRPC20Serializer.json_loads(response_string)

        assert len(responses_data) == 2

        response_json = responses_data[0]
        assert 'error' not in response_json
        assert response_json['id'] == request1['id']
        assert response_json['result'] == 5

        response_json = responses_data[1]
        assert 'error' not in response_json
        assert response_json['id'] == request2['id']
        assert response_json['result'] == 7
Esempio n. 2
0
class JSONPRCApplicationTestSuite(TestCase):

    def setUp(self):
        super(JSONPRCApplicationTestSuite, self).setUp()

        def adder(*args):
            return sum(args)

        class MyTestException(Exception):
            pass

        def blow_up(*args, **kwargs):
            raise MyTestException('Blowing up on command')

        self.app = JSONPRCApplication(JSONRPC20Serializer)
        self.app.register_function(adder)
        self.app.register_function(blow_up)

    def test_process_requests(self):

        request1 = JSONRPC20Serializer.assemble_request(
            'adder',
            (2, 3)
        )
        request2 = JSONRPC20Serializer.assemble_request(
            'adder',
            (4, 3)
        )
        request3 = JSONRPC20Serializer.assemble_request(
            'adder' # note: no args. 'adder' can take it. JSONRpc app must too
        )

        assert len({request1['id'], request2['id'], request3['id']}) == 3

        requests, is_batch_mode = JSONRPC20Serializer.parse_request(
            JSONRPC20Serializer.json_dumps([request1, request2, request3])
        )

        responses = self.app.process_requests(requests)

        assert len(responses) == 3

        response_json = responses[0]
        assert 'error' not in response_json
        assert response_json['id'] == request1['id']
        assert response_json['result'] == 5

        response_json = responses[1]
        assert 'error' not in response_json
        assert response_json['id'] == request2['id']
        assert response_json['result'] == 7

        response_json = responses[2]
        assert 'error' not in response_json
        assert response_json['id'] == request3['id']
        assert response_json['result'] == 0

    def test_process_requests_with_errors(self):

        request1 = JSONRPC20Serializer.assemble_request(
            'blow_up',
            (2, 3)
        )
        request2 = JSONRPC20Serializer.assemble_request(
            'adder',
            (4, 3)
        )
        requests, is_batch_mode = JSONRPC20Serializer.parse_request(
            JSONRPC20Serializer.json_dumps([request1, request2])
        )

        responses = self.app.process_requests(requests)

        assert len(responses) == 2

        response_json = responses[0]
        assert 'error' in response_json
        assert set(response_json['error'].keys()) == {'code', 'message', 'data'}
        assert response_json['error']['message'] == 'Blowing up on command' # actual .message from the raised error
        assert response_json['id'] == request1['id']

        response_json = responses[1]
        assert 'error' not in response_json
        assert response_json['id'] == request2['id']
        assert response_json['result'] == 7

    def test_handle_request_string(self):

        request1 = JSONRPC20Serializer.assemble_request(
            'adder',
            (2, 3)
        )
        request2 = JSONRPC20Serializer.assemble_request(
            'adder',
            (4, 3)
        )
        requests_string = JSONRPC20Serializer.json_dumps([request1, request2])

        response_string = self.app.handle_request_string(requests_string)

        responses_data = JSONRPC20Serializer.json_loads(response_string)

        assert len(responses_data) == 2

        response_json = responses_data[0]
        assert 'error' not in response_json
        assert response_json['id'] == request1['id']
        assert response_json['result'] == 5

        response_json = responses_data[1]
        assert 'error' not in response_json
        assert response_json['id'] == request2['id']
        assert response_json['result'] == 7
Esempio n. 3
0
class JSONPRCApplicationTestSuite(TestCase):

    def setUp(self):
        super(JSONPRCApplicationTestSuite, self).setUp()

        def adder(a, b):
            return a + b

        self.app = JSONPRCApplication(JSONRPC20Serializer)
        self.app.register_function(adder)

    def test_process_requests(self):

        request1 = JSONRPC20Serializer.assemble_request(
            'adder',
            (2, 3)
        )
        request2 = JSONRPC20Serializer.assemble_request(
            'adder',
            (4, 3)
        )
        requests, is_batch_mode = JSONRPC20Serializer.parse_request(
            json.dumps([request1, request2])
        )

        responses = self.app.process_requests(requests)

        assert len(responses) == 2

        response_json = responses[0]
        assert 'error' not in response_json
        assert response_json['id'] == request2['id']
        assert response_json['result'] == 5

        response_json = responses[1]
        assert 'error' not in response_json
        assert response_json['id'] == request2['id']
        assert response_json['result'] == 7

    def test_handle_request_string(self):

        request1 = JSONRPC20Serializer.assemble_request(
            'adder',
            (2, 3)
        )
        request2 = JSONRPC20Serializer.assemble_request(
            'adder',
            (4, 3)
        )
        requests_string = json.dumps([request1, request2])

        response_string = self.app.handle_request_string(requests_string)

        responses_data = json.loads(response_string)

        assert len(responses_data) == 2

        response_json = responses_data[0]
        assert 'error' not in response_json
        assert response_json['id'] == request2['id']
        assert response_json['result'] == 5

        response_json = responses_data[1]
        assert 'error' not in response_json
        assert response_json['id'] == request2['id']
        assert response_json['result'] == 7