mirror of
https://github.com/fastapi/fastapi.git
synced 2026-05-18 13:27:45 -04:00
✨ Allow setting the response_class to RedirectResponse and returning the URL from the function (#3457)
This commit is contained in:
committed by
GitHub
parent
ea8d7f689e
commit
dc5a966548
@@ -5,6 +5,32 @@ from docs_src.custom_response.tutorial006 import app
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
openapi_schema = {
|
||||
"openapi": "3.0.2",
|
||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||
"paths": {
|
||||
"/typer": {
|
||||
"get": {
|
||||
"summary": "Redirect Typer",
|
||||
"operationId": "redirect_typer_typer_get",
|
||||
"responses": {
|
||||
"200": {
|
||||
"description": "Successful Response",
|
||||
"content": {"application/json": {"schema": {}}},
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_get():
|
||||
response = client.get("/typer", allow_redirects=False)
|
||||
assert response.status_code == 307, response.text
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from docs_src.custom_response.tutorial006b import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
openapi_schema = {
|
||||
"openapi": "3.0.2",
|
||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||
"paths": {
|
||||
"/fastapi": {
|
||||
"get": {
|
||||
"summary": "Redirect Fastapi",
|
||||
"operationId": "redirect_fastapi_fastapi_get",
|
||||
"responses": {"307": {"description": "Successful Response"}},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_redirect_response_class():
|
||||
response = client.get("/fastapi", allow_redirects=False)
|
||||
assert response.status_code == 307
|
||||
assert response.headers["location"] == "https://fastapi.tiangolo.com"
|
||||
@@ -0,0 +1,32 @@
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from docs_src.custom_response.tutorial006c import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
openapi_schema = {
|
||||
"openapi": "3.0.2",
|
||||
"info": {"title": "FastAPI", "version": "0.1.0"},
|
||||
"paths": {
|
||||
"/pydantic": {
|
||||
"get": {
|
||||
"summary": "Redirect Pydantic",
|
||||
"operationId": "redirect_pydantic_pydantic_get",
|
||||
"responses": {"302": {"description": "Successful Response"}},
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
def test_openapi_schema():
|
||||
response = client.get("/openapi.json")
|
||||
assert response.status_code == 200, response.text
|
||||
assert response.json() == openapi_schema
|
||||
|
||||
|
||||
def test_redirect_status_code():
|
||||
response = client.get("/pydantic", allow_redirects=False)
|
||||
assert response.status_code == 302
|
||||
assert response.headers["location"] == "https://pydantic-docs.helpmanual.io/"
|
||||
17
tests/test_tutorial/test_custom_response/test_tutorial009.py
Normal file
17
tests/test_tutorial/test_custom_response/test_tutorial009.py
Normal file
@@ -0,0 +1,17 @@
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from docs_src.custom_response import tutorial009
|
||||
from docs_src.custom_response.tutorial009 import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_get(tmp_path: Path):
|
||||
file_path: Path = tmp_path / "large-video-file.mp4"
|
||||
tutorial009.some_file_path = str(file_path)
|
||||
test_content = b"Fake video bytes"
|
||||
file_path.write_bytes(test_content)
|
||||
response = client.get("/")
|
||||
assert response.content == test_content
|
||||
@@ -0,0 +1,17 @@
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from docs_src.custom_response import tutorial009b
|
||||
from docs_src.custom_response.tutorial009b import app
|
||||
|
||||
client = TestClient(app)
|
||||
|
||||
|
||||
def test_get(tmp_path: Path):
|
||||
file_path: Path = tmp_path / "large-video-file.mp4"
|
||||
tutorial009b.some_file_path = str(file_path)
|
||||
test_content = b"Fake video bytes"
|
||||
file_path.write_bytes(test_content)
|
||||
response = client.get("/")
|
||||
assert response.content == test_content
|
||||
Reference in New Issue
Block a user