async def decorated_function(request, *args, **kwargs): is_authenticated = check_token(request) if is_authenticated: response = await f(request, *args, **kwargs) return response else: return text("You are unauthorized.", 401)
async def on_authorization(request, authentication_session): """ Permissions authorization. """ await check_roles(request, request.form.get("role")) if request.form.get("permissions_required"): await check_permissions( request, *request.form.get("permissions_required").split(", ")) return text("Account permitted.")
async def on_role_assign(request, authentication_session): """ Assigns authenticated account a role. """ await assign_role( request.form.get("name"), "Role used for testing.", request.form.get("permissions"), authentication_session.bearer, ) return text("Role assigned.")
def test_request_line(app): app.route("/")(lambda _: text("")) request, _ = app.test_client.get( "/", headers={ "FOO": "bar", "Host": "example.com", "User-Agent": "Sanic-Testing", }, ) assert request.request_line == b"GET / HTTP/1.1"
def test_raw_headers(app): app.route("/")(lambda _: text("")) request, _ = app.test_client.get( "/", headers={ "FOO": "bar", "Host": "example.com", "User-Agent": "Sanic-Testing", }, ) assert request.raw_headers == ( b"Host: example.com\r\nAccept: */*\r\nAccept-Encoding: gzip, " b"deflate\r\nConnection: keep-alive\r\nUser-Agent: " b"Sanic-Testing\r\nFOO: bar")
async def http_receiver(request, protocol: T_BotProtocol): try: raw_event = request.json logger.debug(pformat(raw_event)) await handle_event(protocol, raw_event) except EventHandleError as e: logger.error(e) except Exception: logger.exception("事件处理异常") finally: return text("")
def test_raw_headers(app): app.route("/")(lambda _: text("")) request, _ = app.test_client.get( "/", headers={ "FOO": "bar", "Host": "example.com", "User-Agent": "Sanic-Testing", }, ) assert b"Host: example.com" in request.raw_headers assert b"Accept: */*" in request.raw_headers assert b"Accept-Encoding: gzip, deflate" in request.raw_headers assert b"Connection: keep-alive" in request.raw_headers assert b"User-Agent: Sanic-Testing" in request.raw_headers assert b"FOO: bar" in request.raw_headers
async def handleStaticDirError(request, exception): if isinstance(exception, IsADirectoryError): return text(error_text, status=403) raise exception
async def handler(request): return text(request.ip)
def foo(request): return text("foo")
async def secret(request): return text("To go fast, you must be fast")
async def hello_handler(request) -> HTTPResponse: return text("Hello")
async def base_handler(request): return text("111122223333444455556666777788889999")
def test(request): log.info("received request; responding with 'hey'") return text("hey")
async def file_not_found(request, exception): return text(f"No file: {request.path}", status=404)
async def do_login(request): token = jwt.encode({}, request.app.config.SECRET) return text(token)
def bar(request): return text("bar")
def handler(_): return text("It works")
def handler(_): return text("Done.")