Parameterize test to ensure it works with `root_path`

This commit is contained in:
Yurii Motov 2026-02-12 12:45:52 +01:00
parent cc07b41d65
commit 704d3adb36
1 changed files with 6 additions and 3 deletions

View File

@ -1,3 +1,5 @@
from pathlib import Path
import pytest
from fastapi import APIRouter, FastAPI
from fastapi.exceptions import FastAPIError
@ -5,18 +7,19 @@ from fastapi.staticfiles import StaticFiles
from fastapi.testclient import TestClient
def test_mount_static_files_to_apirouter(tmp_path):
@pytest.mark.parametrize("root_path", ["", "/v1"])
def test_mount_static_files_to_apirouter(tmp_path: Path, root_path: str):
static_asset = tmp_path / "index.html"
static_asset.write_text("Hello, World!")
router = APIRouter()
router.mount("/static", StaticFiles(directory=tmp_path), name="static")
app = FastAPI()
app = FastAPI(root_path=root_path or None)
app.include_router(router)
client = TestClient(app)
response = client.get("/static/index.html")
response = client.get(f"{root_path}/static/index.html")
assert response.status_code == 200
assert response.text == "Hello, World!"