From 5544b2b788812e1245e07d44c1508f332e31a3da Mon Sep 17 00:00:00 2001 From: grindhold Date: Fri, 31 Oct 2025 11:09:52 +0100 Subject: [PATCH] rfc-conformity for cache validation --- starlette/staticfiles.py | 2 ++ tests/test_staticfiles.py | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/starlette/staticfiles.py b/starlette/staticfiles.py index 7fba9aa95..ff77a93c2 100644 --- a/starlette/staticfiles.py +++ b/starlette/staticfiles.py @@ -206,6 +206,8 @@ def is_not_modified(self, response_headers: Headers, request_headers: Headers) - etag = response_headers["etag"] if etag in [tag.strip(" W/") for tag in if_none_match.split(",")]: return True + else: + return False except KeyError: pass diff --git a/tests/test_staticfiles.py b/tests/test_staticfiles.py index dfeb09ebd..ed9822c27 100644 --- a/tests/test_staticfiles.py +++ b/tests/test_staticfiles.py @@ -230,6 +230,20 @@ def test_staticfiles_200_with_etag_mismatch(tmpdir: Path, test_client_factory: T assert second_resp.status_code == 200 assert second_resp.content == b"" +def test_staticfiles_200_with_etag_mismatch_and_timestamp_match(tmpdir: Path, test_client_factory: TestClientFactory) -> None: + path = os.path.join(tmpdir, "example.txt") + with open(path, "w") as file: + file.write("") + + app = StaticFiles(directory=tmpdir) + client = test_client_factory(app) + first_resp = client.get("/example.txt") + assert first_resp.status_code == 200 + assert first_resp.headers["etag"] != '"123"' + last_modified = first_resp.headers["last-modified"] + second_resp = client.get("/example.txt", headers={"if-none-match": '"123"', "if-modified-since": last_modified}) + assert second_resp.status_code == 200 + assert second_resp.content == b"" def test_staticfiles_304_with_last_modified_compare_last_req( tmpdir: Path, test_client_factory: TestClientFactory