当前位置: 首页>>代码示例>>Python>>正文


Python UrlDispatcher.add_static方法代码示例

本文整理汇总了Python中aiohttp.web.UrlDispatcher.add_static方法的典型用法代码示例。如果您正苦于以下问题:Python UrlDispatcher.add_static方法的具体用法?Python UrlDispatcher.add_static怎么用?Python UrlDispatcher.add_static使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在aiohttp.web.UrlDispatcher的用法示例。


在下文中一共展示了UrlDispatcher.add_static方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestUrlDispatcher

# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import add_static [as 别名]

#.........这里部分代码省略.........
        regexp = ("Duplicate 'name', already handled by")
        with self.assertRaisesRegex(ValueError, regexp):
            self.router.add_route('GET', '/get_other', handler2, name='name')

    def test_route_plain(self):
        handler = self.make_handler()
        route = self.router.add_route('GET', '/get', handler, name='name')
        route2 = next(iter(self.router['name']))
        url = route2.url()
        self.assertEqual('/get', url)
        self.assertIs(route, route2)

    def test_route_unknown_route_name(self):
        with self.assertRaises(KeyError):
            self.router['unknown']

    def test_route_dynamic(self):
        handler = self.make_handler()
        route = self.router.add_route('GET', '/get/{name}', handler,
                                      name='name')

        route2 = next(iter(self.router['name']))
        url = route2.url(parts={'name': 'John'})
        self.assertEqual('/get/John', url)
        self.assertIs(route, route2)

    def test_route_with_qs(self):
        handler = self.make_handler()
        self.router.add_route('GET', '/get', handler, name='name')

        url = self.router['name'].url(query=[('a', 'b'), ('c', 1)])
        self.assertEqual('/get?a=b&c=1', url)

    def test_add_static(self):
        route = self.router.add_static('/st',
                                       os.path.dirname(aiohttp.__file__),
                                       name='static')
        resource = self.router['static']
        url = resource.url(filename='/dir/a.txt')
        self.assertEqual('/st/dir/a.txt', url)
        self.assertIs(route, next(iter(resource)))

    def test_plain_not_match(self):
        handler = self.make_handler()
        self.router.add_route('GET', '/get/path', handler, name='name')
        route = self.router['name']
        self.assertIsNone(route._match('/another/path'))

    def test_dynamic_not_match(self):
        handler = self.make_handler()
        self.router.add_route('GET', '/get/{name}', handler, name='name')
        route = self.router['name']
        self.assertIsNone(route._match('/another/path'))

    def test_static_not_match(self):
        self.router.add_static('/pre', os.path.dirname(aiohttp.__file__),
                               name='name')
        route = self.router['name']
        self.assertIsNone(route._route.match('/another/path'))

    def test_dynamic_with_trailing_slash(self):
        handler = self.make_handler()
        self.router.add_route('GET', '/get/{name}/', handler, name='name')
        route = self.router['name']
        self.assertEqual({'name': 'John'}, route._match('/get/John/'))
开发者ID:1st1,项目名称:aiohttp,代码行数:69,代码来源:test_urldispatch.py

示例2: TestUrlDispatcher

# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import add_static [as 别名]

