10 changed files with 74 additions and 196 deletions
@ -1,9 +1,9 @@ |
|||||
from fastapi import FastAPI |
from fastapi import FastAPI |
||||
from fastapi.responses import HTMLResponse |
from fastapi.responses import ORJSONResponse |
||||
|
|
||||
app = FastAPI(default_response_class=HTMLResponse) |
app = FastAPI(default_response_class=ORJSONResponse) |
||||
|
|
||||
|
|
||||
@app.get("/items/") |
@app.get("/items/") |
||||
async def read_items(): |
async def read_items(): |
||||
return "<h1>Items</h1><p>This is a list of items.</p>" |
return [{"item_id": "Foo"}] |
||||
|
|||||
@ -1,51 +0,0 @@ |
|||||
from unittest.mock import patch |
|
||||
|
|
||||
from fastapi import FastAPI |
|
||||
from fastapi.responses import JSONResponse |
|
||||
from fastapi.testclient import TestClient |
|
||||
from pydantic import BaseModel |
|
||||
|
|
||||
|
|
||||
class Item(BaseModel): |
|
||||
name: str |
|
||||
price: float |
|
||||
|
|
||||
|
|
||||
app = FastAPI() |
|
||||
|
|
||||
|
|
||||
@app.get("/default") |
|
||||
def get_default() -> Item: |
|
||||
return Item(name="widget", price=9.99) |
|
||||
|
|
||||
|
|
||||
@app.get("/explicit", response_class=JSONResponse) |
|
||||
def get_explicit() -> Item: |
|
||||
return Item(name="widget", price=9.99) |
|
||||
|
|
||||
|
|
||||
client = TestClient(app) |
|
||||
|
|
||||
|
|
||||
def test_default_response_class_skips_json_dumps(): |
|
||||
"""When no response_class is set, the fast path serializes directly to |
|
||||
JSON bytes via Pydantic's dump_json and never calls json.dumps.""" |
|
||||
with patch( |
|
||||
"starlette.responses.json.dumps", wraps=__import__("json").dumps |
|
||||
) as mock_dumps: |
|
||||
response = client.get("/default") |
|
||||
assert response.status_code == 200 |
|
||||
assert response.json() == {"name": "widget", "price": 9.99} |
|
||||
mock_dumps.assert_not_called() |
|
||||
|
|
||||
|
|
||||
def test_explicit_response_class_uses_json_dumps(): |
|
||||
"""When response_class is explicitly set to JSONResponse, the normal path |
|
||||
is used and json.dumps is called via JSONResponse.render().""" |
|
||||
with patch( |
|
||||
"starlette.responses.json.dumps", wraps=__import__("json").dumps |
|
||||
) as mock_dumps: |
|
||||
response = client.get("/explicit") |
|
||||
assert response.status_code == 200 |
|
||||
assert response.json() == {"name": "widget", "price": 9.99} |
|
||||
mock_dumps.assert_called_once() |
|
||||
@ -1,50 +0,0 @@ |
|||||
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