Пример #1
0
    def test_subrouter(self):
        self.middleware_called = False
        self.endpoint_called = False
        self.middleware_a_called = False
        self.endpoint_a_called = False

        def middleware(req, res, next):
            self.middleware_called = True
            return next(req, res)

        def endpoint(req, res):
            self.endpoint_called = True
            res.set_data('Index')
            return res

        router = Router()
        router._create_middleware(middleware, '/', ['GET'])
        router._create_endpoint(endpoint, '/', ['GET'])

        def middleware_a(req, res, next):
            self.middleware_a_called = True
            return next(req, res)

        def endpoint_a(req, res):
            self.endpoint_a_called = True
            res.set_data('A')
            return res

        router_a = Router()
        router_a._create_middleware(middleware_a, '/b', ['GET'])
        router_a._create_endpoint(endpoint_a, '/b', ['GET'])
        router.mount('/a', router_a)

        with self.subTest('test / path'):
            c = Client(router.build(), Response)
            res = c.get('/')
            self.assertTrue(self.middleware_called)
            self.assertTrue(self.endpoint_called)
            self.assertFalse(self.endpoint_a_called)
            self.assertEqual(res.data, b'Index')

        self.middleware_called = False
        self.endpoint_called = False
        self.middleware_a_called = False
        self.endpoint_a_called = False

        with self.subTest('test /a path'):
            c = Client(router._application, Response)
            res = c.get('/a/b')
            self.assertTrue(self.middleware_called)
            self.assertFalse(self.endpoint_called)
            self.assertTrue(self.middleware_a_called)
            self.assertTrue(self.endpoint_a_called)
            self.assertEqual(res.data, b'A')
Пример #2
0
    def test_wildcard(self):
        self.middleware_called = False
        self.endpoint_called = False
        self.endpoint_a_called = False

        def middleware(req, res, next):
            self.middleware_called = True
            return next(req, res)

        def endpoint_a(req, res):
            self.endpoint_a_called = True
            res.set_data('a')
            return res

        def endpoint(req, res):
            self.endpoint_called = True
            res.set_data('Index')
            return res

        router = Router()
        router._create_middleware(middleware, '%', ['GET'])
        router._create_endpoint(endpoint_a, '/a', ['GET'])
        router._create_endpoint(endpoint, '/', ['GET'])

        c = Client(router.build(), Response)
        res = c.get('/')
        self.assertTrue(self.middleware_called)
        self.assertTrue(self.endpoint_called)
        self.assertFalse(self.endpoint_a_called)
        self.assertEqual(res.data, b'Index')

        self.middleware_called = False
        self.endpoint_called = False
        self.endpoint_a_called = False

        c = Client(router._application, Response)
        res = c.get('/a')
        self.assertTrue(self.middleware_called)
        self.assertFalse(self.endpoint_called)
        self.assertTrue(self.endpoint_a_called)
        self.assertEqual(res.data, b'a')