#.........这里部分代码省略.........
        regexp = ("Duplicate 'name', already handled by")
        with self.assertRaisesRegex(ValueError, regexp):
            self.router.add_route('GET', '/get_other', handler2, name='name')

    def test_route_plain(self):
        handler = self.make_handler()
        route = self.router.add_route('GET', '/get', handler, name='name')
        route2 = self.router['name']
        url = route2.url()
        self.assertEqual('/get', url)
        self.assertIs(route, route2)

    def test_route_unknown_route_name(self):
        with self.assertRaises(KeyError):
            self.router['unknown']

    def test_route_dynamic(self):
        handler = self.make_handler()
        route = self.router.add_route('GET', '/get/{name}', handler,
                                      name='name')

        route2 = self.router['name']
        url = route2.url(parts={'name': 'John'})
        self.assertEqual('/get/John', url)
        self.assertIs(route, route2)

    def test_route_with_qs(self):
        handler = self.make_handler()
        self.router.add_route('GET', '/get', handler, name='name')

        url = self.router['name'].url(query=[('a', 'b'), ('c', 1)])
        self.assertEqual('/get?a=b&c=1', url)

    def test_add_static(self):
        route = self.router.add_static('/st',
                                       os.path.dirname(aiohttp.__file__),
                                       name='static')
        route2 = self.router['static']
        url = route2.url(filename='/dir/a.txt')
        self.assertEqual('/st/dir/a.txt', url)
        self.assertIs(route, route2)

    def test_plain_not_match(self):
        handler = self.make_handler()
        self.router.add_route('GET', '/get/path', handler, name='name')
        route = self.router['name']
        self.assertIsNone(route.match('/another/path'))

    def test_dynamic_not_match(self):
        handler = self.make_handler()
        self.router.add_route('GET', '/get/{name}', handler, name='name')
        route = self.router['name']
        self.assertIsNone(route.match('/another/path'))

    def test_static_not_match(self):
        self.router.add_static('/pre', os.path.dirname(aiohttp.__file__),
                               name='name')
        route = self.router['name']
        self.assertIsNone(route.match('/another/path'))

    def test_dynamic_with_trailing_slash(self):
        handler = self.make_handler()
        self.router.add_route('GET', '/get/{name}/', handler, name='name')
        route = self.router['name']
        self.assertEqual({'name': 'John'}, route.match('/get/John/'))
开发者ID:Gennady-Andreyev,项目名称:aiohttp,代码行数:69,代码来源:test_urldispatch.py

示例3: TestUrlDispatcher

# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import add_static [as 别名]

#.........这里部分代码省略.........

        regexp = "Duplicate 'name', already handled by"
        with self.assertRaisesRegex(ValueError, regexp):
            self.router.add_route("GET", "/get_other", handler2, name="name")

    def test_route_plain(self):
        handler = self.make_handler()
        route = self.router.add_route("GET", "/get", handler, name="name")
        route2 = self.router["name"]
        url = route2.url()
        self.assertEqual("/get", url)
        self.assertIs(route, route2)

    def test_route_unknown_route_name(self):
        with self.assertRaises(KeyError):
            self.router["unknown"]

    def test_route_dynamic(self):
        handler = self.make_handler()
        route = self.router.add_route("GET", "/get/{name}", handler, name="name")

        route2 = self.router["name"]
        url = route2.url(parts={"name": "John"})
        self.assertEqual("/get/John", url)
        self.assertIs(route, route2)

    def test_route_with_qs(self):
        handler = self.make_handler()
        self.router.add_route("GET", "/get", handler, name="name")

        url = self.router["name"].url(query=[("a", "b"), ("c", 1)])
        self.assertEqual("/get?a=b&c=1", url)

    def test_add_static(self):
        route = self.router.add_static("/st", os.path.dirname(aiohttp.__file__), name="static")
        route2 = self.router["static"]
        url = route2.url(filename="/dir/a.txt")
        self.assertEqual("/st/dir/a.txt", url)
        self.assertIs(route, route2)

    def test_plain_not_match(self):
        handler = self.make_handler()
        self.router.add_route("GET", "/get/path", handler, name="name")
        route = self.router["name"]
        self.assertIsNone(route.match("/another/path"))

    def test_dynamic_not_match(self):
        handler = self.make_handler()
        self.router.add_route("GET", "/get/{name}", handler, name="name")
        route = self.router["name"]
        self.assertIsNone(route.match("/another/path"))

    def test_static_not_match(self):
        self.router.add_static("/pre", os.path.dirname(aiohttp.__file__), name="name")
        route = self.router["name"]
        self.assertIsNone(route.match("/another/path"))

    def test_dynamic_with_trailing_slash(self):
        handler = self.make_handler()
        self.router.add_route("GET", "/get/{name}/", handler, name="name")
        route = self.router["name"]
        self.assertEqual({"name": "John"}, route.match("/get/John/"))

    def test_len(self):
        handler = self.make_handler()
        self.router.add_route("GET", "/get1", handler, name="name1")
开发者ID:Axik,项目名称:aiohttp,代码行数:70,代码来源:test_urldispatch.py

示例4: TestUrlDispatcher

# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import add_static [as 别名]

