Beispiel #1
0
def tree_application(mappings):
    """
    Tree middleware factory that mounts a list of (regex_str, application)
    tuples on a Tree instance and returns the instance.
    :param mappings: list of (regex_str, application) tuples
    :type mappings: list
    :return: Tree middleware with mounted mappings
    :rtype: Tree
    """
    tree = Tree()
    for regex_str, app in mappings:
        tree.mount(regex_str, app)
    return tree
Beispiel #2
0
class TreeTester(unittest.TestCase):

    def setUp(self):
        super(TreeTester, self).setUp()
        self.tree = Tree()

    def tearDown(self):
        super(TreeTester, self).tearDown()
        self.tree = None

    def test_dispatch(self):
        environ = util.test_environ(path_info='/foo/bar/')
        self.tree.mount('^/foo', Mock())
        self.tree(environ, Mock())
        self.assertEqual(environ['PATH_INFO'], '/bar/')
        self.assertEqual(environ['SCRIPT_NAME'], '/foo/')

    def test_dispatch_no_slash(self):
        environ = util.test_environ(path_info='/foo/bar')
        self.tree.mount('^/foo', Mock())
        self.tree(environ, Mock())
        self.assertEqual(environ['PATH_INFO'], '/bar')
        self.assertEqual(environ['SCRIPT_NAME'], '/foo/')

    def test_double_match(self):
        environ = util.test_environ(path_info='/foo/foo/')
        self.tree.mount('^/foo', Mock())
        self.tree(environ, Mock())
        self.assertEqual(environ['PATH_INFO'], '/foo/')
        self.assertEqual(environ['SCRIPT_NAME'], '/foo/')

    def test_regex(self):
        environ = util.test_environ('/foo/bar/baz/')
        self.tree.mount('^/foo/[^/]+', Mock())
        self.tree(environ, Mock())
        self.assertEqual(environ['PATH_INFO'], '/baz/')
        self.assertEqual(environ['SCRIPT_NAME'], '/foo/bar/')

    def test_bad_regex(self):
        self.assertRaises(ValueError, self.tree.mount, '/foo', None)
        self.assertRaises(ValueError, self.tree.mount, '^/foo$', None)
        self.assertRaises(ValueError, self.tree.mount, '^/foo/', None)

    def test_not_found(self):
        environ = util.test_environ()
        start_response = Mock()
        self.assertRaises(HttpNotFound, self.tree, environ, start_response)
Beispiel #3
0
 def setUp(self):
     super(TreeTester, self).setUp()
     self.tree = Tree()