| 1 |
from collections.abc import Generator |
| 2 |
from contextlib import contextmanager |
| 3 |
from typing import Annotated, Any |
| 4 |
|
| 5 |
import pytest |
| 6 |
from fastapi import Depends, FastAPI |
| 7 |
from fastapi.responses import StreamingResponse |
| 8 |
from fastapi.testclient import TestClient |
| 9 |
|
| 10 |
|
| 11 |
class Session: |
| 12 |
def __init__(self) -> None: |
| 13 |
self.data = ["foo", "bar", "baz"] |
| 14 |
self.open = True |
| 15 |
|
| 16 |
def __iter__(self) -> Generator[str, None, None]: |
| 17 |
for item in self.data: |
| 18 |
if self.open: |
| 19 |
yield item |
| 20 |
else: |
| 21 |
raise ValueError("Session closed") |
| 22 |
|
| 23 |
|
| 24 |
@contextmanager |
| 25 |
def acquire_session() -> Generator[Session, None, None]: |
| 26 |
session = Session() |
| 27 |
try: |
| 28 |
yield session |
| 29 |
finally: |
| 30 |
session.open = False |
| 31 |
|
| 32 |
|
| 33 |
def dep_session() -> Any: |
| 34 |
with acquire_session() as s: |
| 35 |
yield s |
| 36 |
|
| 37 |
|
| 38 |
def broken_dep_session() -> Any: |
| 39 |
with acquire_session() as s: |
| 40 |
s.open = False |
| 41 |
yield s |
| 42 |
|
| 43 |
|
| 44 |
SessionDep = Annotated[Session, Depends(dep_session)] |
| 45 |
BrokenSessionDep = Annotated[Session, Depends(broken_dep_session)] |
| 46 |
|
| 47 |
app = FastAPI() |
| 48 |
|
| 49 |
|
| 50 |
@app.get("/data") |
| 51 |
def get_data(session: SessionDep) -> Any: |
| 52 |
data = list(session) |
| 53 |
return data |
| 54 |
|
| 55 |
|
| 56 |
@app.get("/stream-simple") |
| 57 |
def get_stream_simple(session: SessionDep) -> Any: |
| 58 |
def iter_data(): |
| 59 |
yield from ["x", "y", "z"] |
| 60 |
|
| 61 |
return StreamingResponse(iter_data()) |
| 62 |
|
| 63 |
|
| 64 |
@app.get("/stream-session") |
| 65 |
def get_stream_session(session: SessionDep) -> Any: |
| 66 |
def iter_data(): |
| 67 |
yield from session |
| 68 |
|
| 69 |
return StreamingResponse(iter_data()) |
| 70 |
|
| 71 |
|
| 72 |
@app.get("/broken-session-data") |
| 73 |
def get_broken_session_data(session: BrokenSessionDep) -> Any: |
| 74 |
return list(session) |
| 75 |
|
| 76 |
|
| 77 |
@app.get("/broken-session-stream") |
| 78 |
def get_broken_session_stream(session: BrokenSessionDep) -> Any: |
| 79 |
def iter_data(): |
| 80 |
yield from session |
| 81 |
|
| 82 |
return StreamingResponse(iter_data()) |
| 83 |
|
| 84 |
|
| 85 |
client = TestClient(app) |
| 86 |
|
| 87 |
|
| 88 |
def test_regular_no_stream(): |
| 89 |
response = client.get("/data") |
| 90 |
assert response.json() == ["foo", "bar", "baz"] |
| 91 |
|
| 92 |
|
| 93 |
def test_stream_simple(): |
| 94 |
response = client.get("/stream-simple") |
| 95 |
assert response.text == "xyz" |
| 96 |
|
| 97 |
|
| 98 |
def test_stream_session(): |
| 99 |
response = client.get("/stream-session") |
| 100 |
assert response.text == "foobarbaz" |
| 101 |
|
| 102 |
|
| 103 |
def test_broken_session_data(): |
| 104 |
with pytest.raises(ValueError, match="Session closed"): |
| 105 |
client.get("/broken-session-data") |
| 106 |
|
| 107 |
|
| 108 |
def test_broken_session_data_no_raise(): |
| 109 |
client = TestClient(app, raise_server_exceptions=False) |
| 110 |
response = client.get("/broken-session-data") |
| 111 |
assert response.status_code == 500 |
| 112 |
assert response.text == "Internal Server Error" |
| 113 |
|
| 114 |
|
| 115 |
def test_broken_session_stream_raise(): |
| 116 |
# Can raise ValueError on Pydantic v2 and ExceptionGroup on Pydantic v1 |
| 117 |
with pytest.raises((ValueError, Exception)): |
| 118 |
client.get("/broken-session-stream") |
| 119 |
|
| 120 |
|
| 121 |
def test_broken_session_stream_no_raise(): |
| 122 |
""" |
| 123 |
When a dependency with yield raises after the streaming response already started |
| 124 |
the 200 status code is already sent, but there's still an error in the server |
| 125 |
afterwards, an exception is raised and captured or shown in the server logs. |
| 126 |
""" |
| 127 |
with TestClient(app, raise_server_exceptions=False) as client: |
| 128 |
response = client.get("/broken-session-stream") |
| 129 |
assert response.status_code == 200 |
| 130 |
assert response.text == "" |