refactor: based on code review feedback

This commit is contained in:
Arjan Singh 2025-08-27 13:02:43 -07:00
parent 0e524c8c28
commit a7592defd2
No known key found for this signature in database
GPG Key ID: B1102A5F9699979D
2 changed files with 8 additions and 14 deletions

View File

@ -57,19 +57,15 @@ async def cache_control(request: web.Request, handler: Callable[[web.Request], A
if request.path.endswith('.js') or request.path.endswith('.css') or request.path.endswith('index.json'):
response.headers.setdefault('Cache-Control', 'no-cache')
elif request.path.lower().endswith(IMG_EXTENSIONS):
if response.status == 304:
# 304 Not Modified - don't set cache headers, inherit from original
pass
elif response.status == 404:
if response.status == 404:
response.headers.setdefault('Cache-Control', f"public, max-age={ONE_HOUR}")
elif 200 <= response.status < 300:
elif response.status in (200, 201, 202, 203, 204, 205, 206, 301, 308):
# Success responses and permanent redirects - cache for 1 day
response.headers.setdefault('Cache-Control', f"public, max-age={ONE_DAY}")
elif response.status == 301 or response.status == 308:
# Permanent redirects - cache for 1 day
response.headers.setdefault('Cache-Control', f"public, max-age={ONE_DAY}")
elif 300 <= response.status < 400:
# Temporary redirects (302, 303, 307) and other 3xx - no cache
elif response.status in (302, 303, 307):
# Temporary redirects - no cache
response.headers.setdefault('Cache-Control', 'no-cache')
# Note: 304 Not Modified falls through - no cache headers set
return response

View File

@ -7,10 +7,8 @@ from unittest.mock import patch
pytestmark = pytest.mark.asyncio # Apply asyncio mark to all tests
# Mock the problematic imports before importing server
with patch('app.frontend_management.FrontendManager'):
with patch('utils.install_util.get_missing_requirements_message'):
with patch('utils.install_util.requirements_path'):
from server import cache_control, ONE_HOUR, ONE_DAY, IMG_EXTENSIONS
with patch('app.frontend_management.FrontendManager'), patch('utils.install_util.get_missing_requirements_message'), patch('utils.install_util.requirements_path'):
from server import cache_control, ONE_HOUR, ONE_DAY, IMG_EXTENSIONS
class TestCacheControl: