Given the request and response headers, return `True` if an HTTP
"Not Modified" response could be returned instead.
"""
- try:
- if_none_match = request_headers["if-none-match"]
+ if if_none_match := request_headers.get("if-none-match"):
+ # The "etag" header is added by FileResponse, so it's always present.
etag = response_headers["etag"]
- if etag in [tag.strip(" W/") for tag in if_none_match.split(",")]:
- return True
- except KeyError:
- pass
+ return etag in [tag.strip(" W/") for tag in if_none_match.split(",")]
try:
if_modified_since = parsedate(request_headers["if-modified-since"])
assert second_resp.content == b"<file content>"
+def test_staticfiles_200_with_etag_mismatch_and_timestamp_match(
+ tmpdir: Path, test_client_factory: TestClientFactory
+) -> None:
+ path = tmpdir / "example.txt"
+ path.write_text("<file content>", encoding="utf-8")
+
+ 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"]
+ # If `if-none-match` is present, `if-modified-since` is ignored.
+ 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"<file content>"
+
+
def test_staticfiles_304_with_last_modified_compare_last_req(
tmpdir: Path, test_client_factory: TestClientFactory
) -> None: