Author: gwilson
Date: 2007-11-18 21:12:19 -0600 (Sun, 18 Nov 2007)
New Revision: 6696

Modified:
   django/trunk/django/utils/cache.py
   django/trunk/tests/regressiontests/cache/tests.py
Log:
Removed `vary_delim_re` in `django/utils/cache.py` in favor of existing 
`cc_delim_re` since the latter is more correct in parsing the header (allows 
whitespace before and after comma separators instead of just after).  As a 
bonus, tests added for `patch_vary_headers()`.


Modified: django/trunk/django/utils/cache.py
===================================================================
--- django/trunk/django/utils/cache.py  2007-11-19 03:01:20 UTC (rev 6695)
+++ django/trunk/django/utils/cache.py  2007-11-19 03:12:19 UTC (rev 6696)
@@ -70,8 +70,6 @@
     cc = ', '.join([dictvalue(el) for el in cc.items()])
     response['Cache-Control'] = cc
 
-vary_delim_re = re.compile(r',\s*')
-
 def patch_response_headers(response, cache_timeout=None):
     """
     Adds some useful headers to the given HttpResponse object:
@@ -111,7 +109,7 @@
     # computing an MD5 hash.
     vary = []
     if response.has_header('Vary'):
-        vary = vary_delim_re.split(response['Vary'])
+        vary = cc_delim_re.split(response['Vary'])
     oldheaders = dict([(el.lower(), 1) for el in vary])
     for newheader in newheaders:
         if not newheader.lower() in oldheaders:
@@ -169,7 +167,7 @@
                     key_prefix, iri_to_uri(request.path))
     if response.has_header('Vary'):
         headerlist = ['HTTP_'+header.upper().replace('-', '_')
-                      for header in vary_delim_re.split(response['Vary'])]
+                      for header in cc_delim_re.split(response['Vary'])]
         cache.set(cache_key, headerlist, cache_timeout)
         return _generate_cache_key(request, headerlist, key_prefix)
     else:

Modified: django/trunk/tests/regressiontests/cache/tests.py
===================================================================
--- django/trunk/tests/regressiontests/cache/tests.py   2007-11-19 03:01:20 UTC 
(rev 6695)
+++ django/trunk/tests/regressiontests/cache/tests.py   2007-11-19 03:12:19 UTC 
(rev 6696)
@@ -3,9 +3,12 @@
 # Unit tests for cache framework
 # Uses whatever cache backend is set in the test settings file.
 
-from django.core.cache import cache
 import time, unittest
 
+from django.core.cache import cache
+from django.utils.cache import patch_vary_headers
+from django.http import HttpResponse
+
 # functions/classes for complex data type tests
 def f():
     return 42
@@ -87,5 +90,30 @@
             cache.set(key, value)
             self.assertEqual(cache.get(key), value)
 
+
+class CacheUtils(unittest.TestCase):
+    """TestCase for django.utils.cache functions."""
+    
+    def test_patch_vary_headers(self):
+        headers = ( 
+            # Initial vary, new headers, resulting vary.
+            (None, ('Accept-Encoding',), 'Accept-Encoding'),
+            ('Accept-Encoding', ('accept-encoding',), 'Accept-Encoding'),
+            ('Accept-Encoding', ('ACCEPT-ENCODING',), 'Accept-Encoding'),
+            ('Cookie', ('Accept-Encoding',), 'Cookie, Accept-Encoding'),
+            ('Cookie, Accept-Encoding', ('Accept-Encoding',), 'Cookie, 
Accept-Encoding'),
+            ('Cookie, Accept-Encoding', ('Accept-Encoding', 'cookie'), 
'Cookie, Accept-Encoding'),
+            (None, ('Accept-Encoding', 'COOKIE'), 'Accept-Encoding, COOKIE'),
+            ('Cookie,     Accept-Encoding', ('Accept-Encoding', 'cookie'), 
'Cookie, Accept-Encoding'),
+            ('Cookie    ,     Accept-Encoding', ('Accept-Encoding', 'cookie'), 
'Cookie, Accept-Encoding'),
+        )
+        for initial_vary, newheaders, resulting_vary in headers:
+            response = HttpResponse()
+            if initial_vary is not None:
+                response['Vary'] = initial_vary
+            patch_vary_headers(response, newheaders)
+            self.assertEqual(response['Vary'], resulting_vary)
+
+
 if __name__ == '__main__':
     unittest.main()


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/django-updates?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to