Esempio n. 1
0
    def test_handle_request_string_non_standard_json_encoder(self):

        serializer = JSONRPC20Serializer

        some_guid = uuid.uuid4()

        def get_misshaped_objects(*args):
            return some_guid

        app = JSONPRCApplication(serializer)
        app.register_function(get_misshaped_objects)

        request = serializer.assemble_request(
            'get_misshaped_objects'
        )
        requests_string = serializer.json_dumps(request)
        response_string = app.handle_request_string(requests_string)
        response_json = serializer.json_loads(response_string)

        assert 'error' in response_json
        assert 'is not JSON serializable' in response_json['error']['data']
        self.assertEqual(
            request['id'],
            response_json['id']
        )
Esempio n. 2
0
    def test_handle_request_string_non_standard_json_encoder(self):

        some_guid = uuid.uuid4()

        def get_misshaped_objects(*args):
            return [{1, 2}, some_guid]

        class MyCustomJSONEncoder(JSONRPC20Serializer.json_encoder):
            def default(self, o):
                if isinstance(o, set):
                    return list(o)
                if isinstance(o, uuid.UUID):
                    return str(o)
                return super(MyCustomJSONEncoder, self).default(o)

        class MyCustomJSONSerializer(JSONRPC20Serializer):

            json_encoder = MyCustomJSONEncoder

        self.app = JSONPRCApplication(MyCustomJSONSerializer)
        self.app.register_function(get_misshaped_objects)

        request = MyCustomJSONSerializer.assemble_request(
            'get_misshaped_objects')
        requests_string = MyCustomJSONSerializer.json_dumps(request)

        response_string = self.app.handle_request_string(requests_string)

        response_json = MyCustomJSONSerializer.json_loads(response_string)

        assert 'error' not in response_json
        assert response_json['id'] == request['id']
        assert response_json['result'] == [[1, 2], str(some_guid)]
Esempio n. 3
0
    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)