本文整理汇总了Python中aiohttp.web.UrlDispatcher.routes方法的典型用法代码示例。如果您正苦于以下问题:Python UrlDispatcher.routes方法的具体用法?Python UrlDispatcher.routes怎么用?Python UrlDispatcher.routes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.web.UrlDispatcher
的用法示例。
在下文中一共展示了UrlDispatcher.routes方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestUrlDispatcher
# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import routes [as 别名]
#.........这里部分代码省略.........
@asyncio.coroutine
def go():
handler = self.make_handler()
self.router.add_route('GET', '/{path}/{subpath}', handler)
resource_id = 'my%2Fpath%7Cwith%21some%25strange%24characters'
req = self.make_request('GET', '/path/{0}'.format(resource_id))
match_info = yield from self.router.resolve(req)
self.assertEqual(match_info, {
'path': 'path',
'subpath': unquote(resource_id)
})
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)
示例2: TestUrlDispatcher
# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import routes [as 别名]
#.........这里部分代码省略.........
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_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__),
示例3: TestUrlDispatcher
# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import routes [as 别名]
#.........这里部分代码省略.........
@asyncio.coroutine
def go():
handler = self.make_handler()
self.router.add_route("GET", "/{name}.{ext}", handler)
req = self.make_request("GET", "/file.html")
match_info = yield from self.router.resolve(req)
self.assertEqual({"name": "file", "ext": "html"}, match_info)
self.loop.run_until_complete(go())
def test_dynamic_match_unquoted_path(self):
@asyncio.coroutine
def go():
handler = self.make_handler()
self.router.add_route("GET", "/{path}/{subpath}", handler)
resource_id = "my%2Fpath%7Cwith%21some%25strange%24characters"
req = self.make_request("GET", "/path/{0}".format(resource_id))
match_info = yield from self.router.resolve(req)
self.assertEqual(match_info, {"path": "path", "subpath": unquote(resource_id)})
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)