| 1 |
import os |
| 2 |
from pathlib import Path |
| 3 |
|
| 4 |
import pytest |
| 5 |
from fastapi.testclient import TestClient |
| 6 |
from inline_snapshot import snapshot |
| 7 |
|
| 8 |
from tests.utils import workdir_lock |
| 9 |
|
| 10 |
|
| 11 |
@pytest.fixture(scope="module") |
| 12 |
def client(): |
| 13 |
static_dir: Path = Path(os.getcwd()) / "static" |
| 14 |
static_dir.mkdir(exist_ok=True) |
| 15 |
sample_file = static_dir / "sample.txt" |
| 16 |
sample_file.write_text("This is a sample static file.") |
| 17 |
from docs_src.static_files.tutorial001_py310 import app |
| 18 |
|
| 19 |
with TestClient(app) as client: |
| 20 |
yield client |
| 21 |
sample_file.unlink() |
| 22 |
static_dir.rmdir() |
| 23 |
|
| 24 |
|
| 25 |
@workdir_lock |
| 26 |
def test_static_files(client: TestClient): |
| 27 |
response = client.get("/static/sample.txt") |
| 28 |
assert response.status_code == 200, response.text |
| 29 |
assert response.text == "This is a sample static file." |
| 30 |
|
| 31 |
|
| 32 |
@workdir_lock |
| 33 |
def test_static_files_not_found(client: TestClient): |
| 34 |
response = client.get("/static/non_existent_file.txt") |
| 35 |
assert response.status_code == 404, response.text |
| 36 |
|
| 37 |
|
| 38 |
@workdir_lock |
| 39 |
def test_openapi_schema(client: TestClient): |
| 40 |
response = client.get("/openapi.json") |
| 41 |
assert response.status_code == 200, response.text |
| 42 |
assert response.json() == snapshot( |
| 43 |
{ |
| 44 |
"openapi": "3.1.0", |
| 45 |
"info": {"title": "FastAPI", "version": "0.1.0"}, |
| 46 |
"paths": {}, |
| 47 |
} |
| 48 |
) |