mirror of https://github.com/tiangolo/fastapi.git
Add variants for `additional_responses/tutorial002`
This commit is contained in:
parent
5b0625df96
commit
c8a99a62a4
|
|
@ -175,7 +175,7 @@ You can use this same `responses` parameter to add different media types for the
|
|||
|
||||
For example, you can add an additional media type of `image/png`, declaring that your *path operation* can return a JSON object (with media type `application/json`) or a PNG image:
|
||||
|
||||
{* ../../docs_src/additional_responses/tutorial002.py hl[19:24,28] *}
|
||||
{* ../../docs_src/additional_responses/tutorial002_py310.py hl[17:22,26] *}
|
||||
|
||||
/// note
|
||||
|
||||
|
|
|
|||
|
|
@ -0,0 +1,28 @@
|
|||
from fastapi import FastAPI
|
||||
from fastapi.responses import FileResponse
|
||||
from pydantic import BaseModel
|
||||
|
||||
|
||||
class Item(BaseModel):
|
||||
id: str
|
||||
value: str
|
||||
|
||||
|
||||
app = FastAPI()
|
||||
|
||||
|
||||
@app.get(
|
||||
"/items/{item_id}",
|
||||
response_model=Item,
|
||||
responses={
|
||||
200: {
|
||||
"content": {"image/png": {}},
|
||||
"description": "Return the JSON item or an image.",
|
||||
}
|
||||
},
|
||||
)
|
||||
async def read_item(item_id: str, img: bool | None = None):
|
||||
if img:
|
||||
return FileResponse("image.png", media_type="image/png")
|
||||
else:
|
||||
return {"id": "foo", "value": "there goes my hero"}
|
||||
|
|
@ -1,21 +1,36 @@
|
|||
import importlib
|
||||
import os
|
||||
import shutil
|
||||
|
||||
import pytest
|
||||
from dirty_equals import IsDict
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from docs_src.additional_responses.tutorial002 import app
|
||||
|
||||
client = TestClient(app)
|
||||
from tests.utils import needs_py310
|
||||
|
||||
|
||||
def test_path_operation():
|
||||
@pytest.fixture(
|
||||
name="client",
|
||||
params=[
|
||||
pytest.param("tutorial002"),
|
||||
pytest.param("tutorial002_py310", marks=needs_py310),
|
||||
],
|
||||
)
|
||||
def get_client(request: pytest.FixtureRequest):
|
||||
mod = importlib.import_module(f"docs_src.additional_responses.{request.param}")
|
||||
|
||||
client = TestClient(mod.app)
|
||||
client.headers.clear()
|
||||
return client
|
||||
|
||||
|
||||
def test_path_operation(client: TestClient):
|
||||
response = client.get("/items/foo")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {"id": "foo", "value": "there goes my hero"}
|
||||
|
||||
|
||||
def test_path_operation_img():
|
||||
def test_path_operation_img(client: TestClient):
|
||||
shutil.copy("./docs/en/docs/img/favicon.png", "./image.png")
|
||||
response = client.get("/items/foo?img=1")
|
||||
assert response.status_code == 200, response.text
|
||||
|
|
@ -24,7 +39,7 @@ def test_path_operation_img():
|
|||
os.remove("./image.png")
|
||||
|
||||
|
||||
def test_openapi_schema():
|
||||
def test_openapi_schema(client: TestClient):
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == {
|
||||
|
|
|
|||
Loading…
Reference in New Issue