mirror of https://github.com/tiangolo/fastapi.git
Fix and improve tests for `dics_src/custom_docs_ui/tutorial002.py`
This commit is contained in:
parent
3a1081dc7c
commit
d7029b2d71
|
|
@ -1,54 +1,94 @@
|
||||||
|
import importlib
|
||||||
import os
|
import os
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from typing import TypedDict
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from fastapi.testclient import TestClient
|
from fastapi.testclient import TestClient
|
||||||
|
|
||||||
root_path = "/api"
|
ROOT_PATH = "/api"
|
||||||
|
|
||||||
|
|
||||||
@pytest.fixture(scope="module")
|
class Params(TypedDict):
|
||||||
def client():
|
app_root_path: str
|
||||||
|
asgi_root_path: str
|
||||||
|
request_prefix: str
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture(
|
||||||
|
params=[
|
||||||
|
Params(app_root_path="", asgi_root_path="", request_prefix=""),
|
||||||
|
Params(app_root_path="/api", asgi_root_path="", request_prefix="/api"),
|
||||||
|
Params(app_root_path="/api", asgi_root_path="", request_prefix=""),
|
||||||
|
Params(app_root_path="", asgi_root_path="/api", request_prefix="/api"),
|
||||||
|
Params(app_root_path="", asgi_root_path="/api", request_prefix=""),
|
||||||
|
],
|
||||||
|
ids=[
|
||||||
|
"Without root_path, request without prefix",
|
||||||
|
"FastAPI(root_path=root_path), request with prefix",
|
||||||
|
"FastAPI(root_path=root_path), request without prefix",
|
||||||
|
"TestClient(root_path=root_path), request with prefix",
|
||||||
|
"TestClient(root_path=root_path), request without prefix",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def params(request: pytest.FixtureRequest):
|
||||||
|
return request.param
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture()
|
||||||
|
def client(params: Params, monkeypatch):
|
||||||
static_dir: Path = Path(os.getcwd()) / "static"
|
static_dir: Path = Path(os.getcwd()) / "static"
|
||||||
print(static_dir)
|
|
||||||
static_dir.mkdir(exist_ok=True)
|
static_dir.mkdir(exist_ok=True)
|
||||||
os.environ["ROOT_PATH"] = root_path
|
|
||||||
from docs_src.custom_docs_ui.tutorial002 import app
|
|
||||||
|
|
||||||
with TestClient(app) as client:
|
monkeypatch.setenv("ROOT_PATH", params["app_root_path"])
|
||||||
|
from docs_src.custom_docs_ui import tutorial002
|
||||||
|
importlib.reload(tutorial002)
|
||||||
|
app = tutorial002.app
|
||||||
|
|
||||||
|
with TestClient(app, root_path=params["asgi_root_path"]) as client:
|
||||||
yield client
|
yield client
|
||||||
|
|
||||||
os.environ.pop("ROOT_PATH", None)
|
|
||||||
static_dir.rmdir()
|
static_dir.rmdir()
|
||||||
|
|
||||||
|
|
||||||
def test_swagger_ui_html(client: TestClient):
|
def test_swagger_ui_html(client: TestClient, params: Params):
|
||||||
response = client.get("/docs")
|
request_prefix = params["request_prefix"]
|
||||||
|
root_path = params["app_root_path"] or params["asgi_root_path"]
|
||||||
|
|
||||||
|
response = client.get(f"{request_prefix}/docs")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert f"{root_path}/static/swagger-ui-bundle.js" in response.text
|
assert f"{root_path}/static/swagger-ui-bundle.js" in response.text
|
||||||
assert f"{root_path}/static/swagger-ui.css" in response.text
|
assert f"{root_path}/static/swagger-ui.css" in response.text
|
||||||
assert f"{root_path}/docs/oauth2-redirect" in response.text
|
assert f"{root_path}/docs/oauth2-redirect" in response.text
|
||||||
|
|
||||||
response = client.get(f"{root_path}/openapi.json")
|
response = client.get(f"{request_prefix}/openapi.json")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
def test_swagger_ui_oauth2_redirect_html(client: TestClient):
|
def test_swagger_ui_oauth2_redirect_html(client: TestClient, params: Params):
|
||||||
response = client.get("/docs/oauth2-redirect")
|
request_prefix = params["request_prefix"]
|
||||||
|
|
||||||
|
response = client.get(f"{request_prefix}/docs/oauth2-redirect")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert "window.opener.swaggerUIRedirectOauth2" in response.text
|
assert "window.opener.swaggerUIRedirectOauth2" in response.text
|
||||||
|
|
||||||
|
|
||||||
def test_redoc_html(client: TestClient):
|
def test_redoc_html(client: TestClient, params: Params):
|
||||||
response = client.get("/redoc")
|
request_prefix = params["request_prefix"]
|
||||||
|
root_path = params["app_root_path"] or params["asgi_root_path"]
|
||||||
|
|
||||||
|
response = client.get(f"{request_prefix}/redoc")
|
||||||
|
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert f"{root_path}/static/redoc.standalone.js" in response.text
|
assert f"{root_path}/static/redoc.standalone.js" in response.text
|
||||||
|
|
||||||
response = client.get(f"{root_path}/openapi.json")
|
response = client.get(f"{request_prefix}/openapi.json")
|
||||||
assert response.status_code == 200
|
assert response.status_code == 200
|
||||||
|
|
||||||
|
|
||||||
def test_api(client: TestClient):
|
def test_api(client: TestClient, params: Params):
|
||||||
response = client.get("/users/john")
|
request_prefix = params["request_prefix"]
|
||||||
|
|
||||||
|
response = client.get(f"{request_prefix}/users/john")
|
||||||
assert response.status_code == 200, response.text
|
assert response.status_code == 200, response.text
|
||||||
assert response.json()["message"] == "Hello john"
|
assert response.json()["message"] == "Hello john"
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue