mirror of https://github.com/tiangolo/fastapi.git
Add variants for `additional_responses/tutorial004`
This commit is contained in:
parent
c8a99a62a4
commit
42e30173d3
|
|
@ -237,7 +237,7 @@ You can use that technique to reuse some predefined responses in your *path oper
|
||||||
|
|
||||||
For example:
|
For example:
|
||||||
|
|
||||||
{* ../../docs_src/additional_responses/tutorial004.py hl[13:17,26] *}
|
{* ../../docs_src/additional_responses/tutorial004_py310.py hl[11:15,24] *}
|
||||||
|
|
||||||
## More information about OpenAPI responses { #more-information-about-openapi-responses }
|
## More information about OpenAPI responses { #more-information-about-openapi-responses }
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,30 @@
|
||||||
|
from fastapi import FastAPI
|
||||||
|
from fastapi.responses import FileResponse
|
||||||
|
from pydantic import BaseModel
|
||||||
|
|
||||||
|
|
||||||
|
class Item(BaseModel):
|
||||||
|
id: str
|
||||||
|
value: str
|
||||||
|
|
||||||
|
|
||||||
|
responses = {
|
||||||
|
404: {"description": "Item not found"},
|
||||||
|
302: {"description": "The item was moved"},
|
||||||
|
403: {"description": "Not enough privileges"},
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
app = FastAPI()
|
||||||
|
|
||||||
|
|
||||||
|
@app.get(
|
||||||
|
"/items/{item_id}",
|
||||||
|
response_model=Item,
|
||||||
|
responses={**responses, 200: {"content": {"image/png": {}}}},
|
||||||
|
)
|
||||||
|
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 os
|
||||||
import shutil
|
import shutil
|
||||||
|
|
||||||
|
import pytest
|
||||||
from dirty_equals import IsDict
|
from dirty_equals import IsDict
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
from docs_src.additional_responses.tutorial004 import app
|
from tests.utils import needs_py310
|
||||||
|
|
||||||
client = TestClient(app)
|
|
||||||
|
|
||||||
|
|
||||||
def test_path_operation():
|
@pytest.fixture(
|
||||||
|
name="client",
|
||||||
|
params=[
|
||||||
|
pytest.param("tutorial004"),
|
||||||
|
pytest.param("tutorial004_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")
|
response = client.get("/items/foo")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json() == {"id": "foo", "value": "there goes my hero"}
|
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")
|
shutil.copy("./docs/en/docs/img/favicon.png", "./image.png")
|
||||||
response = client.get("/items/foo?img=1")
|
response = client.get("/items/foo?img=1")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
|
|
@ -24,7 +39,7 @@ def test_path_operation_img():
|
||||||
os.remove("./image.png")
|
os.remove("./image.png")
|
||||||
|
|
||||||
|
|
||||||
def test_openapi_schema():
|
def test_openapi_schema(client: TestClient):
|
||||||
response = client.get("/openapi.json")
|
response = client.get("/openapi.json")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json() == {
|
assert response.json() == {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue