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


Python Sanic.add_route方法代码示例

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


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

示例1: test_with_middleware_response

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_with_middleware_response():
    app = Sanic('test_with_middleware_response')

    results = []

    @app.middleware('request')
    async def process_response(request):
        results.append(request)

    @app.middleware('response')
    async def process_response(request, response):
        results.append(request)
        results.append(response)

    class DummyView(HTTPMethodView):

        def get(self, request):
            return text('I am get method')

    app.add_route(DummyView.as_view(), '/')

    request, response = sanic_endpoint_test(app)

    assert response.text == 'I am get method'
    assert type(results[0]) is Request
    assert type(results[1]) is Request
    assert isinstance(results[2], HTTPResponse)
开发者ID:blurrcat,项目名称:sanic,代码行数:29,代码来源:test_views.py

示例2: test_remove_route_without_clean_cache

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_remove_route_without_clean_cache():
    app = Sanic('test_remove_static_route')

    async def handler(request):
        return text('OK')

    app.add_route(handler, '/test')

    request, response = app.test_client.get('/test')
    assert response.status == 200

    app.remove_route('/test', clean_cache=True)

    request, response = app.test_client.get('/test')
    assert response.status == 404

    app.add_route(handler, '/test')

    request, response = app.test_client.get('/test')
    assert response.status == 200

    app.remove_route('/test', clean_cache=False)

    request, response = app.test_client.get('/test')
    assert response.status == 200
开发者ID:penelopeia,项目名称:sanic,代码行数:27,代码来源:test_routes.py

示例3: test_remove_unhashable_route

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_remove_unhashable_route():
    app = Sanic('test_remove_unhashable_route')

    async def handler(request, unhashable):
        return text('OK')

    app.add_route(handler, '/folder/<unhashable:[A-Za-z0-9/]+>/end/')

    request, response = app.test_client.get('/folder/test/asdf/end/')
    assert response.status == 200

    request, response = app.test_client.get('/folder/test///////end/')
    assert response.status == 200

    request, response = app.test_client.get('/folder/test/end/')
    assert response.status == 200

    app.remove_route('/folder/<unhashable:[A-Za-z0-9/]+>/end/')

    request, response = app.test_client.get('/folder/test/asdf/end/')
    assert response.status == 404

    request, response = app.test_client.get('/folder/test///////end/')
    assert response.status == 404

    request, response = app.test_client.get('/folder/test/end/')
    assert response.status == 404
开发者ID:penelopeia,项目名称:sanic,代码行数:29,代码来源:test_routes.py

示例4: test_methods

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_methods(method):
    app = Sanic('test_methods')

    class DummyView(HTTPMethodView):

        def get(self, request):
            return text('', headers={'method': 'GET'})

        def post(self, request):
            return text('', headers={'method': 'POST'})

        def put(self, request):
            return text('', headers={'method': 'PUT'})

        def head(self, request):
            return text('', headers={'method': 'HEAD'})

        def options(self, request):
            return text('', headers={'method': 'OPTIONS'})

        def patch(self, request):
            return text('', headers={'method': 'PATCH'})

        def delete(self, request):
            return text('', headers={'method': 'DELETE'})

    app.add_route(DummyView.as_view(), '/')

    request, response = sanic_endpoint_test(app, method=method)
    assert response.headers['method'] == method
开发者ID:blurrcat,项目名称:sanic,代码行数:32,代码来源:test_views.py

示例5: test_request_stream_composition_view

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_request_stream_composition_view():
    '''for self.is_request_stream = True'''

    app = Sanic('test_request_stream__composition_view')

    def get_handler(request):
        assert request.stream is None
        return text('OK')

    async def post_handler(request):
        assert isinstance(request.stream, asyncio.Queue)
        result = ''
        while True:
            body = await request.stream.get()
            if body is None:
                break
            result += body.decode('utf-8')
        return text(result)

    view = CompositionView()
    view.add(['GET'], get_handler)
    view.add(['POST'], post_handler, stream=True)
    app.add_route(view, '/composition_view')

    assert app.is_request_stream is True

    request, response = app.test_client.get('/composition_view')
    assert response.status == 200
    assert response.text == 'OK'

    request, response = app.test_client.post('/composition_view', data=data)
    assert response.status == 200
    assert response.text == data
开发者ID:penelopeia,项目名称:sanic,代码行数:35,代码来源:test_request_stream.py

示例6: test_methods

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_methods(method):
    app = Sanic('test_methods')

    class DummyView(HTTPMethodView):

        async def get(self, request):
            assert request.stream is None
            return text('', headers={'method': 'GET'})

        def post(self, request):
            return text('', headers={'method': 'POST'})

        async def put(self, request):
            return text('', headers={'method': 'PUT'})

        def head(self, request):
            return text('', headers={'method': 'HEAD'})

        def options(self, request):
            return text('', headers={'method': 'OPTIONS'})

        async def patch(self, request):
            return text('', headers={'method': 'PATCH'})

        def delete(self, request):
            return text('', headers={'method': 'DELETE'})

    app.add_route(DummyView.as_view(), '/')
    assert app.is_request_stream is False

    request, response = getattr(app.test_client, method.lower())('/')
    assert response.headers['method'] == method
开发者ID:penelopeia,项目名称:sanic,代码行数:34,代码来源:test_views.py

