committed by
GitHub
6 changed files with 263 additions and 0 deletions
@ -0,0 +1,33 @@ |
|||
from collections.abc import AsyncIterable |
|||
|
|||
from fastapi import Depends, FastAPI, Response |
|||
from fastapi.sse import EventSourceResponse |
|||
from pydantic import BaseModel |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
class Item(BaseModel): |
|||
name: str |
|||
description: str | None |
|||
|
|||
|
|||
items = [ |
|||
Item(name="Plumbus", description="A multi-purpose household device."), |
|||
Item(name="Portal Gun", description="A portal opening device."), |
|||
Item(name="Meeseeks Box", description="A box that summons a Meeseeks."), |
|||
] |
|||
|
|||
|
|||
async def add_stream_headers(response: Response) -> None: |
|||
response.headers["X-Stream-Source"] = "inventory" |
|||
|
|||
|
|||
@app.get( |
|||
"/items/stream", |
|||
response_class=EventSourceResponse, |
|||
dependencies=[Depends(add_stream_headers)], |
|||
) |
|||
async def sse_items() -> AsyncIterable[Item]: |
|||
for item in items: |
|||
yield item |
|||
@ -0,0 +1,28 @@ |
|||
from collections.abc import AsyncIterable |
|||
|
|||
from fastapi import Depends, FastAPI, Response |
|||
from pydantic import BaseModel |
|||
|
|||
app = FastAPI() |
|||
|
|||
|
|||
class Item(BaseModel): |
|||
name: str |
|||
description: str | None |
|||
|
|||
|
|||
items = [ |
|||
Item(name="Plumbus", description="A multi-purpose household device."), |
|||
Item(name="Portal Gun", description="A portal opening device."), |
|||
Item(name="Meeseeks Box", description="A box that summons a Meeseeks."), |
|||
] |
|||
|
|||
|
|||
async def add_stream_headers(response: Response) -> None: |
|||
response.headers["X-Stream-Source"] = "inventory" |
|||
|
|||
|
|||
@app.get("/items/stream", dependencies=[Depends(add_stream_headers)]) |
|||
async def stream_items() -> AsyncIterable[Item]: |
|||
for item in items: |
|||
yield item |
|||
@ -0,0 +1,96 @@ |
|||
import importlib |
|||
|
|||
import pytest |
|||
from fastapi.testclient import TestClient |
|||
from inline_snapshot import snapshot |
|||
|
|||
|
|||
@pytest.fixture( |
|||
name="client", |
|||
params=[ |
|||
pytest.param("tutorial006_py310"), |
|||
], |
|||
) |
|||
def get_client(request: pytest.FixtureRequest): |
|||
mod = importlib.import_module(f"docs_src.server_sent_events.{request.param}") |
|||
|
|||
client = TestClient(mod.app) |
|||
return client |
|||
|
|||
|
|||
def test_stream_items_with_custom_header(client: TestClient): |
|||
response = client.get("/items/stream") |
|||
assert response.status_code == 200, response.text |
|||
assert response.headers["content-type"] == "text/event-stream; charset=utf-8" |
|||
assert response.headers["x-stream-source"] == "inventory" |
|||
data_lines = [ |
|||
line for line in response.text.strip().split("\n") if line.startswith("data: ") |
|||
] |
|||
assert len(data_lines) == 3 |
|||
assert '"name":"Plumbus"' in data_lines[0] or '"name": "Plumbus"' in data_lines[0] |
|||
|
|||
|
|||
def test_openapi_schema(client: TestClient): |
|||
response = client.get("/openapi.json") |
|||
assert response.status_code == 200, response.text |
|||
assert response.json() == snapshot( |
|||
{ |
|||
"openapi": "3.1.0", |
|||
"info": {"title": "FastAPI", "version": "0.1.0"}, |
|||
"paths": { |
|||
"/items/stream": { |
|||
"get": { |
|||
"summary": "Sse Items", |
|||
"operationId": "sse_items_items_stream_get", |
|||
"responses": { |
|||
"200": { |
|||
"description": "Successful Response", |
|||
"content": { |
|||
"text/event-stream": { |
|||
"itemSchema": { |
|||
"type": "object", |
|||
"properties": { |
|||
"data": { |
|||
"type": "string", |
|||
"contentMediaType": "application/json", |
|||
"contentSchema": { |
|||
"$ref": "#/components/schemas/Item" |
|||
}, |
|||
}, |
|||
"event": {"type": "string"}, |
|||
"id": {"type": "string"}, |
|||
"retry": { |
|||
"type": "integer", |
|||
"minimum": 0, |
|||
}, |
|||
}, |
|||
"required": ["data"], |
|||
} |
|||
} |
|||
}, |
|||
} |
|||
}, |
|||
} |
|||
} |
|||
}, |
|||
"components": { |
|||
"schemas": { |
|||
"Item": { |
|||
"properties": { |
|||
"name": {"type": "string", "title": "Name"}, |
|||
"description": { |
|||
"anyOf": [ |
|||
{"type": "string"}, |
|||
{"type": "null"}, |
|||
], |
|||
"title": "Description", |
|||
}, |
|||
}, |
|||
"type": "object", |
|||
"required": ["name", "description"], |
|||
"title": "Item", |
|||
} |
|||
} |
|||
}, |
|||
} |
|||
) |
|||
@ -0,0 +1,82 @@ |
|||
import importlib |
|||
import json |
|||
|
|||
import pytest |
|||
from fastapi.testclient import TestClient |
|||
from inline_snapshot import snapshot |
|||
|
|||
|
|||
@pytest.fixture( |
|||
name="client", |
|||
params=[ |
|||
pytest.param("tutorial002_py310"), |
|||
], |
|||
) |
|||
def get_client(request: pytest.FixtureRequest): |
|||
mod = importlib.import_module(f"docs_src.stream_json_lines.{request.param}") |
|||
|
|||
client = TestClient(mod.app) |
|||
return client |
|||
|
|||
|
|||
def test_stream_items_with_custom_header(client: TestClient): |
|||
response = client.get("/items/stream") |
|||
assert response.status_code == 200, response.text |
|||
assert response.headers["content-type"] == "application/jsonl" |
|||
assert response.headers["x-stream-source"] == "inventory" |
|||
lines = [json.loads(line) for line in response.text.strip().splitlines()] |
|||
assert lines == [ |
|||
{"name": "Plumbus", "description": "A multi-purpose household device."}, |
|||
{"name": "Portal Gun", "description": "A portal opening device."}, |
|||
{"name": "Meeseeks Box", "description": "A box that summons a Meeseeks."}, |
|||
] |
|||
|
|||
|
|||
def test_openapi_schema(client: TestClient): |
|||
response = client.get("/openapi.json") |
|||
assert response.status_code == 200, response.text |
|||
assert response.json() == snapshot( |
|||
{ |
|||
"openapi": "3.1.0", |
|||
"info": {"title": "FastAPI", "version": "0.1.0"}, |
|||
"paths": { |
|||
"/items/stream": { |
|||
"get": { |
|||
"responses": { |
|||
"200": { |
|||
"description": "Successful Response", |
|||
"content": { |
|||
"application/jsonl": { |
|||
"itemSchema": { |
|||
"$ref": "#/components/schemas/Item" |
|||
}, |
|||
} |
|||
}, |
|||
} |
|||
}, |
|||
"summary": "Stream Items", |
|||
"operationId": "stream_items_items_stream_get", |
|||
} |
|||
} |
|||
}, |
|||
"components": { |
|||
"schemas": { |
|||
"Item": { |
|||
"properties": { |
|||
"name": {"type": "string", "title": "Name"}, |
|||
"description": { |
|||
"anyOf": [ |
|||
{"type": "string"}, |
|||
{"type": "null"}, |
|||
], |
|||
"title": "Description", |
|||
}, |
|||
}, |
|||
"type": "object", |
|||
"required": ["name", "description"], |
|||
"title": "Item", |
|||
} |
|||
} |
|||
}, |
|||
} |
|||
) |
|||
Loading…
Reference in new issue