2 changed files with 27 additions and 1 deletions
@ -0,0 +1,26 @@ |
|||
from fastapi import BackgroundTasks, FastAPI |
|||
from fastapi.testclient import TestClient |
|||
from starlette.background import BackgroundTasks as StarletteBackgroundTasks |
|||
from starlette.responses import Response |
|||
|
|||
app = FastAPI() |
|||
|
|||
@app.get("/") |
|||
def endpoint(tasks: BackgroundTasks): |
|||
tasks.add_task(lambda: print("Dependency task")) |
|||
|
|||
response_tasks = StarletteBackgroundTasks() |
|||
response_tasks.add_task(lambda: print("Response task")) |
|||
|
|||
return Response( |
|||
content="Custom response", |
|||
background=response_tasks, |
|||
) |
|||
|
|||
client = TestClient(app) |
|||
|
|||
def test_issue_11215_response_background_tasks_collection(capsys): |
|||
client.get("/") |
|||
captured = capsys.readouterr() |
|||
assert "Dependency task" in captured.out |
|||
assert "Response task" in captured.out |
|||
Loading…
Reference in new issue