Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Removed `vary_delim_re` in `django/utils/cache.py` in favor of existi…

…ng `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()`.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@6696 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 34cc21983ce0124a94d01120431f35ac0653c60f 1 parent 7d8ac66
Gary Wilson Jr. authored November 19, 2007
6  django/utils/cache.py
@@ -70,8 +70,6 @@ def dictvalue(t):
70 70
     cc = ', '.join([dictvalue(el) for el in cc.items()])
71 71
     response['Cache-Control'] = cc
72 72
 
73  
-vary_delim_re = re.compile(r',\s*')
74  
-
75 73
 def patch_response_headers(response, cache_timeout=None):
76 74
     """
77 75
     Adds some useful headers to the given HttpResponse object:
@@ -111,7 +109,7 @@ def patch_vary_headers(response, newheaders):
111 109
     # computing an MD5 hash.
112 110
     vary = []
113 111
     if response.has_header('Vary'):
114  
-        vary = vary_delim_re.split(response['Vary'])
  112
+        vary = cc_delim_re.split(response['Vary'])
115 113
     oldheaders = dict([(el.lower(), 1) for el in vary])
116 114
     for newheader in newheaders:
117 115
         if not newheader.lower() in oldheaders:
@@ -169,7 +167,7 @@ def learn_cache_key(request, response, cache_timeout=None, key_prefix=None):
169 167
                     key_prefix, iri_to_uri(request.path))
170 168
     if response.has_header('Vary'):
171 169
         headerlist = ['HTTP_'+header.upper().replace('-', '_')
172  
-                      for header in vary_delim_re.split(response['Vary'])]
  170
+                      for header in cc_delim_re.split(response['Vary'])]
173 171
         cache.set(cache_key, headerlist, cache_timeout)
174 172
         return _generate_cache_key(request, headerlist, key_prefix)
175 173
     else:
30  tests/regressiontests/cache/tests.py
@@ -3,9 +3,12 @@
3 3
 # Unit tests for cache framework
4 4
 # Uses whatever cache backend is set in the test settings file.
5 5
 
6  
-from django.core.cache import cache
7 6
 import time, unittest
8 7
 
  8
+from django.core.cache import cache
  9
+from django.utils.cache import patch_vary_headers
  10
+from django.http import HttpResponse
  11
+
9 12
 # functions/classes for complex data type tests
10 13
 def f():
11 14
     return 42
@@ -87,5 +90,30 @@ def test_unicode(self):
87 90
             cache.set(key, value)
88 91
             self.assertEqual(cache.get(key), value)
89 92
 
  93
+
  94
+class CacheUtils(unittest.TestCase):
  95
+    """TestCase for django.utils.cache functions."""
  96
+    
  97
+    def test_patch_vary_headers(self):
  98
+        headers = ( 
  99
+            # Initial vary, new headers, resulting vary.
  100
+            (None, ('Accept-Encoding',), 'Accept-Encoding'),
  101
+            ('Accept-Encoding', ('accept-encoding',), 'Accept-Encoding'),
  102
+            ('Accept-Encoding', ('ACCEPT-ENCODING',), 'Accept-Encoding'),
  103
+            ('Cookie', ('Accept-Encoding',), 'Cookie, Accept-Encoding'),
  104
+            ('Cookie, Accept-Encoding', ('Accept-Encoding',), 'Cookie, Accept-Encoding'),
  105
+            ('Cookie, Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'),
  106
+            (None, ('Accept-Encoding', 'COOKIE'), 'Accept-Encoding, COOKIE'),
  107
+            ('Cookie,     Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'),
  108
+            ('Cookie    ,     Accept-Encoding', ('Accept-Encoding', 'cookie'), 'Cookie, Accept-Encoding'),
  109
+        )
  110
+        for initial_vary, newheaders, resulting_vary in headers:
  111
+            response = HttpResponse()
  112
+            if initial_vary is not None:
  113
+                response['Vary'] = initial_vary
  114
+            patch_vary_headers(response, newheaders)
  115
+            self.assertEqual(response['Vary'], resulting_vary)
  116
+
  117
+
90 118
 if __name__ == '__main__':
91 119
     unittest.main()

0 notes on commit 34cc219

Please sign in to comment.
Something went wrong with that request. Please try again.