Skip to content
Myr

test_async.py

This file

Path

tests/test_async.py

154 lines · 3.5 KiB · Python

Last changed by

Merge pull request #4621 from pallets/deprecate-before_first_requestDavid Lordbefore adoptionaab1d99

History of this file

Take it

1import asyncio23import pytest45from flask import Blueprint6from flask import Flask7from flask import request8from flask.views import MethodView9from flask.views import View1011pytest.importorskip("asgiref")121314class AppError(Exception):15    pass161718class BlueprintError(Exception):19    pass202122class AsyncView(View):23    methods = ["GET", "POST"]2425    async def dispatch_request(self):26        await asyncio.sleep(0)27        return request.method282930class AsyncMethodView(MethodView):31    async def get(self):32        await asyncio.sleep(0)33        return "GET"3435    async def post(self):36        await asyncio.sleep(0)37        return "POST"383940@pytest.fixture(name="async_app")41def _async_app():42    app = Flask(__name__)4344    @app.route("/", methods=["GET", "POST"])45    @app.route("/home", methods=["GET", "POST"])46    async def index():47        await asyncio.sleep(0)48        return request.method4950    @app.errorhandler(AppError)51    async def handle(_):52        return "", 4125354    @app.route("/error")55    async def error():56        raise AppError()5758    blueprint = Blueprint("bp", __name__)5960    @blueprint.route("/", methods=["GET", "POST"])61    async def bp_index():62        await asyncio.sleep(0)63        return request.method6465    @blueprint.errorhandler(BlueprintError)66    async def bp_handle(_):67        return "", 4126869    @blueprint.route("/error")70    async def bp_error():71        raise BlueprintError()7273    app.register_blueprint(blueprint, url_prefix="/bp")7475    app.add_url_rule("/view", view_func=AsyncView.as_view("view"))76    app.add_url_rule("/methodview", view_func=AsyncMethodView.as_view("methodview"))7778    return app798081@pytest.mark.parametrize("path", ["/", "/home", "/bp/", "/view", "/methodview"])82def test_async_route(path, async_app):83    test_client = async_app.test_client()84    response = test_client.get(path)85    assert b"GET" in response.get_data()86    response = test_client.post(path)87    assert b"POST" in response.get_data()888990@pytest.mark.parametrize("path", ["/error", "/bp/error"])91def test_async_error_handler(path, async_app):92    test_client = async_app.test_client()93    response = test_client.get(path)94    assert response.status_code == 412959697def test_async_before_after_request():98    app_first_called = False99    app_before_called = False100    app_after_called = False101    bp_before_called = False102    bp_after_called = False103104    app = Flask(__name__)105106    @app.route("/")107    def index():108        return ""109110    with pytest.deprecated_call():111112        @app.before_first_request113        async def before_first():114            nonlocal app_first_called115            app_first_called = True116117    @app.before_request118    async def before():119        nonlocal app_before_called120        app_before_called = True121122    @app.after_request123    async def after(response):124        nonlocal app_after_called125        app_after_called = True126        return response127128    blueprint = Blueprint("bp", __name__)129130    @blueprint.route("/")131    def bp_index():132        return ""133134    @blueprint.before_request135    async def bp_before():136        nonlocal bp_before_called137        bp_before_called = True138139    @blueprint.after_request140    async def bp_after(response):141        nonlocal bp_after_called142        bp_after_called = True143        return response144145    app.register_blueprint(blueprint, url_prefix="/bp")146147    test_client = app.test_client()148    test_client.get("/")149    assert app_first_called150    assert app_before_called151    assert app_after_called152    test_client.get("/bp/")153    assert bp_before_called154    assert bp_after_called