From a7592defd21e5636b4afbc293972d459232cfbcf Mon Sep 17 00:00:00 2001 From: Arjan Singh Date: Wed, 27 Aug 2025 13:02:43 -0700 Subject: [PATCH] refactor: based on code review feedback --- server.py | 16 ++++++---------- tests-unit/server_test/test_cache_control.py | 6 ++---- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/server.py b/server.py index 1b567b07c..44ae24a85 100644 --- a/server.py +++ b/server.py @@ -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 diff --git a/tests-unit/server_test/test_cache_control.py b/tests-unit/server_test/test_cache_control.py index c5b9a15f2..2b8b15252 100644 --- a/tests-unit/server_test/test_cache_control.py +++ b/tests-unit/server_test/test_cache_control.py @@ -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: