committed by
GitHub
3 changed files with 78 additions and 0 deletions
@ -0,0 +1,24 @@ |
|||||
|
from fastapi import BackgroundTasks, FastAPI |
||||
|
from fastapi.testclient import TestClient |
||||
|
from starlette.responses import BackgroundTask, Response |
||||
|
|
||||
|
app = FastAPI() |
||||
|
|
||||
|
|
||||
|
@app.get("/") |
||||
|
def endpoint(tasks: BackgroundTasks): |
||||
|
tasks.add_task(lambda: print("Dependency task executed")) |
||||
|
return Response( |
||||
|
content="Custom response", |
||||
|
background=BackgroundTask(lambda: print("Response task executed")), |
||||
|
) |
||||
|
|
||||
|
|
||||
|
client = TestClient(app) |
||||
|
|
||||
|
|
||||
|
def test_issue_11215(capsys): |
||||
|
client.get("/") |
||||
|
captured = capsys.readouterr() |
||||
|
assert "Dependency task executed" in captured.out |
||||
|
assert "Response task executed" in captured.out |
||||
@ -0,0 +1,29 @@ |
|||||
|
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