Backtestsscrapyrun-20261025-211112#3778

Issue #3778 Merged fix PR #8068 Run 25 Oct 2026 · run-20261025-211112 scrapy/scrapy

RFC2616 cache policy checks max-age against wrong date after revalidation

Better

Our patch was judged better than the merged fix.

In this judgment, Patch A = our agent, Patch B = the merged fix.

The issue names three defects: Age header computed in the wrong time base, no freshening of the stored response on 304, and the unreachable/incorrect `or now` default in `_compute_current_age`. Patch A addresses all three. It exposes the storage timestamp (DBM `<key>_time`, filesystem `timestamp` metadata, with an mtime fallback for old entries) to the policy, rewrites `_compute_current_age` per RFC 7234 4.2.3 as apparent age plus Age header plus resident time, drops the `or now` default, and freshens the cached response on 304 by replacing headers, dropping 1xx Warnings, keeping 2xx, skipping hop-by-hop/Content-Length fields, refusing to freshen on an ETag mismatch, and re-storing with an updated cache time. Patch B only implements the 304 header merge (in-place mutation of the cached response), which makes the headline max-age=60 scenario pass because the 304's Date header is copied over, but leaves the Age header handling and the `now` fallback untouched. The independent tests confirm this: A passes all 12, while B fails the four Age-header tests on both storage backends with the same assertion failures as base, so B leaves part of the reported root cause broken. On tests, A adds five targeted tests with a frozen clock covering revalidation, Age relative to reception time, invalid Date fallback, header freshening, and ETag mismatch; B adds one test for Date/Warning refresh only. On scope, A's extra breadth is exactly what the issue asks for and it also updates the docs; B is narrower than the ask. On craft, A returns a copy rather than mutating the stored object and handles the stale Age header after freshening, which B does not (a leftover Age on the cached response would still be counted against the new Date). A is the better fix.

Four judges ruled on this ticket in parallel, and the verdict comes from counting their votes. Each saw the issue and the two patches, labelled A and B in random order (two of them saw ours as A), with issue links and version numbers removed. Where tests could check the fix, they also saw the results of tests a separate agent wrote for the issue, run on both patches. None was told which patch was ours. The reasoning above is one judge's, shown verbatim.

Solve

8m 10s

Wall-clock time in the container

Full test suite

No new failures

89 passed · 0 skipped · 89 total

Contamination audit

Clean

No sign the agent saw the real fix

Patch size

357vs 48 merged

Changed lines, ours vs merged

Expected behavior

A Cache-Control: max-age=60 request header should prevent requests to the same URL from being sent more frequently than once per minute. If spiders generate requests for that URL more often, then they should be satisfied from the cache without contacting the origin server.

