本文整理汇总了Python中sanic.response.text方法的典型用法代码示例。如果您正苦于以下问题:Python response.text方法的具体用法?Python response.text怎么用?Python response.text使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类sanic.response
的用法示例。
在下文中一共展示了response.text方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_create_task
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_create_task(app):
e = Event()
async def coro():
await asyncio.sleep(0.05)
e.set()
app.add_task(coro)
@app.route("/early")
def not_set(request):
return text(str(e.is_set()))
@app.route("/late")
async def set(request):
await asyncio.sleep(0.1)
return text(str(e.is_set()))
request, response = app.test_client.get("/early")
assert response.body == b"False"
request, response = app.test_client.get("/late")
assert response.body == b"True"
示例2: test_chained_exception_handler
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_chained_exception_handler():
request, response = exception_handler_app.test_client.get(
"/6/0", debug=True
)
assert response.status == 500
soup = BeautifulSoup(response.body, "html.parser")
html = str(soup)
assert "response = handler(request, *args, **kwargs)" in html
assert "handler_6" in html
assert "foo = 1 / arg" in html
assert "ValueError" in html
assert "The above exception was the direct cause" in html
summary_text = " ".join(soup.select(".summary")[0].text.split())
assert (
"ZeroDivisionError: division by zero while handling path /6/0"
) == summary_text
示例3: test_versioned_routes_get
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_versioned_routes_get(app, method):
method = method.lower()
func = getattr(app, method)
if callable(func):
@func(f"/{method}", version=1)
def handler(request):
return text("OK")
else:
print(func)
raise Exception(f"Method: {method} is not callable")
client_method = getattr(app.test_client, method)
request, response = client_method(f"/v1/{method}")
assert response.status == 200
示例4: test_route_strict_slash
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_route_strict_slash(app):
@app.get("/get", strict_slashes=True)
def handler1(request):
assert request.stream is None
return text("OK")
@app.post("/post/", strict_slashes=True)
def handler2(request):
assert request.stream is None
return text("OK")
assert app.is_request_stream is False
request, response = app.test_client.get("/get")
assert response.text == "OK"
request, response = app.test_client.get("/get/")
assert response.status == 404
request, response = app.test_client.post("/post/")
assert response.text == "OK"
request, response = app.test_client.post("/post")
assert response.status == 404
示例5: test_route_slashes_overload
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_route_slashes_overload(app):
@app.get("/hello/")
def handler_get(request):
return text("OK")
@app.post("/hello/")
def handler_post(request):
return text("OK")
request, response = app.test_client.get("/hello")
assert response.text == "OK"
request, response = app.test_client.get("/hello/")
assert response.text == "OK"
request, response = app.test_client.post("/hello")
assert response.text == "OK"
request, response = app.test_client.post("/hello/")
assert response.text == "OK"
示例6: test_dynamic_route_regex
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_dynamic_route_regex(app):
@app.route("/folder/<folder_id:[A-Za-z0-9]{0,4}>")
async def handler(request, folder_id):
return text("OK")
request, response = app.test_client.get("/folder/test")
assert response.status == 200
request, response = app.test_client.get("/folder/test1")
assert response.status == 404
request, response = app.test_client.get("/folder/test-123")
assert response.status == 404
request, response = app.test_client.get("/folder/")
assert response.status == 200
示例7: test_dynamic_route_uuid
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_dynamic_route_uuid(app):
import uuid
results = []
@app.route("/quirky/<unique_id:uuid>")
async def handler(request, unique_id):
results.append(unique_id)
return text("OK")
url = "/quirky/123e4567-e89b-12d3-a456-426655440000"
request, response = app.test_client.get(url)
assert response.text == "OK"
assert type(results[0]) is uuid.UUID
generated_uuid = uuid.uuid4()
request, response = app.test_client.get(f"/quirky/{generated_uuid}")
assert response.status == 200
request, response = app.test_client.get("/quirky/non-existing")
assert response.status == 404
示例8: test_dynamic_route_path
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_dynamic_route_path(app):
@app.route("/<path:path>/info")
async def handler(request, path):
return text("OK")
request, response = app.test_client.get("/path/1/info")
assert response.status == 200
request, response = app.test_client.get("/info")
assert response.status == 404
@app.route("/<path:path>")
async def handler1(request, path):
return text("OK")
request, response = app.test_client.get("/info")
assert response.status == 200
request, response = app.test_client.get("/whatever/you/set")
assert response.status == 200
示例9: test_dynamic_add_route_string
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_dynamic_add_route_string(app):
results = []
async def handler(request, name):
results.append(name)
return text("OK")
app.add_route(handler, "/folder/<name:string>")
request, response = app.test_client.get("/folder/test123")
assert response.text == "OK"
assert results[0] == "test123"
request, response = app.test_client.get("/folder/favicon.ico")
assert response.text == "OK"
assert results[1] == "favicon.ico"
示例10: test_dynamic_add_route_regex
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_dynamic_add_route_regex(app):
async def handler(request, folder_id):
return text("OK")
app.add_route(handler, "/folder/<folder_id:[A-Za-z0-9]{0,4}>")
request, response = app.test_client.get("/folder/test")
assert response.status == 200
request, response = app.test_client.get("/folder/test1")
assert response.status == 404
request, response = app.test_client.get("/folder/test-123")
assert response.status == 404
request, response = app.test_client.get("/folder/")
assert response.status == 200
示例11: test_dynamic_add_route_unhashable
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_dynamic_add_route_unhashable(app):
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
request, response = app.test_client.get("/folder/test/nope/")
assert response.status == 404
示例12: test_redirect_with_params
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_redirect_with_params(app, test_str):
use_in_uri = quote(test_str)
@app.route("/api/v1/test/<test>/")
async def init_handler(request, test):
return redirect(f"/api/v2/test/{use_in_uri}/")
@app.route("/api/v2/test/<test>/")
async def target_handler(request, test):
assert test == test_str
return text("OK")
_, response = app.test_client.get(f"/api/v1/test/{use_in_uri}/")
assert response.status == 200
assert response.content == b"OK"
示例13: test_with_middleware
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [as 别名]
def test_with_middleware(app):
class DummyView(HTTPMethodView):
def get(self, request):
return text("I am get method")
app.add_route(DummyView.as_view(), "/")
results = []
@app.middleware
async def handler(request):
results.append(request)
request, response = app.test_client.get("/")
assert response.text == "I am get method"
assert type(results[0]) is Request
示例14: test_with_middleware_response
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [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)
示例15: test_with_custom_class_methods
# 需要导入模块: from sanic import response [as 别名]
# 或者: from sanic.response import text [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"