16 changed files with 285 additions and 93 deletions
@ -1,9 +1,9 @@ |
|||
from fastapi import FastAPI |
|||
from fastapi.responses import ORJSONResponse |
|||
from fastapi.responses import HTMLResponse |
|||
|
|||
app = FastAPI(default_response_class=ORJSONResponse) |
|||
app = FastAPI(default_response_class=HTMLResponse) |
|||
|
|||
|
|||
@app.get("/items/") |
|||
async def read_items(): |
|||
return [{"item_id": "Foo"}] |
|||
return "<h1>Items</h1><p>This is a list of items.</p>" |
|||
|
|||
@ -0,0 +1,73 @@ |
|||
import warnings |
|||
|
|||
import pytest |
|||
from fastapi import FastAPI |
|||
from fastapi.exceptions import FastAPIDeprecationWarning |
|||
from fastapi.responses import ORJSONResponse, UJSONResponse |
|||
from fastapi.testclient import TestClient |
|||
from pydantic import BaseModel |
|||
|
|||
|
|||
class Item(BaseModel): |
|||
name: str |
|||
price: float |
|||
|
|||
|
|||
# ORJSON |
|||
|
|||
|
|||
def _make_orjson_app() -> FastAPI: |
|||
with warnings.catch_warnings(): |
|||
warnings.simplefilter("ignore", FastAPIDeprecationWarning) |
|||
app = FastAPI(default_response_class=ORJSONResponse) |
|||
|
|||
@app.get("/items") |
|||
def get_items() -> Item: |
|||
return Item(name="widget", price=9.99) |
|||
|
|||
return app |
|||
|
|||
|
|||
def test_orjson_response_returns_correct_data(): |
|||
app = _make_orjson_app() |
|||
client = TestClient(app) |
|||
with warnings.catch_warnings(): |
|||
warnings.simplefilter("ignore", FastAPIDeprecationWarning) |
|||
response = client.get("/items") |
|||
assert response.status_code == 200 |
|||
assert response.json() == {"name": "widget", "price": 9.99} |
|||
|
|||
|
|||
def test_orjson_response_emits_deprecation_warning(): |
|||
with pytest.warns(FastAPIDeprecationWarning, match="ORJSONResponse is deprecated"): |
|||
ORJSONResponse(content={"hello": "world"}) |
|||
|
|||
|
|||
# UJSON |
|||
|
|||
|
|||
def _make_ujson_app() -> FastAPI: |
|||
with warnings.catch_warnings(): |
|||
warnings.simplefilter("ignore", FastAPIDeprecationWarning) |
|||
app = FastAPI(default_response_class=UJSONResponse) |
|||
|
|||
@app.get("/items") |
|||
def get_items() -> Item: |
|||
return Item(name="widget", price=9.99) |
|||
|
|||
return app |
|||
|
|||
|
|||
def test_ujson_response_returns_correct_data(): |
|||
app = _make_ujson_app() |
|||
client = TestClient(app) |
|||
with warnings.catch_warnings(): |
|||
warnings.simplefilter("ignore", FastAPIDeprecationWarning) |
|||
response = client.get("/items") |
|||
assert response.status_code == 200 |
|||
assert response.json() == {"name": "widget", "price": 9.99} |
|||
|
|||
|
|||
def test_ujson_response_emits_deprecation_warning(): |
|||
with pytest.warns(FastAPIDeprecationWarning, match="UJSONResponse is deprecated"): |
|||
UJSONResponse(content={"hello": "world"}) |
|||
@ -0,0 +1,50 @@ |
|||
import importlib |
|||
|
|||
import pytest |
|||
from fastapi.testclient import TestClient |
|||
from inline_snapshot import snapshot |
|||
|
|||
|
|||
@pytest.fixture( |
|||
name="client", |
|||
params=[ |
|||
pytest.param("tutorial010_py310"), |
|||
], |
|||
) |
|||
def get_client(request: pytest.FixtureRequest): |
|||
mod = importlib.import_module(f"docs_src.custom_response.{request.param}") |
|||
client = TestClient(mod.app) |
|||
return client |
|||
|
|||
|
|||
def test_get_custom_response(client: TestClient): |
|||
response = client.get("/items/") |
|||
assert response.status_code == 200, response.text |
|||
assert response.text == snapshot("<h1>Items</h1><p>This is a list of items.</p>") |
|||
|
|||
|
|||
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/": { |
|||
"get": { |
|||
"responses": { |
|||
"200": { |
|||
"description": "Successful Response", |
|||
"content": { |
|||
"text/html": {"schema": {"type": "string"}} |
|||
}, |
|||
} |
|||
}, |
|||
"summary": "Read Items", |
|||
"operationId": "read_items_items__get", |
|||
} |
|||
} |
|||
}, |
|||
} |
|||
) |
|||
Loading…
Reference in new issue