Example #1
0
    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')
Example #2
0
    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')
Example #3
0
    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)
Example #4
0
    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)
Example #5
0
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')
Example #6
0
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')
Example #7
0
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),
        ])
Example #8
0
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),
        ])