| 1 |
"""Test using special types (Response, Request, BackgroundTasks) as dependency annotations. |
| 2 |
|
| 3 |
These tests verify that special FastAPI types can be used with Depends() annotations |
| 4 |
and that the dependency injection system properly handles them. |
| 5 |
""" |
| 6 |
|
| 7 |
from typing import Annotated |
| 8 |
|
| 9 |
from fastapi import BackgroundTasks, Depends, FastAPI, Request, Response |
| 10 |
from fastapi.responses import JSONResponse |
| 11 |
from fastapi.testclient import TestClient |
| 12 |
|
| 13 |
|
| 14 |
def test_response_with_depends_annotated(): |
| 15 |
"""Response type hint should work with Annotated[Response, Depends(...)].""" |
| 16 |
app = FastAPI() |
| 17 |
|
| 18 |
def modify_response(response: Response) -> Response: |
| 19 |
response.headers["X-Custom"] = "modified" |
| 20 |
return response |
| 21 |
|
| 22 |
@app.get("/") |
| 23 |
def endpoint(response: Annotated[Response, Depends(modify_response)]): |
| 24 |
return {"status": "ok"} |
| 25 |
|
| 26 |
client = TestClient(app) |
| 27 |
resp = client.get("/") |
| 28 |
|
| 29 |
assert resp.status_code == 200 |
| 30 |
assert resp.json() == {"status": "ok"} |
| 31 |
assert resp.headers.get("X-Custom") == "modified" |
| 32 |
|
| 33 |
|
| 34 |
def test_response_with_depends_default(): |
| 35 |
"""Response type hint should work with Response = Depends(...).""" |
| 36 |
app = FastAPI() |
| 37 |
|
| 38 |
def modify_response(response: Response) -> Response: |
| 39 |
response.headers["X-Custom"] = "modified" |
| 40 |
return response |
| 41 |
|
| 42 |
@app.get("/") |
| 43 |
def endpoint(response: Response = Depends(modify_response)): |
| 44 |
return {"status": "ok"} |
| 45 |
|
| 46 |
client = TestClient(app) |
| 47 |
resp = client.get("/") |
| 48 |
|
| 49 |
assert resp.status_code == 200 |
| 50 |
assert resp.json() == {"status": "ok"} |
| 51 |
assert resp.headers.get("X-Custom") == "modified" |
| 52 |
|
| 53 |
|
| 54 |
def test_response_without_depends(): |
| 55 |
"""Regular Response injection should still work.""" |
| 56 |
app = FastAPI() |
| 57 |
|
| 58 |
@app.get("/") |
| 59 |
def endpoint(response: Response): |
| 60 |
response.headers["X-Direct"] = "set" |
| 61 |
return {"status": "ok"} |
| 62 |
|
| 63 |
client = TestClient(app) |
| 64 |
resp = client.get("/") |
| 65 |
|
| 66 |
assert resp.status_code == 200 |
| 67 |
assert resp.json() == {"status": "ok"} |
| 68 |
assert resp.headers.get("X-Direct") == "set" |
| 69 |
|
| 70 |
|
| 71 |
def test_response_dependency_chain(): |
| 72 |
"""Response dependency should work in a chain of dependencies.""" |
| 73 |
app = FastAPI() |
| 74 |
|
| 75 |
def first_modifier(response: Response) -> Response: |
| 76 |
response.headers["X-First"] = "1" |
| 77 |
return response |
| 78 |
|
| 79 |
def second_modifier( |
| 80 |
response: Annotated[Response, Depends(first_modifier)], |
| 81 |
) -> Response: |
| 82 |
response.headers["X-Second"] = "2" |
| 83 |
return response |
| 84 |
|
| 85 |
@app.get("/") |
| 86 |
def endpoint(response: Annotated[Response, Depends(second_modifier)]): |
| 87 |
return {"status": "ok"} |
| 88 |
|
| 89 |
client = TestClient(app) |
| 90 |
resp = client.get("/") |
| 91 |
|
| 92 |
assert resp.status_code == 200 |
| 93 |
assert resp.headers.get("X-First") == "1" |
| 94 |
assert resp.headers.get("X-Second") == "2" |
| 95 |
|
| 96 |
|
| 97 |
def test_response_dependency_returns_different_response_instance(): |
| 98 |
"""Dependency that returns a different Response instance should work. |
| 99 |
|
| 100 |
When a dependency returns a new Response object (e.g., JSONResponse) instead |
| 101 |
of modifying the injected one, the returned response should be used and any |
| 102 |
modifications to it in the endpoint should be preserved. |
| 103 |
""" |
| 104 |
app = FastAPI() |
| 105 |
|
| 106 |
def default_response() -> Response: |
| 107 |
response = JSONResponse(content={"status": "ok"}) |
| 108 |
response.headers["X-Custom"] = "initial" |
| 109 |
return response |
| 110 |
|
| 111 |
@app.get("/") |
| 112 |
def endpoint(response: Annotated[Response, Depends(default_response)]): |
| 113 |
response.headers["X-Custom"] = "modified" |
| 114 |
return response |
| 115 |
|
| 116 |
client = TestClient(app) |
| 117 |
resp = client.get("/") |
| 118 |
|
| 119 |
assert resp.status_code == 200 |
| 120 |
assert resp.json() == {"status": "ok"} |
| 121 |
assert resp.headers.get("X-Custom") == "modified" |
| 122 |
|
| 123 |
|
| 124 |
# Tests for Request type hint with Depends |
| 125 |
def test_request_with_depends_annotated(): |
| 126 |
"""Request type hint should work in dependency chain.""" |
| 127 |
app = FastAPI() |
| 128 |
|
| 129 |
def extract_request_info(request: Request) -> dict: |
| 130 |
return { |
| 131 |
"path": request.url.path, |
| 132 |
"user_agent": request.headers.get("user-agent", "unknown"), |
| 133 |
} |
| 134 |
|
| 135 |
@app.get("/") |
| 136 |
def endpoint( |
| 137 |
info: Annotated[dict, Depends(extract_request_info)], |
| 138 |
): |
| 139 |
return info |
| 140 |
|
| 141 |
client = TestClient(app) |
| 142 |
resp = client.get("/", headers={"user-agent": "test-agent"}) |
| 143 |
|
| 144 |
assert resp.status_code == 200 |
| 145 |
assert resp.json() == {"path": "/", "user_agent": "test-agent"} |
| 146 |
|
| 147 |
|
| 148 |
# Tests for BackgroundTasks type hint with Depends |
| 149 |
def test_background_tasks_with_depends_annotated(): |
| 150 |
"""BackgroundTasks type hint should work with Annotated[BackgroundTasks, Depends(...)].""" |
| 151 |
app = FastAPI() |
| 152 |
task_results = [] |
| 153 |
|
| 154 |
def background_task(message: str): |
| 155 |
task_results.append(message) |
| 156 |
|
| 157 |
def add_background_task(background_tasks: BackgroundTasks) -> BackgroundTasks: |
| 158 |
background_tasks.add_task(background_task, "from dependency") |
| 159 |
return background_tasks |
| 160 |
|
| 161 |
@app.get("/") |
| 162 |
def endpoint( |
| 163 |
background_tasks: Annotated[BackgroundTasks, Depends(add_background_task)], |
| 164 |
): |
| 165 |
background_tasks.add_task(background_task, "from endpoint") |
| 166 |
return {"status": "ok"} |
| 167 |
|
| 168 |
client = TestClient(app) |
| 169 |
resp = client.get("/") |
| 170 |
|
| 171 |
assert resp.status_code == 200 |
| 172 |
assert "from dependency" in task_results |
| 173 |
assert "from endpoint" in task_results |