本文整理匯總了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)
示例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"
示例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
示例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
示例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
示例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
示例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"
示例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"
示例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"
示例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
示例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)
示例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)],
)
示例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
示例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'
示例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 /'