Ejemplo n.º 1
0
 def _getDeferredResponse(self, data):
     '''Return the JSON-RPC response from deferred data.'''
     resource = ImplementedJSONRPCResource()
     request = manufacture.makeTwistedWebRequest(
         resource=resource, data=data)
     result = resource.render_POST(request)
     self.assertEqual(server.NOT_DONE_YET, result)
     self.runDeferred(resource._deferred)
     return json.loads(request.test_response_content.decode('utf-8'))
Ejemplo n.º 2
0
 def test_POST_invalid_json(self):
     """
     An error is returned when posting a wrong formatted json.
     """
     data = '{bad-json,}'
     resource = ImplementedJSONRPCResource()
     request = manufacture.makeTwistedWebRequest(
         resource=resource, data=data)
     response = json.loads(resource.render_POST(request))
     self.assertIsNone(response['result'])
     self.assertEqual(-32700, response['error']['code'])
     self.assertEqual(None, response['id'])
Ejemplo n.º 3
0
 def test_POST_no_version(self):
     """
     An error is raised when the request has no version.
     """
     data = '{"method": "some_method", "id": 2, "params": {}}'
     resource = ImplementedJSONRPCResource()
     request = manufacture.makeTwistedWebRequest(
         resource=resource, data=data)
     response = json.loads(resource.render_POST(request).decode('utf-8'))
     self.assertIsNone(response['result'])
     self.assertEqual(-32600, response['error']['code'])
     self.assertEqual(u'Missing "jsonrpc".', response['error']['message'])
     self.assertEqual(2, response['id'])
Ejemplo n.º 4
0
    def test_getSession_no_session(self):
        """
        None is returned if the request does not contain session
        information or the information is not valid.
        """
        session = json_rpc._get_session(self.request)

        self.assertIsNone(session)

        request = manufacture.makeTwistedWebRequest()
        request.setRequestHeader(
            'authorization', manufacture.getUniqueString())

        session = json_rpc._get_session(request)

        self.assertIsNone(session)
Ejemplo n.º 5
0
 def test_GET_request(self):
     """
     A JSON-RCP method can also be requested using GET.
     """
     resource = ImplementedJSONRPCResource()
     request = manufacture.makeTwistedWebRequest(resource=resource)
     request.postpath = ['method_name']
     # We monkey patch the renderJSONRPCOverHTTP since here we only
     # care about how a GET request is converted into a JSON-RPC
     # request. We don't really care about how it is process since
     # it will be checked in POST tests.
     resource._renderJSONRPCOverHTTP = lambda resource, json: json
     jsonrpc_request = resource.render_GET(request)
     self.assertEqual({}, jsonrpc_request['params'])
     self.assertEqual('get_method_name', jsonrpc_request['method'])
     self.assertEqual(1, jsonrpc_request['id'])
     self.assertEqual(2.0, jsonrpc_request['jsonrpc'])
Ejemplo n.º 6
0
    def test_POST_internal_error(self):
        """
        Internal server errors are reported as errors.
        """
        data = (
            '{"jsonrpc": "2.0", "id": 6, '
            '"method": "public_method_internal_error", "params": []}')

        resource = ImplementedJSONRPCResource()
        peer = manufacture.makeIPv4Address()
        request = manufacture.makeTwistedWebRequest(
            resource=resource, data=data, peer=peer)

        result = resource.render_POST(request)
        self.assertEqual(server.NOT_DONE_YET, result)
        self.runDeferred(resource._deferred)
        response = json.loads(request.test_response_content)

        self.assertIsNone(response['result'])
        self.assertEqual(-32603, response['error']['code'])
        self.assertEqual(6, response['id'])
        self.assertTrue(resource._logInternalError_called)
        self.assertTrue(u'internal' in resource._logInternalError_value)
        self.assertEqual(peer, resource._logInternalError_peer)
Ejemplo n.º 7
0
    def test_POST_with_private_method(self):
        """
        Check requesting a private method with a valid session.
        """
        data = (
            '{"jsonrpc": "2.0", "id": 1, '
            '"method": "private_method", "params": {}}')
        resource = ImplementedJSONRPCResource()
        request = manufacture.makeTwistedWebRequest(
            resource=resource, data=data)
        session = request.getSession()
        request.setRequestHeader('authorization', session.uid)

        try:
            result = resource.render_POST(request)
            self.assertEqual(server.NOT_DONE_YET, result)
            self.runDeferred(resource._deferred)
            response = json.loads(
                request.test_response_content.decode('utf-8'))
            self.assertIsNone(response['error'])
            self.assertEqual(u'private_method', response['result'])
            self.assertEqual(1, response['id'])
        finally:
            session.expire()
Ejemplo n.º 8
0
 def setUp(self):
     super(TestHelpers, self).setUp()
     self.request = manufacture.makeTwistedWebRequest()
     self.session = self.request.site.makeSession()
Ejemplo n.º 9
0
 def _checkNotificationResult(self, data):
     resource = ImplementedJSONRPCResource()
     request = manufacture.makeTwistedWebRequest(
         resource=resource, data=data)
     result = resource.render_POST(request)
     self.assertEqual('', result)