本文整理汇总了Python中aiohttp.web.UrlDispatcher.register_route方法的典型用法代码示例。如果您正苦于以下问题:Python UrlDispatcher.register_route方法的具体用法?Python UrlDispatcher.register_route怎么用?Python UrlDispatcher.register_route使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类aiohttp.web.UrlDispatcher
的用法示例。
在下文中一共展示了UrlDispatcher.register_route方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestUrlDispatcher
# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import register_route [as 别名]
class TestUrlDispatcher(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
self.router = UrlDispatcher()
def tearDown(self):
self.loop.close()
def make_request(self, method, path):
self.app = mock.Mock()
message = RawRequestMessage(method, path, HttpVersion(1, 1),
CIMultiDict(), False, False)
self.payload = mock.Mock()
self.transport = mock.Mock()
self.reader = mock.Mock()
self.writer = mock.Mock()
req = Request(self.app, message, self.payload,
self.transport, self.reader, self.writer)
return req
def make_handler(self):
@asyncio.coroutine
def handler(request):
return Response(request) # pragma: no cover
return handler
def test_system_route(self):
route = SystemRoute(201, 'test')
self.assertIsNone(route.match('any'))
with self.assertRaises(RuntimeError):
route.url()
self.assertEqual("<SystemRoute 201: test>", repr(route))
self.assertEqual(201, route.status)
self.assertEqual('test', route.reason)
def test_register_route(self):
handler = self.make_handler()
route = PlainRoute('GET', handler, 'test', '/handler/to/path')
self.router.register_route(route)
req = self.make_request('GET', '/handler/to/path')
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual(0, len(info))
self.assertIs(route, info.route)
self.assertIs(handler, info.handler)
self.assertEqual(info.route.name, 'test')
def test_register_route_checks(self):
self.assertRaises(
AssertionError, self.router.register_route, object())
handler = self.make_handler()
route = PlainRoute('GET', handler, 'test', '/handler/to/path')
self.router.register_route(route)
self.assertRaises(ValueError, self.router.register_route, route)
def test_add_route_root(self):
handler = self.make_handler()
self.router.add_route('GET', '/', handler)
req = self.make_request('GET', '/')
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual(0, len(info))
self.assertIs(handler, info.handler)
self.assertIsNone(info.route.name)
def test_add_route_simple(self):
handler = self.make_handler()
self.router.add_route('GET', '/handler/to/path', handler)
req = self.make_request('GET', '/handler/to/path')
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual(0, len(info))
self.assertIs(handler, info.handler)
self.assertIsNone(info.route.name)
def test_add_with_matchdict(self):
handler = self.make_handler()
self.router.add_route('GET', '/handler/{to}', handler)
req = self.make_request('GET', '/handler/tail')
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual({'to': 'tail'}, info)
self.assertIs(handler, info.handler)
self.assertIsNone(info.route.name)
def test_add_with_name(self):
handler = self.make_handler()
self.router.add_route('GET', '/handler/to/path', handler,
name='name')
req = self.make_request('GET', '/handler/to/path')
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual('name', info.route.name)
#.........这里部分代码省略.........
示例2: TestUrlDispatcher
# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import register_route [as 别名]
class TestUrlDispatcher(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
self.router = UrlDispatcher()
def tearDown(self):
self.loop.close()
def make_request(self, method, path):
self.app = mock.Mock()
message = RawRequestMessage(method, path, HttpVersion(1, 1),
CIMultiDict(), [], False, False)
self.payload = mock.Mock()
self.transport = mock.Mock()
self.reader = mock.Mock()
self.writer = mock.Mock()
req = Request(self.app, message, self.payload,
self.transport, self.reader, self.writer)
return req
def make_handler(self):
@asyncio.coroutine
def handler(request):
return Response(request) # pragma: no cover
return handler
def test_system_route(self):
route = SystemRoute(HTTPCreated(reason='test'))
self.assertIsNone(route.match('any'))
with self.assertRaises(RuntimeError):
route.url()
self.assertEqual("<SystemRoute 201: test>", repr(route))
self.assertEqual(201, route.status)
self.assertEqual('test', route.reason)
def test_register_route(self):
handler = self.make_handler()
route = PlainRoute('GET', handler, 'test', '/handler/to/path')
self.router.register_route(route)
req = self.make_request('GET', '/handler/to/path')
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual(0, len(info))
self.assertIs(route, info.route)
self.assertIs(handler, info.handler)
self.assertEqual(info.route.name, 'test')
def test_register_route_checks(self):
self.assertRaises(
AssertionError, self.router.register_route, object())
handler = self.make_handler()
route = PlainRoute('GET', handler, 'test', '/handler/to/path')
self.router.register_route(route)
self.assertRaises(ValueError, self.router.register_route, route)
route = PlainRoute('GET', handler, '1bad name', '/handler/to/path')
self.assertRaises(ValueError, self.router.register_route, route)
route = PlainRoute('GET', handler, 'return', '/handler/to/path')
self.assertRaises(ValueError, self.router.register_route, route)
route = PlainRoute('GET', handler, 'test.test:test-test',
'/handler/to/path')
self.router.register_route(route)
def test_add_route_root(self):
handler = self.make_handler()
self.router.add_route('GET', '/', handler)
req = self.make_request('GET', '/')
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual(0, len(info))
self.assertIs(handler, info.handler)
self.assertIsNone(info.route.name)
def test_add_route_simple(self):
handler = self.make_handler()
self.router.add_route('GET', '/handler/to/path', handler)
req = self.make_request('GET', '/handler/to/path')
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual(0, len(info))
self.assertIs(handler, info.handler)
self.assertIsNone(info.route.name)
def test_add_with_matchdict(self):
handler = self.make_handler()
self.router.add_route('GET', '/handler/{to}', handler)
req = self.make_request('GET', '/handler/tail')
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual({'to': 'tail'}, info)
self.assertIs(handler, info.handler)
self.assertIsNone(info.route.name)
#.........这里部分代码省略.........
示例3: TestUrlDispatcher
# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import register_route [as 别名]
class TestUrlDispatcher(unittest.TestCase):
def setUp(self):
self.loop = asyncio.new_event_loop()
asyncio.set_event_loop(None)
self.router = UrlDispatcher()
def tearDown(self):
self.loop.close()
def make_request(self, method, path):
self.app = mock.Mock()
message = RawRequestMessage(method, path, HttpVersion(1, 1), CIMultiDict(), False, False)
self.payload = mock.Mock()
self.transport = mock.Mock()
self.reader = mock.Mock()
self.writer = mock.Mock()
req = Request(self.app, message, self.payload, self.transport, self.reader, self.writer)
return req
def make_handler(self):
@asyncio.coroutine
def handler(request):
return Response(request) # pragma: no cover
return handler
def test_system_route(self):
route = SystemRoute(201, "test")
self.assertIsNone(route.match("any"))
with self.assertRaises(RuntimeError):
route.url()
self.assertEqual("<SystemRoute 201: test>", repr(route))
self.assertEqual(201, route.status)
self.assertEqual("test", route.reason)
def test_register_route(self):
handler = self.make_handler()
route = PlainRoute("GET", handler, "test", "/handler/to/path")
self.router.register_route(route)
req = self.make_request("GET", "/handler/to/path")
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual(0, len(info))
self.assertIs(route, info.route)
self.assertIs(handler, info.handler)
self.assertEqual(info.route.name, "test")
def test_register_route_checks(self):
self.assertRaises(AssertionError, self.router.register_route, object())
handler = self.make_handler()
route = PlainRoute("GET", handler, "test", "/handler/to/path")
self.router.register_route(route)
self.assertRaises(ValueError, self.router.register_route, route)
route = PlainRoute("GET", handler, "1bad name", "/handler/to/path")
self.assertRaises(ValueError, self.router.register_route, route)
route = PlainRoute("GET", handler, "return", "/handler/to/path")
self.assertRaises(ValueError, self.router.register_route, route)
def test_add_route_root(self):
handler = self.make_handler()
self.router.add_route("GET", "/", handler)
req = self.make_request("GET", "/")
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual(0, len(info))
self.assertIs(handler, info.handler)
self.assertIsNone(info.route.name)
def test_add_route_simple(self):
handler = self.make_handler()
self.router.add_route("GET", "/handler/to/path", handler)
req = self.make_request("GET", "/handler/to/path")
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual(0, len(info))
self.assertIs(handler, info.handler)
self.assertIsNone(info.route.name)
def test_add_with_matchdict(self):
handler = self.make_handler()
self.router.add_route("GET", "/handler/{to}", handler)
req = self.make_request("GET", "/handler/tail")
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual({"to": "tail"}, info)
self.assertIs(handler, info.handler)
self.assertIsNone(info.route.name)
def test_add_with_name(self):
handler = self.make_handler()
self.router.add_route("GET", "/handler/to/path", handler, name="name")
req = self.make_request("GET", "/handler/to/path")
info = self.loop.run_until_complete(self.router.resolve(req))
self.assertIsNotNone(info)
self.assertEqual("name", info.route.name)
#.........这里部分代码省略.........