Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions starlette/staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
14 changes: 14 additions & 0 deletions tests/test_staticfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"<file content>"

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("<file content>")

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"<file content>"

def test_staticfiles_304_with_last_modified_compare_last_req(
tmpdir: Path, test_client_factory: TestClientFactory
Expand Down
Loading