Exemplo n.º 1
0
 def test_combined(self):
     request = '{"method": "abc", "params": ["p1", 12321], "jsonrpc": 2.0, '
     request += '"id": 123}'
     result = jsonrpc.decodeRequest(request)
     expected = {'method': 'abc', 'params': ['p1', 12321], 'jsonrpc': 2.0,
                 'id': 123}
     self.assertEquals(result, expected)
Exemplo n.º 2
0
 def test_combined(self):
     request = '{"method": "abc", "params": ["p1", 12321], "jsonrpc": 2.0, '
     request += '"id": 123}'
     result = jsonrpc.decodeRequest(request)
     expected = {
         'method': 'abc',
         'params': ['p1', 12321],
         'jsonrpc': 2.0,
         'id': 123
     }
     self.assertEquals(result, expected)
Exemplo n.º 3
0
    def stringReceived(self, string):
        """
        This is the 'main' RPC method. This will always be called when
        a request arrives and it's up to this method to parse the request and
        dispatch it further.

        @type string: str
        @param string: Request from client, just the 'string' itself, already
            stripped of the netstring stuff.

        @rtype: DeferredList
        @return: Deferred, that will fire when all methods are finished. It
            will already have all the callbacks and errbacks neccessary to
            finish and send the response.
        """

        self._logRequest(string)
        try:
            request_content = jsonrpc.decodeRequest(string)
        except jsonrpc.JSONRPCError:
            self._parseError()
            return None

        is_batch = True
        if not isinstance(request_content, list):
            request_content = [request_content]
            is_batch = False

        dl = []
        for request_dict in request_content:
            d = succeed(request_dict)
            d.addCallback(jsonrpc.verifyMethodCall)
            d.addCallback(self._callMethod)
            d.addBoth(jsonrpc.prepareMethodResponse, request_dict['id'],
                      request_dict['jsonrpc'])
            dl.append(d)

        dl = DeferredList(dl, consumeErrors=True)
        dl.addBoth(self._cbFinishRequest, is_batch)

        return dl
Exemplo n.º 4
0
    def _getRequestContent(self, request):
        """
        Parse the JSON from the request. Return it as a list, even if there was
        only one method call (which would give us a dict). This will be useful
        later, as we can iterate over it in the same manner if it is a single
        call or a batch request.

        @type request: t.w.s.Request
        @param request: The request from client

        @rtype: list
        @return: List of dicts, one dict per method call.

        @raise JSONRPCError: If there's error in parsing.
        """

        request.content.seek(0, 0)
        request_json = request.content.read().decode()
        request_content = jsonrpc.decodeRequest(request_json)

        return request_content
Exemplo n.º 5
0
 def test_onlyVersionStr(self):
     request = '{"jsonrpc": "2"}'
     result = jsonrpc.decodeRequest(request)
     expected = {'jsonrpc': "2"}
     self.assertEquals(result, expected)
Exemplo n.º 6
0
 def test_onlyVersionFloat(self):
     request = '{"jsonrpc": 2.0}'
     result = jsonrpc.decodeRequest(request)
     expected = {'jsonrpc': 2.0}
     self.assertEquals(result, expected)
Exemplo n.º 7
0
 def test_onlyIdStr(self):
     request = '{"id": "1b3"}'
     result = jsonrpc.decodeRequest(request)
     expected = {'id': '1b3'}
     self.assertEquals(result, expected)
Exemplo n.º 8
0
 def test_onlyIdInt(self):
     request = '{"id": 123}'
     result = jsonrpc.decodeRequest(request)
     expected = {'id': 123}
     self.assertEquals(result, expected)
Exemplo n.º 9
0
 def test_onlyParams(self):
     request = '{"params": "abcdef"}'
     result = jsonrpc.decodeRequest(request)
     expected = {'params': 'abcdef'}
     self.assert_json(result, expected)
Exemplo n.º 10
0
 def test_onlyMethod(self):
     result = jsonrpc.decodeRequest('{"method": "aa"}')
     expected = {'method': 'aa'}
     self.assert_json(result, expected)
Exemplo n.º 11
0
 def test_onlyMethod(self):
     result = jsonrpc.decodeRequest('{"method": "aa"}')
     expected = {'method': 'aa'}
     self.assertEquals(result, expected)
Exemplo n.º 12
0
 def test_onlyVersionStr(self):
     request = '{"jsonrpc": "2"}'
     result = jsonrpc.decodeRequest(request)
     expected = {'jsonrpc': "2"}
     self.assertEquals(result, expected)
Exemplo n.º 13
0
 def test_onlyVersionFloat(self):
     request = '{"jsonrpc": 2.0}'
     result = jsonrpc.decodeRequest(request)
     expected = {'jsonrpc': 2.0}
     self.assertEquals(result, expected)
Exemplo n.º 14
0
 def test_onlyIdStr(self):
     request = '{"id": "1b3"}'
     result = jsonrpc.decodeRequest(request)
     expected = {'id': '1b3'}
     self.assertEquals(result, expected)
Exemplo n.º 15
0
 def test_onlyIdInt(self):
     request = '{"id": 123}'
     result = jsonrpc.decodeRequest(request)
     expected = {'id': 123}
     self.assertEquals(result, expected)
Exemplo n.º 16
0
 def test_onlyParams(self):
     request = '{"params": "abcdef"}'
     result = jsonrpc.decodeRequest(request)
     expected = {'params': 'abcdef'}
     self.assertEquals(result, expected)