Esempio n. 1
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)
Esempio n. 2
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)
Esempio n. 3
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),
        ])
Esempio n. 4
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),
        ])