Home / File/ test_response_dependency.py — fastapi Source File

test_response_dependency.py — fastapi Source File

Architecture documentation for test_response_dependency.py, a python file in the fastapi codebase. 4 imports, 0 dependents.

File python FastAPI Responses 4 imports 7 functions

Entity Profile

Dependency Diagram

graph LR
  b16d0181_b13b_77f6_4428_f731d5a25f66["test_response_dependency.py"]
  0dda2280_3359_8460_301c_e98c77e78185["typing"]
  b16d0181_b13b_77f6_4428_f731d5a25f66 --> 0dda2280_3359_8460_301c_e98c77e78185
  534f6e44_61b8_3c38_8b89_6934a6df9802["__init__.py"]
  b16d0181_b13b_77f6_4428_f731d5a25f66 --> 534f6e44_61b8_3c38_8b89_6934a6df9802
  967b6712_70e2_f5fa_f671_7c149857a445["responses.py"]
  b16d0181_b13b_77f6_4428_f731d5a25f66 --> 967b6712_70e2_f5fa_f671_7c149857a445
  a7c04dee_ee23_5891_b185_47ff6bed036d["testclient.py"]
  b16d0181_b13b_77f6_4428_f731d5a25f66 --> a7c04dee_ee23_5891_b185_47ff6bed036d
  style b16d0181_b13b_77f6_4428_f731d5a25f66 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

"""Test using special types (Response, Request, BackgroundTasks) as dependency annotations.

These tests verify that special FastAPI types can be used with Depends() annotations
and that the dependency injection system properly handles them.
"""

from typing import Annotated

from fastapi import BackgroundTasks, Depends, FastAPI, Request, Response
from fastapi.responses import JSONResponse
from fastapi.testclient import TestClient


def test_response_with_depends_annotated():
    """Response type hint should work with Annotated[Response, Depends(...)]."""
    app = FastAPI()

    def modify_response(response: Response) -> Response:
        response.headers["X-Custom"] = "modified"
        return response

    @app.get("/")
    def endpoint(response: Annotated[Response, Depends(modify_response)]):
        return {"status": "ok"}

    client = TestClient(app)
    resp = client.get("/")

    assert resp.status_code == 200
    assert resp.json() == {"status": "ok"}
    assert resp.headers.get("X-Custom") == "modified"


def test_response_with_depends_default():
    """Response type hint should work with Response = Depends(...)."""
    app = FastAPI()

    def modify_response(response: Response) -> Response:
        response.headers["X-Custom"] = "modified"
        return response

    @app.get("/")
    def endpoint(response: Response = Depends(modify_response)):
        return {"status": "ok"}

    client = TestClient(app)
    resp = client.get("/")

    assert resp.status_code == 200
    assert resp.json() == {"status": "ok"}
    assert resp.headers.get("X-Custom") == "modified"


def test_response_without_depends():
    """Regular Response injection should still work."""
    app = FastAPI()

    @app.get("/")
    def endpoint(response: Response):
        response.headers["X-Direct"] = "set"
// ... (114 more lines)

Domain

Subdomains

Frequently Asked Questions

What does test_response_dependency.py do?
test_response_dependency.py is a source file in the fastapi codebase, written in python. It belongs to the FastAPI domain, Responses subdomain.
What functions are defined in test_response_dependency.py?
test_response_dependency.py defines 7 function(s): test_background_tasks_with_depends_annotated, test_request_with_depends_annotated, test_response_dependency_chain, test_response_dependency_returns_different_response_instance, test_response_with_depends_annotated, test_response_with_depends_default, test_response_without_depends.
What does test_response_dependency.py depend on?
test_response_dependency.py imports 4 module(s): __init__.py, responses.py, testclient.py, typing.
Where is test_response_dependency.py in the architecture?
test_response_dependency.py is located at tests/test_response_dependency.py (domain: FastAPI, subdomain: Responses, directory: tests).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free