Support View and MethodView instances with async handlers

This commit is contained in:
Miguel Grinberg 2021-05-28 00:01:48 +01:00 committed by Phil Jones
parent 491ea32803
commit 270eb2df2a
4 changed files with 34 additions and 3 deletions

View file

@ -6,6 +6,8 @@ import pytest
from flask import Blueprint
from flask import Flask
from flask import request
from flask.views import MethodView
from flask.views import View
pytest.importorskip("asgiref")
@ -18,6 +20,24 @@ class BlueprintError(Exception):
pass
class AsyncView(View):
methods = ["GET", "POST"]
async def dispatch_request(self):
await asyncio.sleep(0)
return request.method
class AsyncMethodView(MethodView):
async def get(self):
await asyncio.sleep(0)
return 'GET'
async def post(self):
await asyncio.sleep(0)
return 'POST'
@pytest.fixture(name="async_app")
def _async_app():
app = Flask(__name__)
@ -53,11 +73,14 @@ def _async_app():
app.register_blueprint(blueprint, url_prefix="/bp")
app.add_url_rule('/view', view_func=AsyncView.as_view('view'))
app.add_url_rule('/methodview', view_func=AsyncMethodView.as_view('methodview'))
return app
@pytest.mark.skipif(sys.version_info < (3, 7), reason="requires Python >= 3.7")
@pytest.mark.parametrize("path", ["/", "/home", "/bp/"])
@pytest.mark.parametrize("path", ["/", "/home", "/bp/", "/view", "/methodview"])
def test_async_route(path, async_app):
test_client = async_app.test_client()
response = test_client.get(path)