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
3 changes: 2 additions & 1 deletion cachecontrol/heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,8 @@ def update_headers(self, resp: HTTPResponse) -> dict[str, str]:
return {}

time_tuple = parsedate_tz(headers["date"])
assert time_tuple is not None
if time_tuple is None:
return {}
date = calendar.timegm(time_tuple[:6])
last_modified = parsedate(headers["last-modified"])
if last_modified is None:
Expand Down
9 changes: 9 additions & 0 deletions tests/test_expires_heuristics.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pprint import pprint
from unittest.mock import Mock

import pytest
from requests import Session, get

from cachecontrol import CacheControl
Expand Down Expand Up @@ -153,6 +154,14 @@ def test_expires_is_not_replaced_when_present(self):
resp = DummyResponse(200, {"Expires": self.day_ahead})
assert self.heuristic.update_headers(resp) == {}

@pytest.mark.parametrize("date", ["", "not-a-date"])
def test_invalid_date_does_not_infer_expiry(self, date):
headers = {"Date": date, "Last-Modified": self.week_ago}
resp = DummyResponse(200, headers)
assert self.heuristic.update_headers(resp) == {}
assert self.heuristic.apply(resp) is resp
assert dict(resp.headers) == headers

def test_last_modified_is_used(self):
resp = DummyResponse(200, {"Date": self.now, "Last-Modified": self.week_ago})
modified = self.heuristic.update_headers(resp)
Expand Down