Пример #1
0
    def test_injected_arg(self):
        """Test that only one argument is required (method signature is detected correctly)."""

        request = Request(parsed_request={
            "jsonrpc": "2.0",
            "id": 321,
            "method": "injected_arg",
            "params": []
        })
        response = service.handle_request(request)

        self.assertEqual(
            json.loads(response.body), {
                "jsonrpc": "2.0",
                "id": 321,
                "error": {
                    "code": -32602,
                    "message": "missing a required argument: 'a'"
                }
            })

        request = Request(parsed_request={
            "jsonrpc": "2.0",
            "id": 321,
            "method": "injected_arg",
            "params": [1]
        })
        response = service.handle_request(request)

        self.assertEqual(json.loads(response.body), {
            "jsonrpc": "2.0",
            "id": 321,
            "result": 11
        })
Пример #2
0
    def test_is_notification(self):
        request = Request(parsed_request={
            'jsonrpc': '2.0',
            'id': 123,
            'method': 'foo'
        })
        self.assertFalse(request.is_notification)

        request = Request(parsed_request={'jsonrpc': '2.0', 'method': 'foo'})
        self.assertTrue(request.is_notification)
Пример #3
0
    def test_fail_as_notification(self):
        request = Request(parsed_request={"jsonrpc": "2.0", "method": "fail"})
        response = service.handle_request(request)

        self.assertEqual(response.body, "")

        self.assertIsInstance(response.exc_info[1], NameError)
    def test_parameter_injection(self):
        request = Request(
            raw_request='{"jsonrpc": "2.0", "id": 666, "method": "get_donut"}')
        request.language = "en"

        response = service.handle_request(request)

        self.assertEqual(
            json.loads(response.body), {
                "jsonrpc": "2.0",
                "id": 666,
                "result": {
                    "message": "Here, have this donut.",
                    "_timing": 1000
                }
            })
Пример #5
0
    def test_foo(self):
        request = Request(
            raw_request='{"jsonrpc": "2.0", "id": 321, "method": "foo"}')
        response = service.handle_request(request)

        self.assertEqual(json.loads(response.body), {
            "jsonrpc": "2.0",
            "id": 321,
            "result": "Foo is not bar."
        })
Пример #6
0
    def test_ping(self):
        request = Request(
            raw_request='{"jsonrpc": "2.0", "id": 321, "method": "ping"}')
        response = service.handle_request(request)

        self.assertEqual(json.loads(response.body), {
            "jsonrpc": "2.0",
            "id": 321,
            "result": "pong"
        })
Пример #7
0
    def test_request_with_array_params(self):
        request = Request(parsed_request={
            'jsonrpc': '2.0',
            'id': 123,
            'method': 'foo',
            'params': ['abc', 'def']
        })

        self.assertEqual(request.args, ['abc', 'def'])
        self.assertEqual(request.kwargs, {})
Пример #8
0
    def test_success_response_for_notification(self):
        result = {"foo": "bar"}

        request = Request(parsed_request={"jsonrpc": "2.0", "method": "foo"})
        response = SuccessResponse(request, result)

        self.assertEqual(response.dict, {
            "jsonrpc": "2.0",
            "id": None,
            "result": result
        })
        self.assertEqual(response.body, "")
Пример #9
0
    def test_request_with_raw_input(self):
        request = Request(
            raw_request='{"jsonrpc": "2.0", "id": 123, "method": "foo"}')

        self.assertEqual(request.parsed_request, {
            'jsonrpc': '2.0',
            'id': 123,
            'method': 'foo'
        })
        self.assertEqual(request.id, 123)
        self.assertEqual(request.method, 'foo')
        self.assertEqual(request.args, [])
        self.assertEqual(request.kwargs, {})
Пример #10
0
    def test_parse_error(self):
        request = Request(raw_request="foo")
        response = service.handle_request(request)

        self.assertEqual(
            json.loads(response.body), {
                'id': None,
                'jsonrpc': '2.0',
                'error': {
                    'code': -32700,
                    'message': 'Expecting value: line 1 column 1 (char 0)'
                }
            })
Пример #11
0
    def test_add_with_list_params(self):
        request = Request(parsed_request={
            "jsonrpc": "2.0",
            "id": 321,
            "method": "add",
            "params": [5, 13]
        })
        response = service.handle_request(request)

        self.assertEqual(json.loads(response.body), {
            "jsonrpc": "2.0",
            "id": 321,
            "result": 18
        })
Пример #12
0
    def test_request_with_object_params(self):
        request = Request(
            parsed_request={
                'jsonrpc': '2.0',
                'id': 123,
                'method': 'foo',
                'params': {
                    'abc': 1,
                    'def': 2
                }
            })

        self.assertEqual(request.args, [])
        self.assertEqual(request.kwargs, {'abc': 1, 'def': 2})
Пример #13
0
    def test_language_in_params(self):
        request = Request(
            raw_request=
            '{"jsonrpc": "2.0", "id": 666, "method": "get_donut", "params": {"language": "cs"}}'
        )
        response = service.handle_request(request)

        self.assertEqual(
            json.loads(response.body), {
                "jsonrpc": "2.0",
                "id": 666,
                "result": {
                    "message": "Na, tu máš koblihu.",
                    "_timing": 1000
                }
            })
Пример #14
0
    def test_request_with_parsed_input(self):
        request = Request(parsed_request={
            'jsonrpc': '2.0',
            'id': 123,
            'method': 'foo'
        })

        self.assertEqual(request.parsed_request, {
            'jsonrpc': '2.0',
            'id': 123,
            'method': 'foo'
        })
        self.assertEqual(request.id, 123)
        self.assertEqual(request.method, 'foo')
        self.assertEqual(request.args, [])
        self.assertEqual(request.kwargs, {})
