當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。