示例7: test_request_stream_method_view

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_request_stream_method_view():
    '''for self.is_request_stream = True'''

    app = Sanic('test_request_stream_method_view')

    class SimpleView(HTTPMethodView):

        def get(self, request):
            assert request.stream is None
            return text('OK')

        @stream_decorator
        async def post(self, request):
            assert isinstance(request.stream, asyncio.Queue)
            result = ''
            while True:
                body = await request.stream.get()
                if body is None:
                    break
                result += body.decode('utf-8')
            return text(result)

    app.add_route(SimpleView.as_view(), '/method_view')

    assert app.is_request_stream is True

    request, response = app.test_client.get('/method_view')
    assert response.status == 200
    assert response.text == 'OK'

    request, response = app.test_client.post('/method_view', data=data)
    assert response.status == 200
    assert response.text == data
开发者ID:penelopeia,项目名称:sanic,代码行数:35,代码来源:test_request_stream.py

示例8: test_remove_static_route

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_remove_static_route():
    app = Sanic('test_remove_static_route')

    async def handler1(request):
        return text('OK1')

    async def handler2(request):
        return text('OK2')

    app.add_route(handler1, '/test')
    app.add_route(handler2, '/test2')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200

    request, response = sanic_endpoint_test(app, uri='/test2')
    assert response.status == 200

    app.remove_route('/test')
    app.remove_route('/test2')

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 404

    request, response = sanic_endpoint_test(app, uri='/test2')
    assert response.status == 404
开发者ID:blurrcat,项目名称:sanic,代码行数:28,代码来源:test_routes.py

示例9: test_remove_static_route

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_remove_static_route():
    app = Sanic('test_remove_static_route')

    async def handler1(request):
        return text('OK1')

    async def handler2(request):
        return text('OK2')

    app.add_route(handler1, '/test')
    app.add_route(handler2, '/test2')

    request, response = app.test_client.get('/test')
    assert response.status == 200

    request, response = app.test_client.get('/test2')
    assert response.status == 200

    app.remove_route('/test')
    app.remove_route('/test2')

    request, response = app.test_client.get('/test')
    assert response.status == 404

    request, response = app.test_client.get('/test2')
    assert response.status == 404
开发者ID:penelopeia,项目名称:sanic,代码行数:28,代码来源:test_routes.py

示例10: test_url_attributes_with_ssl

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_url_attributes_with_ssl(path, query, expected_url):
    app = Sanic('test_url_attrs_with_ssl')

    current_dir = os.path.dirname(os.path.realpath(__file__))
    context = ssl.create_default_context(purpose=ssl.Purpose.CLIENT_AUTH)
    context.load_cert_chain(
        os.path.join(current_dir, 'certs/selfsigned.cert'),
        keyfile=os.path.join(current_dir, 'certs/selfsigned.key'))

    async def handler(request):
        return text('OK')

    app.add_route(handler, path)

    request, response = app.test_client.get(
        'https://{}:{}'.format(HOST, PORT) + path + '?{}'.format(query),
        server_kwargs={'ssl': context})
    assert request.url == expected_url.format(HOST, PORT)

    parsed = urlparse(request.url)

    assert parsed.scheme == request.scheme
    assert parsed.path == request.path
    assert parsed.query == request.query_string
    assert parsed.netloc == request.host
开发者ID:rezaprimasatya,项目名称:sanic,代码行数:27,代码来源:test_requests.py

示例11: test_add_route_method_not_allowed

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_add_route_method_not_allowed():
    app = Sanic('test_add_route_method_not_allowed')

    async def handler(request):
        return text('OK')

    app.add_route(handler, '/test', methods=['GET'])

    request, response = sanic_endpoint_test(app, uri='/test')
    assert response.status == 200

    request, response = sanic_endpoint_test(app, method='post', uri='/test')
    assert response.status == 405
开发者ID:blurrcat,项目名称:sanic,代码行数:15,代码来源:test_routes.py

示例12: test_add_route_method_not_allowed

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_add_route_method_not_allowed():
    app = Sanic('test_add_route_method_not_allowed')

    async def handler(request):
        return text('OK')

    app.add_route(handler, '/test', methods=['GET'])

    request, response = app.test_client.get('/test')
    assert response.status == 200

    request, response = app.test_client.post('/test')
    assert response.status == 405
开发者ID:penelopeia,项目名称:sanic,代码行数:15,代码来源:test_routes.py

示例13: test_unexisting_methods

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_unexisting_methods():
    app = Sanic('test_unexisting_methods')

    class DummyView(HTTPMethodView):

        def get(self, request):
            return text('I am get method')

    app.add_route(DummyView.as_view(), '/')
    request, response = sanic_endpoint_test(app, method="get")
    assert response.text == 'I am get method'
    request, response = sanic_endpoint_test(app, method="post")
    assert response.text == 'Error: Method POST not allowed for URL /'
开发者ID:blurrcat,项目名称:sanic,代码行数:15,代码来源:test_views.py

示例14: test_argument_methods

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_argument_methods():
    app = Sanic('test_argument_methods')

    class DummyView(HTTPMethodView):

        def get(self, request, my_param_here):
            return text('I am get method with %s' % my_param_here)

    app.add_route(DummyView.as_view(), '/<my_param_here>')

    request, response = sanic_endpoint_test(app, uri='/test123')

    assert response.text == 'I am get method with test123'
开发者ID:blurrcat,项目名称:sanic,代码行数:15,代码来源:test_views.py

示例15: test_remove_dynamic_route

# 需要导入模块: from sanic import Sanic [as 别名]
# 或者: from sanic.Sanic import add_route [as 别名]
def test_remove_dynamic_route():
    app = Sanic('test_remove_dynamic_route')

    async def handler(request, name):
        return text('OK')

    app.add_route(handler, '/folder/<name>')

    request, response = app.test_client.get('/folder/test123')
    assert response.status == 200

    app.remove_route('/folder/<name>')
    request, response = app.test_client.get('/folder/test123')
    assert response.status == 404
开发者ID:penelopeia,项目名称:sanic,代码行数:16,代码来源:test_routes.py


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