Exemple #1
0
class TestPathRouter(unittest.TestCase):
    def setUp(self):
        self.context = Context()

    def test_routes_empty_path(self):
        app = Mock(name='app')
        r = wsgi.PathRouter(
            ('', lambda: app())
        )
        self.assertIs(r(self.context, ''), app.return_value)

    def test_matches_path(self):
        app = Mock(name='app')
        r = wsgi.PathRouter(
            ('foo', lambda: Mock(name='foo')()),
            ('bar', lambda: app())
        )
        self.assertIs(r(self.context, 'bar'), app.return_value)

    def test_injects_match_groups_to_app(self):
        app = Mock(name='app')
        r = wsgi.PathRouter(
            ('{foo}/{bar}', lambda foo, bar: app(foo, bar)),
        )
        self.assertIs(r(self.context, 'oof/rab'), app.return_value)
        app.assert_called_once_with('oof', 'rab')

    def test_match_can_be_template(self):
        template = Template('')
        r = wsgi.PathRouter((template, lambda: Mock()()))
        r.match = Mock(return_value={})
        r(self.context, sentinel.path)
        r.match.assert_called_once_with(template, sentinel.path)

    def test_match_can_be_template_arg_tuple(self):
        template = ('{foo:\d+}', {'foo': int})
        r = wsgi.PathRouter((template, lambda: Mock()()))
        r(self.context, '42')
        self.assertEqual(self.context['foo'], 42)

    def test_gets_path_from_context(self):
        template = Template('')
        r = wsgi.PathRouter((template, lambda: Mock()()))
        r.match = Mock(return_value={})
        self.context['path_info'] = sentinel.path
        self.context.inject(r)
        r.match.assert_called_once_with(template, sentinel.path)

    def test_reverse(self):
        r = wsgi.PathRouter(
            ('hello', 'hello/{name}', lambda: Mock()()),
        )
        self.assertEqual(r.reverse('hello', name='guido'), 'hello/guido')
Exemple #2
0
 def test_simple_config(self):
     module = ModuleType('module')
     module.handler1 = lambda: sentinel.a2
     module.handler2 = lambda a1, a2: (a1, a2, sentinel.result)
     config = '''
     index /{a1:\d+} (a1: int):
         handler1 (a2)
         handler2
     '''
     router = configparser.parse_config(config.splitlines(), module)
     self.assertEqual(router.reverse('index', a1=37), '/37')
     ctx = Context(path_info='/42')
     self.assertEqual(
         ctx.inject(router),
         (42, sentinel.a2, sentinel.result)
     )
Exemple #3
0
class MethodRouter(unittest.TestCase):
    def setUp(self):
        self.context = Context()

    def test_routes_by_method(self):
        app = Mock(name='app')
        r = wsgi.MethodRouter(
            ('GET', lambda: app())
        )
        self.assertIs(r(self.context, 'GET'), app.return_value)

    def test_tuple_specifies_multiple_methods(self):
        app = Mock(name='app')
        r = wsgi.MethodRouter(
            (('GET', 'HEAD'), lambda: app())
        )
        self.assertIs(r(self.context, 'GET'), app.return_value)
        self.assertIs(r(self.context, 'HEAD'), app.return_value)

    def test_gets_method_from_context(self):
        r = wsgi.MethodRouter(
            (sentinel.match, lambda: Mock()()),
        )
        r.match = Mock(return_value={})
        self.context['request_method'] = sentinel.method
        self.context.inject(r)
        r.match.assert_called_once_with(sentinel.match, sentinel.method)

    def test_NoRoute_is_subclass(self):
        self.assertTrue(issubclass(
            wsgi.MethodRouter.MethodNotAllowed, wsgi.Router.NoRoute))
        app = Mock(name='app')
        r = wsgi.MethodRouter(
            (('GET', 'HEAD'), lambda: app()),
            (('POST',), lambda: app())
        )
        with self.assertRaises(r.MethodNotAllowed) as assertion:
            r(self.context, 'DELETE')
        self.assertEqual(assertion.exception.request_method, 'DELETE')
        self.assertEqual(
            assertion.exception.allowed_methods,
            ['GET', 'HEAD', 'POST']
        )
Exemple #4
0
 def test_complex_config(self):
     module = ModuleType('module')
     module.exc1 = type('exc1', (Exception,), {})
     module.exc2 = type('exc2', (Exception,), {})
     module.exc3 = type('exc3', (Exception,), {})
     module.handler1 = lambda: sentinel.a2
     module.handler2 = lambda a1, a2: (a1, a2, sentinel.result)
     module.handler3 = lambda: Mock(side_effect=module.exc2)()
     module.handler4 = lambda: sentinel.a4
     module.handler5 = lambda: sentinel.a5
     module.handler6 = lambda r1: (r1, sentinel.a6)
     module.handler7 = lambda: sentinel.a7
     module.handler8 = lambda: sentinel.a8
     module.handler9 = lambda r1, r2: (r1, r2, sentinel.a9)
     config = """
     index /:
         * GET, HEAD:
             handler1 (a2)
             handler2
         * POST:
             handler1
     todo /{todo_id:\d+} (todo_id: int):
         * GET, HEAD:
             handler3 (r1):
                 exc1, exc2: handler4
                 exc3: handler5
             handler6
     other /foo:
         handler7 (r1)
         * DELETE:
             handler8 (r2)
         handler9
     """
     router = configparser.parse_config(config.splitlines(), module)
     ctx = Context(path_info='/42', request_method='GET')
     self.assertEqual(ctx.inject(router), (sentinel.a4, sentinel.a6))
     ctx = Context(path_info='/foo', request_method='DELETE')
     self.assertEqual(
         ctx.inject(router),
         (sentinel.a7, sentinel.a8, sentinel.a9)
     )
Exemple #5
0
 def test_medium_config(self):
     module = ModuleType('module')
     module.handler1 = lambda: sentinel.a2
     module.handler2 = lambda a1, a2: (a1, a2, sentinel.result)
     config = '''
     index /{a1:\d+} (a1: int):
         * GET, HEAD:
             handler1 (a2)
             handler2
         * POST:
             handler1
     '''
     router = configparser.parse_config(config.splitlines(), module)
     self.assertEqual(router.reverse('index', a1=37), '/37')
     ctx = Context(path_info='/42', request_method='GET')
     self.assertEqual(
         ctx.inject(router),
         (42, sentinel.a2, sentinel.result)
     )
     ctx = Context(path_info='/42', request_method='POST')
     self.assertIs(ctx.inject(router), sentinel.a2)
Exemple #6
0
 def setUp(self):
     self.context = Context()