def test_get_endpoint_url(self): disp = Dispatcher() disp.add_endpoint('user_page0', '/path0/to/{tenant_id}') self.app.add_dispatcher('SERVICE', disp) req = MagicMock() req.env = {'tenant_id': '1234'} req.protocol = 'http' req.get_header.return_value = 'some_host' req.app = '' url = self.app.get_endpoint_url('SERVICE', req, 'user_page0') self.assertEqual(url, 'http://some_host/path0/to/1234')
def test_make_api(self): # Populate a dispatcher with some resources disp = Dispatcher() resources = [StubResource() for i in range(10)] for i, resource in enumerate(resources): disp.add_endpoint('test_%s' % i, '/path/to/%s' % i) disp.set_handler('test_%s' % i, resource) self.app.add_dispatcher('SERVICE', disp) api = self.app.make_api() self.assertTrue(hasattr(api, '__call__')) self.assertIsInstance(api, falcon.API) self.assertEqual(len(api._routes), 20)
def test_all_dispatchers(self): for service in SUPPORTED_SERVICES: disp = Dispatcher() dispatcher_module = importlib.import_module('jumpgate.' + service) dispatcher_module.add_endpoints(disp) self.assertGreater(len(disp._endpoints), 0)
def test_add_get_dispatcher(self): disp = Dispatcher() self.app.add_dispatcher('SERVICE', disp) self.assertEqual(self.app._dispatchers, {'SERVICE': disp}) disp_return = self.app.get_dispatcher('SERVICE') self.assertEqual(disp_return, disp)
def test_all_endpoints(self): for service in SUPPORTED_SERVICES: app = Jumpgate() app.config = MOCK_CONFIG disp = Dispatcher() dispatcher_module = importlib.import_module('jumpgate.' + service) dispatcher_module.add_endpoints(disp) module_name = 'jumpgate.%s.drivers.sl' % service module = importlib.import_module(module_name) module.setup_routes(app, disp) self.assertGreater(len(disp._endpoints), 0)
def load_endpoints(self): for service in SUPPORTED_SERVICES: enabled_services = self.config['enabled_services'] if service in enabled_services: service_module = importlib.import_module('jumpgate.' + service) # Import the dispatcher for the service disp = Dispatcher(mount=self.config[service]['mount']) service_module.add_endpoints(disp) self.add_dispatcher(service, disp) self.installed_modules[service] = True else: self.installed_modules[service] = False
class TestDispatcherUrls(unittest.TestCase): def setUp(self): self.disp = Dispatcher() self.disp.add_endpoint('user_page', '/path/to/{tenant_id}') self.disp.add_endpoint('instance_detail', '/path/to/{tenant_id}/{instance_id}') def test_get_endpoint_path(self): req = MagicMock() req.env = {'tenant_id': '1234'} path = self.disp.get_endpoint_path(req, 'user_page') self.assertEqual(path, '/path/to/1234') path = self.disp.get_endpoint_path(req, 'instance_detail', instance_id='9876') self.assertEqual(path, '/path/to/1234/9876') def test_get_endpoint_url(self): req = MagicMock() req.env = {'tenant_id': '1234'} req.protocol = 'http' req.get_header.return_value = 'some_host' req.app = '' path = self.disp.get_endpoint_url(req, 'user_page') self.assertEqual(path, 'http://some_host/path/to/1234') path = self.disp.get_endpoint_url(req, 'instance_detail', instance_id='9876') self.assertEqual(path, 'http://some_host/path/to/1234/9876')
class TestDispatcherUrls(unittest.TestCase): def setUp(self): self.disp = Dispatcher() self.disp.add_endpoint('user_page', '/path/to/{tenant_id}') self.disp.add_endpoint('instance_detail', '/path/to/{tenant_id}/{instance_id}') def test_get_endpoint_path(self): req = MagicMock() req.env = {'tenant_id': '1234'} path = self.disp.get_endpoint_path(req, 'user_page') self.assertEquals(path, '/path/to/1234') path = self.disp.get_endpoint_path( req, 'instance_detail', instance_id='9876') self.assertEquals(path, '/path/to/1234/9876') def test_get_endpoint_url(self): req = MagicMock() req.env = {'tenant_id': '1234'} req.protocol = 'http' req.get_header.return_value = 'some_host' req.app = '' path = self.disp.get_endpoint_url(req, 'user_page') self.assertEquals(path, 'http://some_host/path/to/1234') path = self.disp.get_endpoint_url( req, 'instance_detail', instance_id='9876') self.assertEquals(path, 'http://some_host/path/to/1234/9876')
def setUp(self): self.disp = Dispatcher(mount='/mountpoint')
class TestDispatcher(unittest.TestCase): def setUp(self): self.disp = Dispatcher(mount='/mountpoint') def test_init(self): self.assertEquals(self.disp._endpoints, {}) def test_add_endpoint(self): self.disp.add_endpoint('user_page', '/path/to/{tenant_id}') self.assertEquals(len(self.disp._endpoints), 1) self.assertEquals(self.disp._endpoints['user_page'], ('/mountpoint/path/to/{tenant_id}', None)) def test_set_handler(self): self.disp.add_endpoint('user_page0', '/path0/to/{tenant_id}') handler = MagicMock() self.disp.set_handler('user_page0', handler) self.assertEquals(self.disp._endpoints, { 'user_page0': ('/mountpoint/path0/to/{tenant_id}', handler)}) self.assertRaises( ValueError, self.disp.set_handler, 'unknown', handler) def test_get_unused_endpoints(self): self.disp.add_endpoint('user_page0', '/path0/to/{tenant_id}') self.disp.add_endpoint('user_page1', '/path1/to/{tenant_id}') self.disp.add_endpoint('user_page2', '/path2/to/{tenant_id}') self.disp.set_handler('user_page0', MagicMock()) unused_endpoints = self.disp.get_unused_endpoints() self.assertEquals(unused_endpoints, ['user_page1', 'user_page2']) def test_get_routes(self): self.disp.add_endpoint('user_page0', '/path0/to/{tenant_id}') self.disp.add_endpoint('user_page1', '/path1/to/{tenant_id}') self.disp.add_endpoint('user_page2', '/path2/to/{tenant_id}') handler = MagicMock() self.disp.set_handler('user_page0', handler) endpoints = self.disp.get_routes() self.assertEquals(endpoints, [ ('/mountpoint/path0/to/{tenant_id}', handler), ])
def setUp(self): self.disp = Dispatcher() self.disp.add_endpoint('user_page', '/path/to/{tenant_id}') self.disp.add_endpoint('instance_detail', '/path/to/{tenant_id}/{instance_id}')
class TestDispatcher(unittest.TestCase): def setUp(self): self.disp = Dispatcher(mount='/mountpoint') def test_init(self): self.assertEqual(self.disp._endpoints, {}) def test_add_endpoint(self): self.disp.add_endpoint('user_page', '/path/to/{tenant_id}') self.assertEqual(len(self.disp._endpoints), 1) self.assertEqual(self.disp._endpoints['user_page'], ('/mountpoint/path/to/{tenant_id}', None)) def test_set_handler(self): self.disp.add_endpoint('user_page0', '/path0/to/{tenant_id}') handler = MagicMock() self.disp.set_handler('user_page0', handler) self.assertEqual( self.disp._endpoints, {'user_page0': ('/mountpoint/path0/to/{tenant_id}', handler)}) self.assertRaises(ValueError, self.disp.set_handler, 'unknown', handler) def test_get_unused_endpoints(self): self.disp.add_endpoint('user_page0', '/path0/to/{tenant_id}') self.disp.add_endpoint('user_page1', '/path1/to/{tenant_id}') self.disp.add_endpoint('user_page2', '/path2/to/{tenant_id}') self.disp.set_handler('user_page0', MagicMock()) unused_endpoints = self.disp.get_unused_endpoints() self.assertEqual(unused_endpoints, ['user_page1', 'user_page2']) def test_get_routes(self): self.disp.add_endpoint('user_page0', '/path0/to/{tenant_id}') self.disp.add_endpoint('user_page1', '/path1/to/{tenant_id}') self.disp.add_endpoint('user_page2', '/path2/to/{tenant_id}') handler = MagicMock() self.disp.set_handler('user_page0', handler) endpoints = self.disp.get_routes() self.assertEqual(endpoints, [ ('/mountpoint/path0/to/{tenant_id}', handler), ])