Merge branch 'request-no-cache' into 'v3'
Request no cache See merge request openflexure/openflexure-microscope-server!361
This commit is contained in:
commit
bfdbbb9ad2
2 changed files with 73 additions and 2 deletions
|
|
@ -9,6 +9,12 @@ from fastapi import FastAPI
|
||||||
|
|
||||||
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
|
||||||
|
|
||||||
|
NO_CACHE_HEADERS = {
|
||||||
|
"Cache-Control": "no-store, no-cache, must-revalidate, max-age=0",
|
||||||
|
"Pragma": "no-cache",
|
||||||
|
"Expires": "0",
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
def add_static_file(app: FastAPI, fname: str, folder: str) -> None:
|
def add_static_file(app: FastAPI, fname: str, folder: str) -> None:
|
||||||
"""Add a single file to the root of the FastAPI app.
|
"""Add a single file to the root of the FastAPI app.
|
||||||
|
|
@ -20,9 +26,12 @@ def add_static_file(app: FastAPI, fname: str, folder: str) -> None:
|
||||||
fname: the name of the file to add
|
fname: the name of the file to add
|
||||||
folder: the containing folder of the file
|
folder: the containing folder of the file
|
||||||
"""
|
"""
|
||||||
p = os.path.join(folder, fname)
|
file_path = os.path.join(folder, fname)
|
||||||
|
# Cache font files, but not other static files.
|
||||||
|
headers = {} if fname.endswith(".woff2") else NO_CACHE_HEADERS
|
||||||
|
|
||||||
app.get(f"/{fname}", response_class=FileResponse, include_in_schema=False)(
|
app.get(f"/{fname}", response_class=FileResponse, include_in_schema=False)(
|
||||||
lambda: FileResponse(p)
|
lambda: FileResponse(file_path, headers=headers)
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
62
tests/test_serve_static_files.py
Normal file
62
tests/test_serve_static_files.py
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
"""Test the code that mounts static files to the server, without creating a server."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
from starlette.responses import FileResponse
|
||||||
|
from openflexure_microscope_server.server import serve_static_files
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.fixture
|
||||||
|
def mock_app(mocker):
|
||||||
|
"""Return a mock FastAPI app.
|
||||||
|
|
||||||
|
This is just a mock, where the get method returns a mock.
|
||||||
|
"""
|
||||||
|
wrapper = mocker.Mock()
|
||||||
|
app = mocker.Mock()
|
||||||
|
app.get.return_value = wrapper
|
||||||
|
return app
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
"filename, allow_cache",
|
||||||
|
[
|
||||||
|
["foo", False],
|
||||||
|
["woff2.foo", False],
|
||||||
|
["strange_file_ending_in_woff2", False],
|
||||||
|
["fontfile.woff2", True],
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_add_static_file(filename, allow_cache, mock_app):
|
||||||
|
"""Test running add_static_file with both font and non-font files.
|
||||||
|
|
||||||
|
This should creates a wrapped function that returns a FileResponse, this
|
||||||
|
should be have the path on disk and the no cache headers if not a woff2 font.
|
||||||
|
"""
|
||||||
|
wrapper = mock_app.get.return_value
|
||||||
|
serve_static_files.add_static_file(mock_app, fname=filename, folder="bar")
|
||||||
|
|
||||||
|
# It is called once
|
||||||
|
assert mock_app.get.call_count == 1
|
||||||
|
# Check args for the get decorator
|
||||||
|
assert len(mock_app.get.call_args.args) == 1
|
||||||
|
assert mock_app.get.call_args.args[0] == "/" + filename
|
||||||
|
assert len(mock_app.get.call_args.kwargs) == 2
|
||||||
|
assert mock_app.get.call_args.kwargs["response_class"] == FileResponse
|
||||||
|
assert not mock_app.get.call_args.kwargs["include_in_schema"]
|
||||||
|
|
||||||
|
# Checked wrapper and the the returned wrapped function
|
||||||
|
assert wrapper.call_count == 1
|
||||||
|
wrapped = wrapper.call_args.args[0]
|
||||||
|
# the wrapped function should return the file response
|
||||||
|
response = wrapped()
|
||||||
|
assert isinstance(response, FileResponse)
|
||||||
|
assert response.path == "bar/" + filename
|
||||||
|
# The file response headers always have some standard data, and if the file is
|
||||||
|
# allowed to be cached it should also have the no_cache data
|
||||||
|
for key, value in serve_static_files.NO_CACHE_HEADERS.items():
|
||||||
|
if allow_cache:
|
||||||
|
assert key not in response.headers
|
||||||
|
else:
|
||||||
|
# Check headers that refuse caching are present
|
||||||
|
assert key in response.headers
|
||||||
|
assert response.headers[key] == value
|
||||||
Loading…
Add table
Add a link
Reference in a new issue