Fixed issue #12 - Cache-Control: only-if-cached incorrectly does request if item not in cache
diff --git a/Makefile b/Makefile
index 12ca5ce..205786c 100755
--- a/Makefile
+++ b/Makefile
@@ -1,3 +1,5 @@
+tests:
+ python httplib2test.py
release:
python2.4 setup.py sdist --formats=gztar,zip
doc:
diff --git a/httplib2/__init__.py b/httplib2/__init__.py
index 6c289e8..9f11e86 100755
--- a/httplib2/__init__.py
+++ b/httplib2/__init__.py
@@ -1088,7 +1088,13 @@
self.cache.delete(cachekey)
content = new_content
else:
- (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
+ cc = _parse_cache_control(headers)
+ if cc.has_key('only-if-cached'):
+ info['status'] = '504'
+ response = Response(info)
+ content = ""
+ else:
+ (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
except Exception, e:
if self.force_exception_to_status_code:
if isinstance(e, HttpLib2ErrorWithResponse):
diff --git a/httplib2test.py b/httplib2test.py
index f882b17..a0b4dc5 100755
--- a/httplib2test.py
+++ b/httplib2test.py
@@ -205,13 +205,27 @@
self.assertEqual(response.status, 200)
self.assertEqual(response.previous, None)
+ def testGetOnlyIfCachedCacheHit(self):
+ # Test that can do a GET with cache and 'only-if-cached'
+ uri = urlparse.urljoin(base, "304/test_etag.txt")
+ (response, content) = self.http.request(uri, "GET")
+ (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
+ self.assertEqual(response.fromcache, True)
+ self.assertEqual(response.status, 200)
+
+ def testGetOnlyIfCachedCacheMissCache(self):
+ # Test that can do a GET with cache and 'only-if-cached'
+ uri = urlparse.urljoin(base, "304/test_etag.txt")
+ (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
+ self.assertEqual(response.fromcache, False)
+ self.assertEqual(response.status, 504)
+
def testGetOnlyIfCachedCacheMiss(self):
# Test that can do a GET with no cache with 'only-if-cached'
- http = httplib2.Http()
uri = urlparse.urljoin(base, "304/test_etag.txt")
- (response, content) = http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
+ (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
self.assertEqual(response.fromcache, False)
- self.assertEqual(response.status, 200)
+ self.assertEqual(response.status, 504)
def testGetOnlyIfCachedNoCacheAtAll(self):
# Test that can do a GET with no cache with 'only-if-cached'
@@ -222,7 +236,7 @@
uri = urlparse.urljoin(base, "304/test_etag.txt")
(response, content) = http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
self.assertEqual(response.fromcache, False)
- self.assertEqual(response.status, 200)
+ self.assertEqual(response.status, 504)
def testUserAgent(self):
# Test that we provide a default user-agent
diff --git a/python3/httplib2/__init__.py b/python3/httplib2/__init__.py
index d660796..389c3fe 100755
--- a/python3/httplib2/__init__.py
+++ b/python3/httplib2/__init__.py
@@ -1074,7 +1074,13 @@
self.cache.delete(cachekey)
content = new_content
else:
- (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
+ cc = _parse_cache_control(headers)
+ if 'only-if-cached'in cc:
+ info['status'] = '504'
+ response = Response(info)
+ content = b""
+ else:
+ (response, content) = self._request(conn, authority, uri, request_uri, method, body, headers, redirections, cachekey)
except Exception as e:
if self.force_exception_to_status_code:
if isinstance(e, HttpLib2ErrorWithResponse):
diff --git a/python3/httplib2test.py b/python3/httplib2test.py
index 5b91164..6db6756 100755
--- a/python3/httplib2test.py
+++ b/python3/httplib2test.py
@@ -203,13 +203,27 @@
self.assertEqual(response.status, 200)
self.assertEqual(response.previous, None)
+ def testGetOnlyIfCachedCacheHit(self):
+ # Test that can do a GET with cache and 'only-if-cached'
+ uri = urllib.parse.urljoin(base, "304/test_etag.txt")
+ (response, content) = self.http.request(uri, "GET")
+ (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
+ self.assertEqual(response.fromcache, True)
+ self.assertEqual(response.status, 200)
+
+ def testGetOnlyIfCachedCacheMissCache(self):
+ # Test that can do a GET with cache and 'only-if-cached'
+ uri = urllib.parse.urljoin(base, "304/test_etag.txt")
+ (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
+ self.assertEqual(response.fromcache, False)
+ self.assertEqual(response.status, 504)
+
def testGetOnlyIfCachedCacheMiss(self):
# Test that can do a GET with no cache with 'only-if-cached'
- http = httplib2.Http()
uri = urllib.parse.urljoin(base, "304/test_etag.txt")
- (response, content) = http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
+ (response, content) = self.http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
self.assertEqual(response.fromcache, False)
- self.assertEqual(response.status, 200)
+ self.assertEqual(response.status, 504)
def testGetOnlyIfCachedNoCacheAtAll(self):
# Test that can do a GET with no cache with 'only-if-cached'
@@ -220,7 +234,7 @@
uri = urllib.parse.urljoin(base, "304/test_etag.txt")
(response, content) = http.request(uri, "GET", headers={'cache-control': 'only-if-cached'})
self.assertEqual(response.fromcache, False)
- self.assertEqual(response.status, 200)
+ self.assertEqual(response.status, 504)
def testUserAgent(self):
# Test that we provide a default user-agent