mirror of https://github.com/encode/starlette.git
StaticFiles will check if directory exists upon instantiation (#195)
* Add check_dir parameter * Add test coverage for check_dir * Update documentation * linting * Remove extra space
This commit is contained in:
parent
58888bb53f
commit
150c7092e5
|
@ -1,7 +1,12 @@
|
|||
|
||||
Starlette also includes a `StaticFiles` class for serving a specific directory:
|
||||
Starlette also includes a `StaticFiles` class for serving files in a given directory:
|
||||
|
||||
* `StaticFiles(directory)` - Serve any files in the given `directory`.
|
||||
### StaticFiles
|
||||
|
||||
Signature: `StaticFiles(directory, check_dir=True)`
|
||||
|
||||
* `directory` - A string denoting the directory path
|
||||
* `check_dir` - Ensure that the directory exists upon instantiation. Defaults to `True`
|
||||
|
||||
You can combine this ASGI application with Starlette's routing to provide
|
||||
comprehensive static file serving.
|
||||
|
|
|
@ -8,7 +8,9 @@ from starlette.types import ASGIInstance, Receive, Scope, Send
|
|||
|
||||
|
||||
class StaticFiles:
|
||||
def __init__(self, *, directory: str) -> None:
|
||||
def __init__(self, *, directory: str, check_dir: bool = True) -> None:
|
||||
if check_dir and not os.path.isdir(directory):
|
||||
raise RuntimeError("Directory '%s' does not exist" % directory)
|
||||
self.directory = directory
|
||||
self.config_checked = False
|
||||
|
||||
|
|
|
@ -54,9 +54,16 @@ def test_staticfiles_with_missing_file_returns_404(tmpdir):
|
|||
assert response.text == "Not Found"
|
||||
|
||||
|
||||
def test_staticfiles_instantiated_with_missing_directory(tmpdir):
|
||||
with pytest.raises(RuntimeError) as exc:
|
||||
path = os.path.join(tmpdir, "no_such_directory")
|
||||
app = StaticFiles(directory=path)
|
||||
assert "does not exist" in str(exc)
|
||||
|
||||
|
||||
def test_staticfiles_configured_with_missing_directory(tmpdir):
|
||||
path = os.path.join(tmpdir, "no_such_directory")
|
||||
app = StaticFiles(directory=path)
|
||||
app = StaticFiles(directory=path, check_dir=False)
|
||||
client = TestClient(app)
|
||||
with pytest.raises(RuntimeError) as exc:
|
||||
client.get("/example.txt")
|
||||
|
@ -68,7 +75,7 @@ def test_staticfiles_configured_with_file_instead_of_directory(tmpdir):
|
|||
with open(path, "w") as file:
|
||||
file.write("<file content>")
|
||||
|
||||
app = StaticFiles(directory=path)
|
||||
app = StaticFiles(directory=path, check_dir=False)
|
||||
client = TestClient(app)
|
||||
with pytest.raises(RuntimeError) as exc:
|
||||
client.get("/example.txt")
|
||||
|
|
Loading…
Reference in New Issue