diff --git a/cachecontrol/heuristics.py b/cachecontrol/heuristics.py index d95e78a9..3d82d360 100644 --- a/cachecontrol/heuristics.py +++ b/cachecontrol/heuristics.py @@ -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: diff --git a/tests/test_expires_heuristics.py b/tests/test_expires_heuristics.py index 2a5fd23b..491a1400 100644 --- a/tests/test_expires_heuristics.py +++ b/tests/test_expires_heuristics.py @@ -9,6 +9,7 @@ from pprint import pprint from unittest.mock import Mock +import pytest from requests import Session, get from cachecontrol import CacheControl @@ -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)