Add test for stream_with_context raising RuntimeError on async view

This commit is contained in:
Vignesh Deenadayal 2025-07-16 13:22:39 +05:30
parent 85c5d93cbd
commit 4d29df4361

View file

@ -234,19 +234,29 @@ class TestNoImports:
class TestStreaming: class TestStreaming:
def test_streaming_with_context(self, app, client): def test_stream_with_context_fails_with_async_route(self):
@app.route("/") import flask
def index(): import pytest
import gc
app = flask.Flask(__name__)
@app.route("/stream")
async def stream():
@flask.stream_with_context
def generate(): def generate():
yield "Hello " yield "hello"
yield flask.request.args["name"] return flask.Response(generate())
yield "!"
return flask.Response(flask.stream_with_context(generate())) with app.test_request_context("/stream"):
with pytest.raises(RuntimeError, match="Install Flask with the 'async' extra"):
# This forces Flask to run the async view and raise the RuntimeError
app.ensure_sync(app.view_functions["stream"])()
rv = client.get("/?name=World") # Clean up coroutine warnings
assert rv.data == b"Hello World!" gc.collect()
def test_streaming_with_context_as_decorator(self, app, client): def test_streaming_with_context_as_decorator(self, app, client):
@app.route("/") @app.route("/")
def index(): def index():