本文整理汇总了Python中aiohttp.web.UrlDispatcher.named_routes方法的典型用法代码示例。如果您正苦于以下问题:Python UrlDispatcher.named_routes方法的具体用法?Python UrlDispatcher.named_routes怎么用?Python UrlDispatcher.named_routes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.web.UrlDispatcher
的用法示例。
在下文中一共展示了UrlDispatcher.named_routes方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestUrlDispatcher
# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import named_routes [as 别名]
#.........这里部分代码省略.........
self.router.add_route('INVALID_METHOD', '/path', handler)
def test_static_handle_eof(self):
loop = mock.Mock()
route = self.router.add_static('/st',
os.path.dirname(aiohttp.__file__))
with mock.patch('aiohttp.web_urldispatcher.os') as m_os:
out_fd = 30
in_fd = 31
fut = asyncio.Future(loop=self.loop)
m_os.sendfile.return_value = 0
route._sendfile_cb(fut, out_fd, in_fd, 0, 100, loop, False)
m_os.sendfile.assert_called_with(out_fd, in_fd, 0, 100)
self.assertTrue(fut.done())
self.assertIsNone(fut.result())
self.assertFalse(loop.add_writer.called)
self.assertFalse(loop.remove_writer.called)
def test_static_handle_again(self):
loop = mock.Mock()
route = self.router.add_static('/st',
os.path.dirname(aiohttp.__file__))
with mock.patch('aiohttp.web_urldispatcher.os') as m_os:
out_fd = 30
in_fd = 31
fut = asyncio.Future(loop=self.loop)
m_os.sendfile.side_effect = BlockingIOError()
route._sendfile_cb(fut, out_fd, in_fd, 0, 100, loop, False)
m_os.sendfile.assert_called_with(out_fd, in_fd, 0, 100)
self.assertFalse(fut.done())
loop.add_writer.assert_called_with(out_fd, route._sendfile_cb,
fut, out_fd, in_fd, 0, 100,
loop, True)
self.assertFalse(loop.remove_writer.called)
def test_static_handle_exception(self):
loop = mock.Mock()
route = self.router.add_static('/st',
os.path.dirname(aiohttp.__file__))
with mock.patch('aiohttp.web_urldispatcher.os') as m_os:
out_fd = 30
in_fd = 31
fut = asyncio.Future(loop=self.loop)
exc = OSError()
m_os.sendfile.side_effect = exc
route._sendfile_cb(fut, out_fd, in_fd, 0, 100, loop, False)
m_os.sendfile.assert_called_with(out_fd, in_fd, 0, 100)
self.assertTrue(fut.done())
self.assertIs(exc, fut.exception())
self.assertFalse(loop.add_writer.called)
self.assertFalse(loop.remove_writer.called)
def fill_routes(self):
route1 = self.router.add_route('GET', '/plain', self.make_handler())
route2 = self.router.add_route('GET', '/variable/{name}',
self.make_handler())
route3 = self.router.add_static('/static',
os.path.dirname(aiohttp.__file__))
return route1, route2, route3
def test_routes_view_len(self):
self.fill_routes()
self.assertEqual(3, len(self.router.routes()))
def test_routes_view_iter(self):
routes = self.fill_routes()
self.assertEqual(list(routes), list(self.router.routes()))
def test_routes_view_contains(self):
routes = self.fill_routes()
for route in routes:
self.assertIn(route, self.router.routes())
def test_routes_abc(self):
self.assertIsInstance(self.router.routes(), Sized)
self.assertIsInstance(self.router.routes(), Iterable)
self.assertIsInstance(self.router.routes(), Container)
def fill_named_routes(self):
route1 = self.router.add_route('GET', '/plain', self.make_handler(),
name='route1')
route2 = self.router.add_route('GET', '/variable/{name}',
self.make_handler(), name='route2')
route3 = self.router.add_static('/static',
os.path.dirname(aiohttp.__file__),
name='route3')
return route1, route2, route3
def test_named_routes_abc(self):
self.assertIsInstance(self.router.named_routes(), Mapping)
self.assertNotIsInstance(self.router.named_routes(), MutableMapping)
def test_named_routes(self):
named_routes = self.fill_named_routes()
self.assertEqual(3, len(self.router.named_routes()))
for route in named_routes:
self.assertIn(route.name, self.router.named_routes())
self.assertEqual(route, self.router.named_routes()[route.name])
示例2: TestUrlDispatcher
# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import named_routes [as 别名]
#.........这里部分代码省略.........
self.make_handler())
route3 = self.router.add_static('/static',
os.path.dirname(aiohttp.__file__))
return route1, route2, route3
def test_routes_view_len(self):
self.fill_routes()
self.assertEqual(3, len(self.router.routes()))
def test_routes_view_iter(self):
routes = self.fill_routes()
self.assertEqual(list(routes), list(self.router.routes()))
def test_routes_view_contains(self):
routes = self.fill_routes()
for route in routes:
self.assertIn(route, self.router.routes())
def test_routes_abc(self):
self.assertIsInstance(self.router.routes(), Sized)
self.assertIsInstance(self.router.routes(), Iterable)
self.assertIsInstance(self.router.routes(), Container)
def fill_named_resources(self):
route1 = self.router.add_route('GET', '/plain', self.make_handler(),
name='route1')
route2 = self.router.add_route('GET', '/variable/{name}',
self.make_handler(), name='route2')
route3 = self.router.add_static('/static',
os.path.dirname(aiohttp.__file__),
name='route3')
return route1.name, route2.name, route3.name
def test_named_routes_abc(self):
self.assertIsInstance(self.router.named_routes(), Mapping)
self.assertNotIsInstance(self.router.named_routes(), MutableMapping)
def test_named_resources_abc(self):
self.assertIsInstance(self.router.named_resources(), Mapping)
self.assertNotIsInstance(self.router.named_resources(), MutableMapping)
def test_named_routes(self):
self.fill_named_resources()
with self.assertWarns(DeprecationWarning):
self.assertEqual(3, len(self.router.named_routes()))
def test_named_resources(self):
names = self.fill_named_resources()
self.assertEqual(3, len(self.router.named_resources()))
for name in names:
self.assertIn(name, self.router.named_routes())
self.assertIsInstance(self.router.named_routes()[name],
AbstractResource)
def test_resource_adapter_not_match(self):
route = PlainRoute('GET', lambda req: None, None, '/path')
self.router.register_route(route)
resource = route.resource
self.assertIsNotNone(resource)
self.assertIsNone(resource._route.match('/another/path'))
def test_resource_adapter_resolve_not_math(self):
route = PlainRoute('GET', lambda req: None, None, '/path')
示例3: TestUrlDispatcher
# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import named_routes [as 别名]
#.........这里部分代码省略.........
self.loop.run_until_complete(go())
def test_add_route_not_started_with_slash(self):
with self.assertRaises(ValueError):
handler = self.make_handler()
self.router.add_route("GET", "invalid_path", handler)
def test_add_route_invalid_method(self):
with self.assertRaises(ValueError):
handler = self.make_handler()
self.router.add_route("INVALID_METHOD", "/path", handler)
def test_static_handle_eof(self):
loop = mock.Mock()
route = self.router.add_static("/st", os.path.dirname(aiohttp.__file__))
with mock.patch("aiohttp.web_urldispatcher.os") as m_os:
out_fd = 30
in_fd = 31
fut = asyncio.Future(loop=self.loop)
m_os.sendfile.return_value = 0
route._sendfile_cb(fut, out_fd, in_fd, 0, 100, loop, False)
m_os.sendfile.assert_called_with(out_fd, in_fd, 0, 100)
self.assertTrue(fut.done())
self.assertIsNone(fut.result())
self.assertFalse(loop.add_writer.called)
self.assertFalse(loop.remove_writer.called)
def test_static_handle_again(self):
loop = mock.Mock()
route = self.router.add_static("/st", os.path.dirname(aiohttp.__file__))
with mock.patch("aiohttp.web_urldispatcher.os") as m_os:
out_fd = 30
in_fd = 31
fut = asyncio.Future(loop=self.loop)
m_os.sendfile.side_effect = BlockingIOError()
route._sendfile_cb(fut, out_fd, in_fd, 0, 100, loop, False)
m_os.sendfile.assert_called_with(out_fd, in_fd, 0, 100)
self.assertFalse(fut.done())
loop.add_writer.assert_called_with(out_fd, route._sendfile_cb, fut, out_fd, in_fd, 0, 100, loop, True)
self.assertFalse(loop.remove_writer.called)
def test_static_handle_exception(self):
loop = mock.Mock()
route = self.router.add_static("/st", os.path.dirname(aiohttp.__file__))
with mock.patch("aiohttp.web_urldispatcher.os") as m_os:
out_fd = 30
in_fd = 31
fut = asyncio.Future(loop=self.loop)
exc = OSError()
m_os.sendfile.side_effect = exc
route._sendfile_cb(fut, out_fd, in_fd, 0, 100, loop, False)
m_os.sendfile.assert_called_with(out_fd, in_fd, 0, 100)
self.assertTrue(fut.done())
self.assertIs(exc, fut.exception())
self.assertFalse(loop.add_writer.called)
self.assertFalse(loop.remove_writer.called)
def fill_routes(self):
route1 = self.router.add_route("GET", "/plain", self.make_handler())
route2 = self.router.add_route("GET", "/variable/{name}", self.make_handler())
route3 = self.router.add_static("/static", os.path.dirname(aiohttp.__file__))
return route1, route2, route3
def test_routes_view_len(self):
self.fill_routes()
self.assertEqual(3, len(self.router.routes()))
def test_routes_view_iter(self):
routes = self.fill_routes()
self.assertEqual(list(routes), list(self.router.routes()))
def test_routes_view_contains(self):
routes = self.fill_routes()
for route in routes:
self.assertIn(route, self.router.routes())
def test_routes_abc(self):
self.assertIsInstance(self.router.routes(), Sized)
self.assertIsInstance(self.router.routes(), Iterable)
self.assertIsInstance(self.router.routes(), Container)
def fill_named_routes(self):
route1 = self.router.add_route("GET", "/plain", self.make_handler(), name="route1")
route2 = self.router.add_route("GET", "/variable/{name}", self.make_handler(), name="route2")
route3 = self.router.add_static("/static", os.path.dirname(aiohttp.__file__), name="route3")
return route1, route2, route3
def test_named_routes_abc(self):
self.assertIsInstance(self.router.named_routes(), Mapping)
self.assertNotIsInstance(self.router.named_routes(), MutableMapping)
def test_named_routes(self):
named_routes = self.fill_named_routes()
self.assertEqual(3, len(self.router.named_routes()))
for route in named_routes:
self.assertIn(route.name, self.router.named_routes())
self.assertEqual(route, self.router.named_routes()[route.name])