Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #15863 - SimpleCookies are not correctly serialized with the fi…

…le or database cache backends

Thanks to rakuco for the report and for the tests.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@17200 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit a935d436e9e11e1801ca5e9421a055e1f04eb125 1 parent 58276bd
Luke Plant authored December 11, 2011
11  django/http/__init__.py
@@ -587,6 +587,17 @@ def __delitem__(self, header):
587 587
     def __getitem__(self, header):
588 588
         return self._headers[header.lower()][1]
589 589
 
  590
+    def __getstate__(self):
  591
+        # SimpleCookie is not pickeable with pickle.HIGHEST_PROTOCOL, so we
  592
+        # serialise to a string instead
  593
+        state = self.__dict__.copy()
  594
+        state['cookies'] = str(state['cookies'])
  595
+        return state
  596
+
  597
+    def __setstate__(self, state):
  598
+        self.__dict__.update(state)
  599
+        self.cookies = SimpleCookie(self.cookies)
  600
+
590 601
     def has_header(self, header):
591 602
         """Case-insensitive check for a header."""
592 603
         return header.lower() in self._headers
40  tests/regressiontests/cache/tests.py
@@ -176,6 +176,17 @@ def test_decr_version(self):
176 176
 class BaseCacheTests(object):
177 177
     # A common set of tests to apply to all cache backends
178 178
 
  179
+    def _get_request_cache(self, path):
  180
+        request = HttpRequest()
  181
+        request.META = {
  182
+            'SERVER_NAME': 'testserver',
  183
+            'SERVER_PORT': 80,
  184
+        }
  185
+        request.path = request.path_info = path
  186
+        request._cache_update_cache = True
  187
+        request.method = 'GET'
  188
+        return request
  189
+
179 190
     def test_simple(self):
180 191
         # Simple cache set/get works
181 192
         self.cache.set("key", "value")
@@ -741,6 +752,35 @@ def test_custom_key_func(self):
741 752
         self.assertEqual(self.custom_key_cache2.get('answer2'), 42)
742 753
 
743 754
 
  755
+    def test_cache_write_unpickable_object(self):
  756
+        update_middleware = UpdateCacheMiddleware()
  757
+        update_middleware.cache = self.cache
  758
+
  759
+        fetch_middleware = FetchFromCacheMiddleware()
  760
+        fetch_middleware.cache = self.cache
  761
+
  762
+        request = self._get_request_cache('/cache/test')
  763
+        get_cache_data = FetchFromCacheMiddleware().process_request(request)
  764
+        self.assertEqual(get_cache_data, None)
  765
+
  766
+        response = HttpResponse()
  767
+        content = 'Testing cookie serialization.'
  768
+        response.content = content
  769
+        response.set_cookie('foo', 'bar')
  770
+
  771
+        update_middleware.process_response(request, response)
  772
+
  773
+        get_cache_data = fetch_middleware.process_request(request)
  774
+        self.assertNotEqual(get_cache_data, None)
  775
+        self.assertEqual(get_cache_data.content, content)
  776
+        self.assertEqual(get_cache_data.cookies, response.cookies)
  777
+
  778
+        update_middleware.process_response(request, get_cache_data)
  779
+        get_cache_data = fetch_middleware.process_request(request)
  780
+        self.assertNotEqual(get_cache_data, None)
  781
+        self.assertEqual(get_cache_data.content, content)
  782
+        self.assertEqual(get_cache_data.cookies, response.cookies)
  783
+
744 784
 def custom_key_func(key, key_prefix, version):
745 785
     "A customized cache key function"
746 786
     return 'CUSTOM-' + '-'.join([key_prefix, str(version), key])

0 notes on commit a935d43

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