-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Document static file mount gotcha
- Loading branch information
Showing
3 changed files
with
45 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
from fastapi import FastAPI | ||
from fastapi.staticfiles import StaticFiles | ||
|
||
from fastapi_versionizer import Versionizer | ||
|
||
app = FastAPI() | ||
|
||
# This will not work! | ||
app.mount('/examples-not-working', StaticFiles(directory='examples'), name='examples') | ||
|
||
versions = Versionizer( | ||
app=app, | ||
).versionize() | ||
|
||
# Only static file mounts added *after* instantiating Versionizer will work | ||
app.mount('/examples', StaticFiles(directory='examples'), name='examples') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from fastapi.testclient import TestClient | ||
|
||
from unittest import TestCase | ||
from examples.with_static_file_mount import app | ||
|
||
from pathlib import Path | ||
|
||
|
||
class TestWithStaticFileMount(TestCase): | ||
def test_with_static_file_mount_example(self) -> None: | ||
test_client = TestClient(app) | ||
|
||
# Read example file from file system | ||
expected = Path('examples/with_static_file_mount.py').read_text() | ||
|
||
# Compare local file contents with the same retrieved via the static file mount | ||
self.assertEqual(expected, test_client.get('/examples/with_static_file_mount.py').text) | ||
|
||
# Check that a static mount before instantiating Versionizer will not work | ||
self.assertEquals('{"detail":"Not Found"}', | ||
test_client.get('/examples-not-working/with_static_file_mount.py').text) |