MCPcopy
hub / github.com/fastapi/fastapi / test_request_with_depends_annotated

Function test_request_with_depends_annotated

tests/test_response_dependency.py:125–145  ·  view source on GitHub ↗

Request type hint should work in dependency chain.

()

Source from the content-addressed store, hash-verified

123
124# Tests for Request type hint with Depends
125def 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

Callers

nothing calls this directly

Calls 2

FastAPIClass · 0.90
getMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…