Ticket #15863: ticket_15863.diff

File ticket_15863.diff, 3.0 KB (added by lukeplant, 4 years ago)

Patch (incorporating tests)

  • django/http/__init__.py

    diff -r b33d4705456a django/http/__init__.py
    a b  
    587587    def __getitem__(self, header):
    588588        return self._headers[header.lower()][1]
    589589
     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
    590601    def has_header(self, header):
    591602        """Case-insensitive check for a header."""
    592603        return header.lower() in self._headers
  • tests/regressiontests/cache/tests.py

    diff -r b33d4705456a tests/regressiontests/cache/tests.py
    a b  
    176176class BaseCacheTests(object):
    177177    # A common set of tests to apply to all cache backends
    178178
     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
    179190    def test_simple(self):
    180191        # Simple cache set/get works
    181192        self.cache.set("key", "value")
     
    741752        self.assertEqual(self.custom_key_cache2.get('answer2'), 42)
    742753
    743754
     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
    744784def custom_key_func(key, key_prefix, version):
    745785    "A customized cache key function"
    746786    return 'CUSTOM-' + '-'.join([key_prefix, str(version), key])
Back to Top