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


Python views.HTTPMethodView方法代码示例

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


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

示例1: test_with_middleware_response

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

    @app.middleware("request")
    async def process_request(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 = app.test_client.get("/")

    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:huge-success,项目名称:sanic,代码行数:26,代码来源:test_views.py

示例2: test_with_custom_class_methods

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [as 别名]
def test_with_custom_class_methods(app):
    class DummyView(HTTPMethodView):
        global_var = 0

        def _iternal_method(self):
            self.global_var += 10

        def get(self, request):
            self._iternal_method()
            return text(
                f"I am get method and global var " f"is {self.global_var}"
            )

    app.add_route(DummyView.as_view(), "/")
    request, response = app.test_client.get("/")
    assert response.text == "I am get method and global var is 10" 
开发者ID:huge-success,项目名称:sanic,代码行数:18,代码来源:test_views.py

示例3: test_with_decorator

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [as 别名]
def test_with_decorator(app):
    results = []

    def stupid_decorator(view):
        def decorator(*args, **kwargs):
            results.append(1)
            return view(*args, **kwargs)

        return decorator

    class DummyView(HTTPMethodView):
        decorators = [stupid_decorator]

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

    app.add_route(DummyView.as_view(), "/")
    request, response = app.test_client.get("/")
    assert response.text == "I am get method"
    assert results[0] == 1 
开发者ID:huge-success,项目名称:sanic,代码行数:22,代码来源:test_views.py

示例4: protected

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [as 别名]
def protected(initialized_on=None, **kw):
    def decorator(f):
        @wraps(f)
        async def decorated_function(request, *args, **kwargs):
            if issubclass(request.__class__, HTTPMethodView):
                request = args[0]
            kwargs.update(
                {
                    "initialized_on": initialized_on,
                    "kw": kw,
                    "request": request,
                    "f": f,
                }
            )
            return await _do_protection(*args, **kwargs)

        return decorated_function

    return decorator 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:21,代码来源:decorators.py

示例5: test_methods

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [as 别名]
def test_methods(app, method):
    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:huge-success,项目名称:sanic,代码行数:31,代码来源:test_views.py

示例6: test_unexisting_methods

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [as 别名]
def test_unexisting_methods(app):
    class DummyView(HTTPMethodView):
        def get(self, request):
            return text("I am get method")

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

示例7: test_argument_methods

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [as 别名]
def test_argument_methods(app):
    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 = app.test_client.get("/test123")

    assert response.text == "I am get method with test123" 
开发者ID:huge-success,项目名称:sanic,代码行数:12,代码来源:test_views.py

示例8: test_with_bp

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [as 别名]
def test_with_bp(app):
    bp = Blueprint("test_text")

    class DummyView(HTTPMethodView):
        def get(self, request):
            assert request.stream is None
            return text("I am get method")

    bp.add_route(DummyView.as_view(), "/")

    app.blueprint(bp)
    request, response = app.test_client.get("/")

    assert app.is_request_stream is False
    assert response.text == "I am get method" 
开发者ID:huge-success,项目名称:sanic,代码行数:17,代码来源:test_views.py

示例9: test_with_bp_with_url_prefix

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [as 别名]
def test_with_bp_with_url_prefix(app):
    bp = Blueprint("test_text", url_prefix="/test1")

    class DummyView(HTTPMethodView):
        def get(self, request):
            return text("I am get method")

    bp.add_route(DummyView.as_view(), "/")

    app.blueprint(bp)
    request, response = app.test_client.get("/test1/")

    assert response.text == "I am get method" 
开发者ID:huge-success,项目名称:sanic,代码行数:15,代码来源:test_views.py

示例10: test_request_stream_method_view

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

    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, StreamBuffer)
            result = ""
            while True:
                body = await request.stream.read()
                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:huge-success,项目名称:sanic,代码行数:32,代码来源:test_request_stream.py

示例11: test_request_stream_100_continue

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [as 别名]
def test_request_stream_100_continue(app, headers, expect_raise_exception):
    class SimpleView(HTTPMethodView):
        @stream_decorator
        async def post(self, request):
            assert isinstance(request.stream, StreamBuffer)
            result = ""
            while True:
                body = await request.stream.read()
                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

    if not expect_raise_exception:
        request, response = app.test_client.post(
            "/method_view", data=data, headers={"EXPECT": "100-continue"}
        )
        assert response.status == 200
        assert response.text == data
    else:
        with pytest.raises(ValueError) as e:
            app.test_client.post(
                "/method_view",
                data=data,
                headers={"EXPECT": "100-continue-extra"},
            )
            assert "Unknown Expect: 100-continue-extra" in str(e) 
开发者ID:huge-success,项目名称:sanic,代码行数:33,代码来源:test_request_stream.py

示例12: test_initialize_with_custom_endpoint_not_subclassed

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [as 别名]
def test_initialize_with_custom_endpoint_not_subclassed():
    class SubclassHTTPMethodView(HTTPMethodView):
        async def options(self, request):
            return text("", status=204)

        async def get(self, request):
            return text("ok")

    app = Sanic("sanic-jwt-test")
    with pytest.raises(exceptions.InvalidClassViewsFormat):
        Initialize(
            app,
            authenticate=lambda: True,
            class_views=[("/subclass", SubclassHTTPMethodView)],
        ) 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:17,代码来源:test_initialize.py

示例13: inject_user

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [as 别名]
def inject_user(initialized_on=None, **kw):
    def decorator(f):
        @wraps(f)
        async def decorated_function(request, *args, **kwargs):
            if issubclass(request.__class__, HTTPMethodView):
                request = args[0]

            if initialized_on and isinstance(
                initialized_on, Blueprint
            ):  # noqa
                instance = initialized_on
            else:
                instance = request.app

            with instant_config(instance, request=request, **kw):
                if request.method == "OPTIONS":
                    return await utils.call(
                        f, request, *args, **kwargs
                    )  # noqa

                payload = await instance.auth.extract_payload(request, verify=False)
                user = await utils.call(
                    instance.auth.retrieve_user, request, payload
                )
                response = f(request, user=user, *args, **kwargs)
                return await response

        return decorated_function

    return decorator 
开发者ID:ahopkins,项目名称:sanic-jwt,代码行数:32,代码来源:decorators.py

示例14: test_methods

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

    class DummyView(HTTPMethodView):

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

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

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

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

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

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

    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 == 'I am post method'
    request, response = sanic_endpoint_test(app, method="put")
    assert response.text == 'I am put method'
    request, response = sanic_endpoint_test(app, method="patch")
    assert response.text == 'I am patch method'
    request, response = sanic_endpoint_test(app, method="delete")
    assert response.text == 'I am delete method' 
开发者ID:hhstore,项目名称:annotated-py-projects,代码行数:34,代码来源:test_views.py

示例15: test_unexisting_methods

# 需要导入模块: from sanic import views [as 别名]
# 或者: from sanic.views import HTTPMethodView [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(), '/')
    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:hhstore,项目名称:annotated-py-projects,代码行数:15,代码来源:test_views.py


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