From 3f03a2ca509b7e8961ced2f5f98bf162077bf03f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Gra=C3=B1a?= Date: Fri, 4 Jan 2013 04:20:45 -0200 Subject: [PATCH] requests with no-cache set must force revalidation of cached responses --- scrapy/contrib/downloadermiddleware/httpcache.py | 3 ++- scrapy/tests/test_downloadermiddleware_httpcache.py | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/scrapy/contrib/downloadermiddleware/httpcache.py b/scrapy/contrib/downloadermiddleware/httpcache.py index aa736708b..b3ac193b4 100644 --- a/scrapy/contrib/downloadermiddleware/httpcache.py +++ b/scrapy/contrib/downloadermiddleware/httpcache.py @@ -79,7 +79,8 @@ class RFC2616Policy(object): def is_cached_response_fresh(self, cachedresponse, request): cc = self._parse_cachecontrol(cachedresponse) - if 'no-cache' in cc: + ccreq = self._parse_cachecontrol(request) + if 'no-cache' in cc or 'no-cache' in ccreq: return False now = time() diff --git a/scrapy/tests/test_downloadermiddleware_httpcache.py b/scrapy/tests/test_downloadermiddleware_httpcache.py index 28b3026f0..00baf4748 100644 --- a/scrapy/tests/test_downloadermiddleware_httpcache.py +++ b/scrapy/tests/test_downloadermiddleware_httpcache.py @@ -241,15 +241,27 @@ class RFC2616MiddlewareTest(DefaultStorageTest): headers={'Expires': self.tomorrow}) req0 = Request('http://example.com') req1 = req0.replace(headers={'Cache-Control': 'no-store'}) + req2 = req0.replace(headers={'Cache-Control': 'no-cache'}) with self._middleware() as mw: + # response for a request with no-store must not be cached res1 = self._process_requestresponse(mw, req1, res0) self.assertEqualResponse(res1, res0) assert mw.storage.retrieve_response(self.spider, req1) is None + # Re-do request without no-store and expect it to be cached res2 = self._process_requestresponse(mw, req0, res0) assert 'cached' not in res2.flags res3 = mw.process_request(req0, self.spider) assert 'cached' in res3.flags self.assertEqualResponse(res2, res3) + # request with no-cache directive must not return cached response + # but it allows new response to be stored + res0b = res0.replace(body='foo') + res4 = self._process_requestresponse(mw, req2, res0b) + self.assertEqualResponse(res4, res0b) + assert 'cached' not in res4.flags + res5 = self._process_requestresponse(mw, req0, None) + self.assertEqualResponse(res5, res0b) + assert 'cached' in res5.flags def test_response_cacheability(self): responses = [