Esempio n. 1
0
    def test_set_request_handler(self):
        # If: I add a request handler
        params = IncomingMessageConfiguration('test/test', int)
        handler = mock.MagicMock()
        server = JSONRPCServer(None, None)
        server.set_request_handler(params, handler)

        # Then: The request handler should contain the handler
        self.assertTrue(params.method in server._request_handlers)
        self.assertIsNotNone(server._request_handlers[params.method])
        self.assertIs(server._request_handlers[params.method].class_, int)
        self.assertIs(server._request_handlers[params.method].handler, handler)
Esempio n. 2
0
    def test_dispatch_request_normal(self):
        # Setup: Create a server with a single handler that has none for the deserialization class
        config = IncomingMessageConfiguration('test/test', _TestParams)
        handler = mock.MagicMock()
        server = JSONRPCServer(None, None, logger=utils.get_mock_logger())
        server.set_request_handler(config, handler)

        # If: I dispatch a message that has none set for the deserialization class
        params = {}
        message = JSONRPCMessage.create_request('123', 'test/test', params)
        server._dispatch_message(message)

        # Then:
        # ... The handler should have been called
        handler.assert_called_once()

        # ... The parameters to the handler should have been a request context and params
        self.assertIsInstance(handler.mock_calls[0][1][0], RequestContext)
        self.assertIs(handler.mock_calls[0][1][0]._queue, server._output_queue)
        self.assertIs(handler.mock_calls[0][1][0]._message, message)
        self.assertIsInstance(handler.mock_calls[0][1][1], _TestParams)