#.........这里部分代码省略.........

        regexp = ("Duplicate 'name', already handled by")
        with self.assertRaisesRegex(ValueError, regexp):
            self.router.add_route('GET', '/get_other', lambda r: None,
                                  name='name')

    def test_route_plain(self):
        route = self.router.add_route('GET', '/get', lambda r: None,
                                      name='name')
        route2 = self.router['name']
        url = route2.url()
        self.assertEqual('/get', url)
        self.assertIs(route, route2)

    def test_route_unknown_route_name(self):
        with self.assertRaises(KeyError):
            self.router['unknown']

    def test_route_dynamic(self):
        route = self.router.add_route('GET', '/get/{name}',
                                      lambda r: None, name='name')

        route2 = self.router['name']
        url = route2.url(parts={'name': 'John'})
        self.assertEqual('/get/John', url)
        self.assertIs(route, route2)

    def test_route_with_qs(self):
        self.router.add_route('GET', '/get', lambda r: None, name='name')

        url = self.router['name'].url(query=[('a', 'b'), ('c', 1)])
        self.assertEqual('/get?a=b&c=1', url)

    def test_add_static(self):
        route = self.router.add_static('/st',
                                       os.path.dirname(aiohttp.__file__),
                                       name='static')
        route2 = self.router['static']
        url = route2.url(filename='/dir/a.txt')
        self.assertEqual('/st/dir/a.txt', url)
        self.assertIs(route, route2)

    def test_plain_not_match(self):
        self.router.add_route('GET', '/get/path',
                              lambda r: None, name='name')
        route = self.router['name']
        self.assertIsNone(route.match('/another/path'))

    def test_dynamic_not_match(self):
        self.router.add_route('GET', '/get/{name}',
                              lambda r: None, name='name')
        route = self.router['name']
        self.assertIsNone(route.match('/another/path'))

    def test_static_not_match(self):
        self.router.add_static('/pre', os.path.dirname(aiohttp.__file__),
                               name='name')
        route = self.router['name']
        self.assertIsNone(route.match('/another/path'))

    def test_dynamic_with_trailing_slash(self):
        self.router.add_route('GET', '/get/{name}/',
                              lambda r: None, name='name')
        route = self.router['name']
        self.assertEqual({'name': 'John'}, route.match('/get/John/'))
开发者ID:jgeerds,项目名称:aiohttp,代码行数:69,代码来源:test_urldispatch.py

示例5: TestWebSendFile

# 需要导入模块: from aiohttp.web import UrlDispatcher [as 别名]
# 或者: from aiohttp.web.UrlDispatcher import add_static [as 别名]
class TestWebSendFile(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_handler(self):
    #     @asyncio.coroutine
    #     def handler(request):
    #         return aiohttp.Response(request)  # pragma: no cover
    #
    #     return handler

    def test_env_nosendfile(self):
        directory = os.path.dirname(__file__)

        with mock.patch.dict(os.environ, {'AIOHTTP_NOSENDFILE': '1'}):
            route = web.StaticRoute(None, "/", directory)
            file_sender = FileSender(resp_factory=route._response_factory,
                                     chunk_size=route._chunk_size
                                     )
            self.assertEqual(file_sender._sendfile,
                             file_sender._sendfile_fallback)

    def test_static_handle_eof(self):
        loop = mock.Mock()
        route = self.router.add_static('/st',
                                       os.path.dirname(aiohttp.__file__))
        with mock.patch('aiohttp.helpers.os') as m_os:
            out_fd = 30
            in_fd = 31
            fut = helpers.create_future(self.loop)
            m_os.sendfile.return_value = 0
            file_sender = FileSender(resp_factory=route._response_factory,
                                     chunk_size=route._chunk_size
                                     )
            file_sender._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.helpers.os') as m_os:
            out_fd = 30
            in_fd = 31
            fut = helpers.create_future(self.loop)
            m_os.sendfile.side_effect = BlockingIOError()
            file_sender = FileSender(resp_factory=route._response_factory,
                                     chunk_size=route._chunk_size
                                     )
            file_sender._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,
                                               file_sender._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.helpers.os') as m_os:
            out_fd = 30
            in_fd = 31
            fut = helpers.create_future(self.loop)
            exc = OSError()
            m_os.sendfile.side_effect = exc
            file_sender = FileSender(resp_factory=route._response_factory,
                                     chunk_size=route._chunk_size
                                     )
            file_sender._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)
开发者ID:dkuznetsov,项目名称:aiohttp,代码行数:88,代码来源:test_websendfile.py


注:本文中的aiohttp.web.UrlDispatcher.add_static方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。