Skip to content

Commit 11ed26e

Browse files
authored
Merge pull request psf#124 from eads/master
apply heuristics more aggresively
2 parents 4746b90 + 0fa7adc commit 11ed26e

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

cachecontrol/adapter.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ def build_response(self, request, response, from_cache=False):
5656
cached response
5757
"""
5858
if not from_cache and request.method == 'GET':
59+
# Check for any heuristics that might update headers
60+
# before trying to cache.
61+
if self.heuristic:
62+
response = self.heuristic.apply(response)
5963

6064
# apply any expiration heuristics
6165
if response.status == 304:
@@ -83,11 +87,6 @@ def build_response(self, request, response, from_cache=False):
8387
elif response.status == 301:
8488
self.controller.cache_response(request, response)
8589
else:
86-
# Check for any heuristics that might update headers
87-
# before trying to cache.
88-
if self.heuristic:
89-
response = self.heuristic.apply(response)
90-
9190
# Wrap the response file with a wrapper that will cache the
9291
# response when the stream has been consumed.
9392
response._fp = CallbackFileWrapper(

tests/conftest.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ def update_etag(self, env, start_response):
5959
start_response('200 OK', headers)
6060
return [pformat(env).encode("utf8")]
6161

62+
def conditional_get(self, env, start_response):
63+
return start_response('304 Not Modified', [])
64+
6265
def etag(self, env, start_response):
6366
headers = [
6467
('Etag', self.etag_string),

tests/test_expires_heuristics.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,26 @@ def test_no_header_change_means_no_warning_header(self, url):
3636
assert not self.heuristic.warning.called
3737

3838

39+
class TestHeuristicWith3xxResponse(object):
40+
def setup(self):
41+
42+
class DummyHeuristic(BaseHeuristic):
43+
def update_headers(self, resp):
44+
return {'x-dummy-header': 'foobar'}
45+
46+
self.sess = CacheControl(Session(), heuristic=DummyHeuristic())
47+
48+
def test_heuristic_applies_to_301(self, url):
49+
the_url = url + 'permanent_redirect'
50+
resp = self.sess.get(the_url)
51+
assert 'x-dummy-header' in resp.headers
52+
53+
def test_heuristic_applies_to_304(self, url):
54+
the_url = url + 'conditional_get'
55+
resp = self.sess.get(the_url)
56+
assert 'x-dummy-header' in resp.headers
57+
58+
3959
class TestUseExpiresHeuristic(object):
4060

4161
def test_expires_heuristic_arg(self):

0 commit comments

Comments
 (0)