def test_should_store(self): cache = ResponseCache(1) # Check normal response (200) self.assertTrue( cache.should_store(self.factory.get('/'), HttpResponse())) # Check some non-200 response codes for code in (201, 404, 403, 500, 502, 503, 301, 302): self.assertFalse( cache.should_store(self.factory.get('/'), HttpResponse(status=code)))
def test_default_key_function_parts_omission(self): request = self.factory.get('/url10/?k10=v10') # Empty prefix self.assertEqual( ResponseCache(1).key_func(request), '#GET#http#testserver#/url10/?k10=v10') # Do not consider scheme self.assertEqual( ResponseCache(1, key_prefix='p', include_scheme=False).key_func(request), 'p#GET##testserver#/url10/?k10=v10') # Do not consider host self.assertEqual( ResponseCache(1, key_prefix='p', include_host=False).key_func(request), 'p#GET#http##/url10/?k10=v10')
def test_absent_hitmiss_header(self): cache = ResponseCache(0.3, cache='testcache', hitmiss_header=None) decorated_view = cache(randomView) request = self.random_get() rsp1 = decorated_view(request) rsp2 = decorated_view(request) self.assertFalse(rsp1.has_header('X-Cache')) self.assertFalse(rsp2.has_header('X-Cache'))
def test_caching_req_cookies(self): cache = ResponseCache(0.3, cache='testcache', cache_cookies=True) decorated_view = cache(randomView) # This request should be cached with any cookie set request = self.random_get() request.COOKIES['c1'] = 'v1' rsp1 = decorated_view(request) rsp2 = decorated_view(request) self.assertEqual(rsp1.content, rsp2.content)
def test_should_fetch(self): # test default behavious: GETs only cache = ResponseCache(1) self.assertTrue(cache.should_fetch(self.factory.get('/'))) self.assertFalse(cache.should_fetch(self.factory.head('/'))) # Allow HEAD too cache = ResponseCache(1, methods=('GET', 'HEAD')) self.assertTrue(cache.should_fetch(self.factory.head('/'))) # Check authenticated requests (shouldn't cache by default) request = self.factory.get('/') request.user = User.objects.create_user('u1', '*****@*****.**', 'u1') self.assertFalse(cache.should_fetch(request)) # This instance should ignore authenticated user self.assertTrue( ResponseCache(1, anonymous_only=False).should_fetch(request)) # Anonymous' responses should be cached request.user = AnonymousUser() self.assertTrue(cache.should_fetch(request))
def test_whitelisted_req_cookies(self): cache = ResponseCache(0.3, cache='testcache', excluded_cookies=('c1', )) decorated_view = cache(randomView) # This cookie does not prevent request from being cached request = self.random_get() request.COOKIES['c1'] = 'v1' rsp1 = decorated_view(request) rsp2 = decorated_view(request) self.assertEqual(rsp1.content, rsp2.content)
def test_not_caching_configured_rsp_hdr(self): decorated_view = ResponseCache(0.3, cache='testcache', nocache_rsp=('Hdr1', ))(randomView) # First request with Set-Cookie is not cached request = self.random_get() rsp1 = decorated_view(request, headers={'Hdr1': 'val1'}) rsp2 = decorated_view(request) self.assertNotEqual(rsp1.content, rsp2.content) self.assertTrue(rsp1.has_header('Hdr1')) self.assertFalse(rsp2.has_header('Hdr1'))
def test_custom_hitmiss_header_template_view(self): cache = ResponseCache(0.3, cache='testcache', hitmiss_header=('h', '+', '-')) decorated_view = cache(randomTemplateView) request = self.random_get() rsp1 = decorated_view(request) rsp2 = decorated_view(request) self.assertFalse(rsp1.has_header('X-Cache')) self.assertFalse(rsp2.has_header('X-Cache')) self.assertEqual(rsp1['h'], '-') self.assertEqual(rsp2['h'], '+')
def test_blacklisted_req_cookies(self): cache = ResponseCache(0.3, cache='testcache', cache_cookies=True, excluded_cookies=('c1', )) decorated_view = cache(randomView) # This cookie prevents this request from being cached, # though generally cookies are allowed request = self.random_get() request.COOKIES['c1'] = 'v1' rsp1 = decorated_view(request) rsp2 = decorated_view(request) self.assertNotEqual(rsp1.content, rsp2.content)
def test_not_caching_configured_req_hdr(self): decorated_view = ResponseCache(0.3, cache='testcache', nocache_req={'HTTP_HDR1': '.123[abc]'})(randomView) request = self.random_get() # Should match and pass not cached request.META['HTTP_HDR1'] = '0a123b' rsp1 = decorated_view(request) rsp2 = decorated_view(request) self.assertNotEqual(rsp1.content, rsp2.content) # Should not match and be cached request.META['HTTP_HDR1'] = 'other' rsp3 = decorated_view(request) rsp4 = decorated_view(request) self.assertEqual(rsp3.content, rsp4.content)
def test_default_key_function(self): cache = ResponseCache(1, key_prefix='p') # Plain test with all defaults self.assertEqual(cache.key_func(self.factory.get('/url1/?k1=v1')), 'p#GET#http#testserver#/url1/?k1=v1') # Different method self.assertEqual(cache.key_func(self.factory.head('/url2/?k2=v2')), 'p#HEAD#http#testserver#/url2/?k2=v2') # Try HTTPS (hacky) request = self.factory.get('/url3/?k3=v3') request.is_secure = lambda: True self.assertEqual(cache.key_func(request), 'p#GET#https#testserver:80#/url3/?k3=v3') # Try different Host: + normalisation request = self.factory.get('/url4/?k4=v4', HTTP_HOST='FooBar') self.assertEqual(cache.key_func(request), 'p#GET#http#foobar#/url4/?k4=v4')
def test_should_store_streaming(self): cache = ResponseCache(1) # StreamingHttpResponse is never cached self.assertFalse( cache.should_store(self.factory.get('/'), StreamingHttpResponse()))
def test_user_supplied_key_function(self): # Test dumb user supplied key function cache = ResponseCache(1, key_func=lambda r: "KeyValue") request = self.factory.get('/') self.assertEqual(cache.key_func(request), 'KeyValue')
def csrfView(request): log.info("call csrfView") t = Template("{{uuid}} {% csrf_token %}") context = RequestContext(request, {"uuid": uuid4()}) response = HttpResponse(t.render(context)) return response def csrfTemplateView(request): t = "myapp/csrf_template.html" return TemplateResponse(request, t, {"uuid": uuid4()}) rsp_cache = ResponseCache(0.3, cache='testcache') class ResponseCacheTest(TestCase): def setUp(self): self.factory = RequestFactory() caches['testcache'].clear() def random_get(self): return self.factory.get('/%s' % uuid4()) def test_default_key_function(self): cache = ResponseCache(1, key_prefix='p') # Plain test with all defaults self.assertEqual(cache.key_func(self.factory.get('/url1/?k1=v1')), 'p#GET#http#testserver#/url1/?k1=v1')