Пример #15
0
    def test_missing_params(self):
        request = Request(parsed_request={
            "jsonrpc": "2.0",
            "id": 321,
            "method": "add"
        })
        response = service.handle_request(request)

        self.assertEqual(
            json.loads(response.body), {
                "jsonrpc": "2.0",
                "id": 321,
                'error': {
                    'code': -32602,
                    'message': "missing a required argument: 'a'"
                }
            })
Пример #16
0
    def test_custom_error_handling(self):
        request = Request(
            raw_request=
            '{"jsonrpc": "2.0", "id": 666, "method": "get_donut", "params": {"language": "lg"}}'
        )
        response = service.handle_request(request)

        self.assertEqual(
            json.loads(response.body), {
                "jsonrpc": "2.0",
                "id": 666,
                'error': {
                    'code': -2288,
                    'data': 'Unknown language "lg".',
                    'message': 'There is some custom problem.'
                }
            })
Пример #17
0
    def test_fail(self):
        request = Request(parsed_request={
            "jsonrpc": "2.0",
            "id": 321,
            "method": "fail"
        })
        response = service.handle_request(request)

        self.assertEqual(
            json.loads(response.body), {
                "jsonrpc": "2.0",
                "id": 321,
                'error': {
                    'code': -32603,
                    'message': "Internal error"
                }
            })
Пример #18
0
    def test_fail_with_grace(self):
        request = Request(parsed_request={
            "jsonrpc": "2.0",
            "id": 321,
            "method": "fail_with_grace"
        })
        response = service.handle_request(request)

        self.assertEqual(
            json.loads(response.body), {
                "jsonrpc": "2.0",
                "id": 321,
                'error': {
                    'code': -13,
                    'message': 'Graceful as a fail whale.',
                    'data': 'Graceful!'
                }
            })

        self.assertIsInstance(response.exc_info[1], JsonRpcError)
Пример #19
0
    def test_success_response(self):
        result = {"foo": "bar"}

        request = Request(parsed_request={
            "jsonrpc": "2.0",
            "id": 123,
            "method": "foo"
        })
        response = SuccessResponse(request, result)

        self.assertEqual(response.dict, {
            "jsonrpc": "2.0",
            "id": 123,
            "result": result
        })
        self.assertEqual(json.loads(response.body), {
            "jsonrpc": "2.0",
            "id": 123,
            "result": result
        })
Пример #20
0
    def test_error_response_for_notification(self):
        request = Request(parsed_request={"jsonrpc": "2.0", "method": "foo"})
        response = ErrorResponse(request,
                                 -123,
                                 "Out of icecream.",
                                 data={"mood": "sad"})

        expected = {
            "jsonrpc": "2.0",
            "id": None,
            "error": {
                "message": "Out of icecream.",
                "code": -123,
                "data": {
                    "mood": "sad"
                }
            }
        }

        self.assertEqual(response.dict, expected)
        self.assertEqual(response.body, "")
        self.assertEqual(response.exc_info, (None, None, None))
Пример #21
0
    def test_error_response(self):
        request = Request(parsed_request={
            "jsonrpc": "2.0",
            "id": 123,
            "method": "foo"
        })
        response = ErrorResponse(request,
                                 -123,
                                 "Out of icecream.",
                                 exc_info=("Passed as it is", ))

        expected = {
            "jsonrpc": "2.0",
            "id": 123,
            "error": {
                "message": "Out of icecream.",
                "code": -123
            }
        }

        self.assertEqual(response.dict, expected)
        self.assertEqual(json.loads(response.body), expected)
        self.assertEqual(response.exc_info, ("Passed as it is", ))
Пример #22
0
    def test_invalid_request(self):
        request = Request(parsed_request=[])
        self.assertRaisesRegex(InvalidRequest, 'Expected an object',
                               lambda: request.id)
        self.assertFalse(request.is_notification)

        request = Request(parsed_request={})
        self.assertRaisesRegex(InvalidRequest, 'Missing "jsonrpc"',
                               lambda: request.version)
        self.assertFalse(request.is_notification)

        request = Request(parsed_request={"id": 123})
        self.assertRaisesRegex(InvalidRequest, 'Missing "jsonrpc"',
                               lambda: request.version)
        self.assertEqual(request.id,
                         123)  # id will be preserved if it can be obtained

        request = Request(parsed_request={"jsonrpc": "2.0"})
        self.assertRaisesRegex(InvalidRequest, 'Missing "method"',
                               lambda: request.method)
        self.assertIs(request.id, None)
        self.assertTrue(request.is_notification)

        request = Request(parsed_request={"jsonrpc": "2.0", "method": 1})
        self.assertRaisesRegex(InvalidRequest, 'must be a string',
                               lambda: request.method)

        request = Request(parsed_request={"jsonrpc": "2.0", "method": "foo"})
        self.assertEqual(request.method, "foo")

        request = Request(parsed_request={
            "jsonrpc": "2.0",
            "method": "foo",
            "params": False
        })
        self.assertRaisesRegex(InvalidRequest,
                               'must be either object or array',
                               lambda: request.params)
Пример #23
0
    def test_parse_error(self):
        request = Request(raw_request="I am little teapot, short and stout.")

        self.assertRaises(ParseError, lambda: request.id)
        self.assertFalse(request.is_notification)
Пример #24
0
    def test_notification(self):
        request = Request(raw_request='{"jsonrpc": "2.0", "method": "ping"}')
        response = service.handle_request(request)

        self.assertEqual(response.body, "")