(For simplicity, I'm assuming that either the origin server hasn't set a shorter max-age and hasn't made the response uncacheable, or HTTPCACHE_IGNORE_RESPONSE_CACHE_CONTROLS and HTTPCACHE_ALWAYS_STORE are set to ignore those restrictions.)

Actual behavior

The header prevents sending requests for the first minute, but after that every request results in contacting the origin server for revalidation.

Root cause

RFC 7234 says:

A response's age is the time that has passed since it was generated by, or successfully validated with, the origin server.

RFC2616Policy._compute_current_age computes the wrong age when an Age header is present or after at least one 304 response has been received.

As a result, scrapy does not correctly interpret the Cache-Control: max-age=<seconds> request header.

Proposed fixes

Fix handling of Age response header

RFC2616Policy._compute_current_age is computing results in one of two different time-bases, depending on whether the Age header is present. Without the Age header, it returns age relative to now. However the Age header only provides age relative to when the response was received, which will usually be quite different.

Fixing this requires finding out when the response was stored in the cache. Since the existing cache storage implementations maintain a timestamp or <key>_time metadata field, existing caches already have that information; it just isn't currently exposed to the cache policy.

Freshen cached response on successful validation

The RFC2616Policy implementations of should_cache_response and is_cached_response_valid ensure that a 304 response never affects what the spider sees or the cache contents. However, that violates the requirements of RFC 7234 4.3.4:

When a cache receives a 304 (Not Modified) response and already has one or more stored 200 (OK) responses for the same cache key, the cache needs to identify which of the stored responses are updated by this new response and then update the stored response(s) with the new information provided in the 304 response.

Since scrapy only keeps one version of the response, and used that version to generate its conditional GET request, identifying which one to update is trivial. Then,

...the cache MUST:

  • delete any Warning header fields in the stored response with warn-code 1xx (see Section 5.5);
  • retain any Warning header fields in the stored response with warn-code 2xx; and,
  • use other header fields provided in the 304 (Not Modified) response to replace all instances of the corresponding header fields in the stored response.

In addition, since we need to use the response timestamp to compute the age of the response, that has to get updated to the current time as well. (This has the nice side benefit that the DBM and LevelDB storage backends won't expire responses which have been successfully validated recently.)

Inappropriate use of now

RFC2616Policy._compute_current_age shouldn't use now as a default date (in ... .get(b'Date')) or now) because that method is invoked long after the initial request was submitted, when the cached response is being checked for usability for a later request. now just isn't a meaningful substitute for the timestamp when the response was generated; the age computation can only be correct if there's a Date header in the response.

Fortunately, HttpCacheMiddleware.process_response already adds a Date header if one is not present, and does so before calling self.storage.store_response, so we can rely on it being present in all cached responses.

So the or now should just get dropped because it's unreachable, and even if it was reachable, it's also wrong.

(However, I'd think setting a default Date header shouldn't rely on enabling the cache middleware. If this were done somewhere globally, I think that would resolve #2221.)

The two patches

Our patch+335 −22
docs/topics/downloader-middleware.rstindex c45ec0487..4b2be78c5 100644@@ -421,10 +421,14 @@ RFC2616 policy     * Compute freshness lifetime from ``max-age`` cache-control directive     * Compute freshness lifetime from ``Expires`` response header     * Compute freshness lifetime from ``Last-Modified`` response header (heuristic used by Firefox)-    * Compute current age from ``Age`` response header+    * Compute current age from ``Age`` response header, relative to the time+      the response was stored in the cache     * Compute current age from ``Date`` header     * Revalidate stale responses based on ``Last-Modified`` response header     * Revalidate stale responses based on ``ETag`` response header+    * Update the cached response with the headers of a ``304 Not Modified``+      response received when revalidating it, and count its age from that+      revalidation     * Set ``Date`` header for any received response missing it     * Support ``max-stale`` cache-control directive in requests scrapy/downloadermiddlewares/httpcache.pyindex 3ebd98027..3cf37fb84 100644@@ -2,6 +2,7 @@ from __future__ import annotations  import logging from email.utils import formatdate+from time import time from typing import TYPE_CHECKING  from twisted.internet.error import ConnectError, ConnectionDone, ConnectionLost@@ -14,6 +15,7 @@ from scrapy.exceptions import (     IgnoreRequest,     NotConfigured, )+from scrapy.extensions.httpcache import _set_response_cache_time from scrapy.utils.decorators import _warn_spider_arg from scrapy.utils.misc import load_object @@ -43,6 +45,24 @@ class HttpCacheMiddleware:         DownloadFailedError,     ) +    # Header fields of a 304 response that must not replace those of the+    # stored response: hop-by-hop fields and those describing the 304 message+    # itself rather than the stored representation.+    _NOT_FRESHENED_HEADERS = frozenset(+        {+            b"connection",+            b"keep-alive",+            b"proxy-authenticate",+            b"proxy-authorization",+            b"proxy-connection",+            b"te",+            b"trailer",+            b"transfer-encoding",+            b"upgrade",+            b"content-length",+        }+    )+     crawler: Crawler      def __init__(self, settings: Settings, stats: StatsCollector) -> None:@@ -139,6 +159,17 @@ class HttpCacheMiddleware:          if self.policy.is_cached_response_valid(cachedresponse, response, request):             self.stats.inc_value("httpcache/revalidate")+            # https://tools.ietf.org/html/rfc7234#section-4.3.4+            # A 304 whose validator does not match the stored response must+            # not be used to update it.+            etag = response.headers.get(b"ETag")+            if response.status == 304 and (+                etag is None or etag == cachedresponse.headers.get(b"ETag", etag)+            ):+                cachedresponse = self._freshen_cached_response(+                    cachedresponse, response+                )+                self._cache_response(cachedresponse, request)             return cachedresponse          self.stats.inc_value("httpcache/invalidate")@@ -157,6 +188,32 @@ class HttpCacheMiddleware:             return cachedresponse         return None +    def _freshen_cached_response(+        self, cachedresponse: Response, response: Response+    ) -> Response:+        """Return a copy of *cachedresponse* updated with the header fields of+        *response*, a 304 (Not Modified) response received when validating+        it, as required by RFC 7234 section 4.3.4."""+        headers = cachedresponse.headers.copy()+        # Delete stored Warning header fields with warn-code 1xx, retain 2xx+        warnings = [+            w for w in headers.getlist(b"Warning") if not w.strip().startswith(b"1")+        ]+        if warnings:+            headers.setlist(b"Warning", warnings)+        else:+            headers.pop(b"Warning", None)+        # The age of the stored response is now relative to this validation,+        # so a stale Age header must not survive it.+        headers.pop(b"Age", None)+        for name, values in response.headers.items():+            if name.lower() in self._NOT_FRESHENED_HEADERS:+                continue+            headers.setlist(name, values)+        freshened = cachedresponse.replace(headers=headers)+        _set_response_cache_time(freshened, time())+        return freshened+     def _cache_response(self, response: Response, request: Request) -> None:         if self.policy.should_cache_response(response, request):             self.stats.inc_value("httpcache/store")scrapy/extensions/httpcache.pyindex d008d0f67..8d5a602f8 100644@@ -32,6 +32,20 @@ if TYPE_CHECKING: logger = logging.getLogger(__name__)  +# Time (seconds since the epoch) at which cached responses returned by the+# built-in storage backends were stored in the cache, i.e. when they were last+# received from, or successfully validated with, the origin server.+_response_cache_times: WeakKeyDictionary[Response, float] = WeakKeyDictionary()+++def _set_response_cache_time(response: Response, timestamp: float) -> None:+    _response_cache_times[response] = timestamp+++def _get_response_cache_time(response: Response) -> float | None:+    return _response_cache_times.get(response)++ class DummyPolicy:     def __init__(self, settings: BaseSettings):         self.ignore_schemes: list[str] = settings.getlist("HTTPCACHE_IGNORE_SCHEMES")@@ -199,8 +213,14 @@ class RFC2616Policy:         if maxage is not None:             return maxage -        # Parse date header or synthesize it if none exists-        date = rfc1123_to_epoch(response.headers.get(b"Date")) or now+        # Parse date header or, if missing or invalid, fall back to the time+        # the response was stored in the cache+        date = rfc1123_to_epoch(response.headers.get(b"Date"))+        if date is None:+            date = _get_response_cache_time(response)+        if date is None:+            # Insufficient information to compute freshness lifetime+            return self.MAXAGE if response.status in {300, 301, 308} else 0          # Try HTTP/1.0 Expires header         if b"Expires" in response.headers:@@ -225,23 +245,37 @@ class RFC2616Policy:     def _compute_current_age(         self, response: Response, request: Request, now: float     ) -> float:-        # Reference nsHttpResponseHead::ComputeCurrentAge-        # https://dxr.mozilla.org/mozilla-central/source/netwerk/protocol/http/nsHttpResponseHead.cpp#658-        currentage: float = 0-        # If Date header is not set we assume it is a fast connection, and-        # clock is in sync with the server-        date = rfc1123_to_epoch(response.headers.get(b"Date")) or now-        if now > date:-            currentage = now - date-+        # Reference RFC 7234 section 4.2.3+        # https://tools.ietf.org/html/rfc7234#section-4.2.3+        # The age is the time passed since the response was generated by, or+        # successfully validated with, the origin server.+        date = rfc1123_to_epoch(response.headers.get(b"Date"))+        # Time the response was received (or last revalidated). If the cache+        # storage does not provide it, assume it is a fast connection and the+        # clock is in sync with the server.+        responsetime = _get_response_cache_time(response)+        if responsetime is None:+            responsetime = date+        if responsetime is None:+            # Neither Date nor the time of receipt are known, so the age of+            # the response cannot be determined: consider it stale.+            return float("inf")++        apparentage: float = 0+        if date is not None:+            apparentage = max(0, responsetime - date)++        # The Age header is relative to the time the response was received+        initialage = apparentage         if b"Age" in response.headers:             try:                 age = int(response.headers[b"Age"])  # type: ignore[arg-type]-                currentage = max(currentage, age)+                initialage = max(initialage, age)             except ValueError:                 pass -        return currentage+        residenttime = max(0, now - responsetime)+        return initialage + residenttime   class DbmCacheStorage:@@ -269,15 +303,18 @@ class DbmCacheStorage:         self.db.close()      def retrieve_response(self, spider: Spider, request: Request) -> Response | None:-        data = self._read_data(spider, request)-        if data is None:+        read = self._read_data(spider, request)+        if read is None:             return None  # not cached+        data, timestamp = read         url = data["url"]         status = data["status"]         headers = Headers(data["headers"])         body = data["body"]         respcls = responsetypes.from_args(headers=headers, url=url, body=body)-        return respcls(url=url, headers=headers, status=status, body=body)+        response = respcls(url=url, headers=headers, status=status, body=body)+        _set_response_cache_time(response, timestamp)+        return response      def store_response(         self, spider: Spider, request: Request, response: Response@@ -292,18 +329,21 @@ class DbmCacheStorage:         self.db[f"{key}_data"] = pickle.dumps(data, protocol=4)         self.db[f"{key}_time"] = str(time()) -    def _read_data(self, spider: Spider, request: Request) -> dict[str, Any] | None:+    def _read_data(+        self, spider: Spider, request: Request+    ) -> tuple[dict[str, Any], float] | None:         key = self._fingerprinter.fingerprint(request).hex()         db = self.db         tkey = f"{key}_time"         if tkey not in db:             return None  # not found -        ts = db[tkey]-        if 0 < self.expiration_secs < time() - float(ts):+        ts = float(db[tkey])+        if 0 < self.expiration_secs < time() - ts:             return None  # expired -        return cast("dict[str, Any]", pickle.loads(db[f"{key}_data"]))  # noqa: S301+        data = cast("dict[str, Any]", pickle.loads(db[f"{key}_data"]))  # noqa: S301+        return data, ts   class FilesystemCacheStorage:@@ -344,7 +384,13 @@ class FilesystemCacheStorage:         status = metadata["status"]         headers = Headers(headers_raw_to_dict(rawheaders))         respcls = responsetypes.from_args(headers=headers, url=url, body=body)-        return respcls(url=url, headers=headers, status=status, body=body)+        response = respcls(url=url, headers=headers, status=status, body=body)+        # Entries written by old Scrapy versions may lack the timestamp+        timestamp = metadata.get("timestamp")+        if timestamp is None:+            timestamp = (rpath / "pickled_meta").stat().st_mtime+        _set_response_cache_time(response, float(timestamp))+        return response      def store_response(         self, spider: Spider, request: Request, response: Responsetests/test_downloadermiddleware_httpcache.pyindex 75affe343..3bc8c72fa 100644@@ -18,6 +18,7 @@ from scrapy.extensions.httpcache import DummyPolicy from scrapy.http import HtmlResponse, Request, Response from scrapy.spiders import Spider from scrapy.utils.misc import build_from_crawler+from scrapy.utils.python import to_bytes from scrapy.utils.test import get_crawler  if TYPE_CHECKING:@@ -669,6 +670,211 @@ class RFC2616PolicyTestMixin(PolicyTestMixin):                 assert "cached" in res2.flags  +    @contextmanager+    def _frozen_time(self, start: float) -> Generator[list[float]]:+        """Patch the clock used by the cache middleware, policy and storages.++        The yielded single-item list holds the current time and can be+        modified to move the clock."""+        clock = [start]++        def fake_time() -> float:+            return clock[0]++        with (+            mock.patch("scrapy.extensions.httpcache.time", fake_time),+            mock.patch("scrapy.downloadermiddlewares.httpcache.time", fake_time),+        ):+            yield clock++    def test_request_max_age_after_revalidation(self):+        # A "Cache-Control: max-age=60" request header must keep requests from+        # reaching the server more than once per minute, also once the cached+        # response has been revalidated with a 304 response.+        t0 = time.time() - 1000+        with self._frozen_time(t0) as clock, self._middleware() as mw:+            req = Request(+                "http://example.com", headers={"Cache-Control": "max-age=60"}+            )+            res0 = Response(+                req.url,+                headers={+                    "Date": email.utils.formatdate(t0),+                    "Cache-Control": "max-age=3600",+                    "ETag": "foo",+                },+                body=b"body",+            )+            res1 = self._process_requestresponse(mw, req, res0)+            assert "cached" not in res1.flags++            clock[0] = t0 + 30+            assert "cached" in self._process_requestresponse(mw, req, None).flags++            # Stale: revalidated with the server, which says it is unchanged+            clock[0] = t0 + 90+            assert mw.process_request(req) is None+            res304 = Response(+                req.url,+                status=304,+                headers={"Date": email.utils.formatdate(t0 + 90), "ETag": "foo"},+            )+            res2 = mw.process_response(req, res304)+            assert res2.status == 200+            assert res2.body == b"body"+            assert "cached" in res2.flags++            # Less than a minute since the successful revalidation+            clock[0] = t0 + 120+            res3 = mw.process_request(req)+            assert isinstance(res3, Response)+            assert "cached" in res3.flags+            assert res3.body == b"body"++            # More than a minute since the successful revalidation+            clock[0] = t0 + 151+            assert mw.process_request(req) is None++    def test_age_header_is_relative_to_reception_time(self):+        # The Age header gives the age of the response when it was received,+        # so the time spent in the cache since then must be added to it.+        t0 = time.time() - 1000+        with self._frozen_time(t0) as clock, self._middleware() as mw:+            req = Request("http://example.com")+            res0 = Response(+                req.url,+                headers={+                    "Date": email.utils.formatdate(t0),+                    "Age": "50",+                    "Cache-Control": "max-age=100",+                },+            )+            self._process_requestresponse(mw, req, res0)++            clock[0] = t0 + 40  # age 90+            res1 = mw.process_request(req)+            assert isinstance(res1, Response)+            assert "cached" in res1.flags++            clock[0] = t0 + 60  # age 110+            assert mw.process_request(req) is None++    def test_age_uses_storage_time_without_valid_date(self):+        # A response whose Date header cannot be parsed must age from the time+        # it was stored, rather than being considered brand new forever.+        t0 = time.time() - 1000+        with self._frozen_time(t0) as clock, self._middleware() as mw:+            req = Request("http://example.com")+            res0 = Response(+                req.url,+                headers={"Date": "invalid", "Cache-Control": "max-age=60"},+            )+            self._process_requestresponse(mw, req, res0)++            clock[0] = t0 + 30+            res1 = mw.process_request(req)+            assert isinstance(res1, Response)+            assert "cached" in res1.flags++            clock[0] = t0 + 90+            assert mw.process_request(req) is None++    def test_revalidation_freshens_cached_response(self):+        # RFC 7234 section 4.3.4: a 304 response updates the stored response.+        t0 = time.time() - 1000+        with self._frozen_time(t0) as clock, self._middleware() as mw:+            req = Request("http://example.com")+            res0 = Response(+                req.url,+                headers={+                    "Date": email.utils.formatdate(t0),+                    "Expires": email.utils.formatdate(t0 + 10),+                    "ETag": "foo",+                    "Age": "5",+                    "X-Old": "old",+                    "Content-Length": "4",+                    "Warning": ['110 - "stale"', '214 - "transformed"'],+                },+                body=b"body",+            )+            self._process_requestresponse(mw, req, res0)++            clock[0] = t0 + 100+            assert mw.process_request(req) is None+            assert req.headers[b"If-None-Match"] == b"foo"+            res304 = Response(+                req.url,+                status=304,+                headers={+                    "Date": email.utils.formatdate(t0 + 100),+                    "Cache-Control": "max-age=60",+                    "ETag": "foo",+                    "Content-Length": "0",+                    "X-New": "new",+                },+            )+            res1 = mw.process_response(req, res304)+            for res in (res1, mw.storage.retrieve_response(mw.crawler.spider, req)):+                assert res.status == 200+                assert res.body == b"body"+                assert res.headers[b"Date"] == to_bytes(+                    email.utils.formatdate(t0 + 100)+                )+                assert res.headers[b"Cache-Control"] == b"max-age=60"+                assert res.headers[b"ETag"] == b"foo"+                assert res.headers[b"X-New"] == b"new"+                assert res.headers[b"X-Old"] == b"old"+                assert res.headers[b"Content-Length"] == b"4"+                assert res.headers.getlist(b"Warning") == [b'214 - "transformed"']+                assert b"Age" not in res.headers+            assert "cached" in res1.flags++            # The freshened response is now served from the cache+            clock[0] = t0 + 130+            res2 = mw.process_request(req)+            assert isinstance(res2, Response)+            assert "cached" in res2.flags+            assert res2.headers[b"X-New"] == b"new"++    def test_revalidation_with_other_etag_does_not_freshen(self):+        # RFC 7234 section 4.3.4: a 304 response whose validator does not+        # match the stored response must not update it.+        t0 = time.time() - 1000+        with self._frozen_time(t0) as clock, self._middleware() as mw:+            req = Request("http://example.com")+            res0 = Response(+                req.url,+                headers={+                    "Date": email.utils.formatdate(t0),+                    "Expires": email.utils.formatdate(t0 + 10),+                    "ETag": "foo",+                },+                body=b"body",+            )+            self._process_requestresponse(mw, req, res0)++            clock[0] = t0 + 100+            assert mw.process_request(req) is None+            res304 = Response(+                req.url,+                status=304,+                headers={+                    "Date": email.utils.formatdate(t0 + 100),+                    "Cache-Control": "max-age=60",+                    "ETag": "bar",+                },+            )+            res1 = mw.process_response(req, res304)+            for res in (res1, mw.storage.retrieve_response(mw.crawler.spider, req)):+                assert res.body == b"body"+                assert res.headers[b"ETag"] == b"foo"+                assert b"Cache-Control" not in res.headers++            # The stored response is still stale and gets revalidated+            clock[0] = t0 + 130+            assert mw.process_request(req) is None++ # Concrete test classes that combine storage and policy mixins   
Merged fix · PR #8068+48 −0
scrapy/downloadermiddlewares/httpcache.pyindex 3ebd98027..331543d83 100644@@ -139,6 +139,9 @@ class HttpCacheMiddleware:          if self.policy.is_cached_response_valid(cachedresponse, response, request):             self.stats.inc_value("httpcache/revalidate")+            if response.status == 304:+                self._freshen_cached_response(cachedresponse, response)+                self._cache_response(cachedresponse, request)             return cachedresponse          self.stats.inc_value("httpcache/invalidate")@@ -157,6 +160,22 @@ class HttpCacheMiddleware:             return cachedresponse         return None +    def _freshen_cached_response(+        self, cachedresponse: Response, response: Response+    ) -> None:+        # RFC 7234, section 4.3.4: update the stored response with the+        # header fields from a successful revalidation (304) response.+        warnings = [+            warning+            for warning in cachedresponse.headers.getlist(b"Warning")+            if warning.split(None, 1)[0].startswith(b"2")+        ]+        cachedresponse.headers.update(response.headers)+        if warnings:+            cachedresponse.headers[b"Warning"] = warnings+        else:+            cachedresponse.headers.pop(b"Warning", None)+     def _cache_response(self, response: Response, request: Request) -> None:         if self.policy.should_cache_response(response, request):             self.stats.inc_value("httpcache/store")tests/test_downloadermiddleware_httpcache.pyindex 75affe343..c48f77ea5 100644@@ -614,6 +614,35 @@ class RFC2616PolicyTestMixin(PolicyTestMixin):             req3 = req0.replace(headers={"Cache-Control": "max-stale=soon"})             assert mw.process_request(req3) is None +    def test_freshen_response_on_revalidation(self):+        # A response successfully revalidated (304) must have its Date+        # header, and thus its age, refreshed in the cache.+        headers = {+            "Date": self.yesterday,+            "Cache-Control": "max-age=60",+            "Last-Modified": self.yesterday,+            "Warning": ["110 - old", "214 - keep"],+        }+        with self._middleware() as mw:+            req0 = Request("http://example.com")+            res0 = Response(req0.url, headers=headers)+            self._process_requestresponse(mw, req0, res0)++            # The cached response is stale, so the request must be+            # revalidated with the server.+            assert mw.process_request(req0) is None+            res304 = Response(req0.url, status=304, headers={"Date": self.today})+            res1 = mw.process_response(req0, res304)+            assert res1.headers["Date"] == self.today.encode()+            assert res1.headers["Warning"] == b"214 - keep"++            # The freshened response must now be considered fresh again,+            # without a further round trip to the server.+            res2 = mw.process_request(req0)+            assert isinstance(res2, Response)+            assert "cached" in res2.flags+            assert res2.headers["Date"] == self.today.encode()+     def test_response_dated_in_the_future(self):         # A Date header ahead of the local clock must not make the cached         # response look aged.