Skip to content
Myr

test_async.py

This file

Path

tests/test_async.py

145 lines · 3.3 KiB · Python

Last changed by

Not known.

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_before_called = False99    app_after_called = False100    bp_before_called = False101    bp_after_called = False102103    app = Flask(__name__)104105    @app.route("/")106    def index():107        return ""108109    @app.before_request110    async def before():111        nonlocal app_before_called112        app_before_called = True113114    @app.after_request115    async def after(response):116        nonlocal app_after_called117        app_after_called = True118        return response119120    blueprint = Blueprint("bp", __name__)121122    @blueprint.route("/")123    def bp_index():124        return ""125126    @blueprint.before_request127    async def bp_before():128        nonlocal bp_before_called129        bp_before_called = True130131    @blueprint.after_request132    async def bp_after(response):133        nonlocal bp_after_called134        bp_after_called = True135        return response136137    app.register_blueprint(blueprint, url_prefix="/bp")138139    test_client = app.test_client()140    test_client.get("/")141    assert app_before_called142    assert app_after_called143    test_client.get("/bp/")144    assert bp_before_called145    assert bp_after_called