summaryrefslogtreecommitdiff
path: root/tests/regressiontests/conditional_processing/models.py
diff options
context:
space:
mode:
authorMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-03-22 07:58:29 +0000
committerMalcolm Tredinnick <malcolm.tredinnick@gmail.com>2009-03-22 07:58:29 +0000
commitb203db6ec850fee9ad8f2e2c8873be986325572b (patch)
tree349736a64d97e83f5817d79e7e12ad84973cdc0a /tests/regressiontests/conditional_processing/models.py
parent5ac154e06568b9815e85b32f144ab4ee10190a61 (diff)
Fixed #5791 -- Added early-bailout support for views (ETags and Last-modified).
This provides support for views that can have their ETag and/or Last-modified values computed much more quickly than the view itself. Supports all HTTP verbs (not just GET). Documentation and tests need a little more fleshing out (I'm not happy with the documentation at the moment, since it's a bit backwards), but the functionality is correct. git-svn-id: http://code.djangoproject.com/svn/django/trunk@10114 bcc190cf-cafb-0310-a4f2-bffc1f526a37
Diffstat (limited to 'tests/regressiontests/conditional_processing/models.py')
-rw-r--r--tests/regressiontests/conditional_processing/models.py100
1 files changed, 100 insertions, 0 deletions
diff --git a/tests/regressiontests/conditional_processing/models.py b/tests/regressiontests/conditional_processing/models.py
new file mode 100644
index 0000000000..5c52acbd52
--- /dev/null
+++ b/tests/regressiontests/conditional_processing/models.py
@@ -0,0 +1,100 @@
+# -*- coding:utf-8 -*-
+from datetime import datetime, timedelta
+from calendar import timegm
+
+from django.test import TestCase
+from django.utils.http import parse_etags, quote_etag
+
+FULL_RESPONSE = 'Test conditional get response'
+LAST_MODIFIED = datetime(2007, 10, 21, 23, 21, 47)
+LAST_MODIFIED_STR = 'Sun, 21 Oct 2007 23:21:47 GMT'
+EXPIRED_LAST_MODIFIED_STR = 'Sat, 20 Oct 2007 23:21:47 GMT'
+ETAG = 'b4246ffc4f62314ca13147c9d4f76974'
+EXPIRED_ETAG = '7fae4cd4b0f81e7d2914700043aa8ed6'
+
+class ConditionalGet(TestCase):
+ def assertFullResponse(self, response, check_last_modified=True, check_etag=True):
+ self.assertEquals(response.status_code, 200)
+ self.assertEquals(response.content, FULL_RESPONSE)
+ if check_last_modified:
+ self.assertEquals(response['Last-Modified'], LAST_MODIFIED_STR)
+ if check_etag:
+ self.assertEquals(response['ETag'], '"%s"' % ETAG)
+
+ def assertNotModified(self, response):
+ self.assertEquals(response.status_code, 304)
+ self.assertEquals(response.content, '')
+
+ def testWithoutConditions(self):
+ response = self.client.get('/condition/')
+ self.assertFullResponse(response)
+
+ def testIfModifiedSince(self):
+ self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
+ response = self.client.get('/condition/')
+ self.assertNotModified(response)
+ self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
+ response = self.client.get('/condition/')
+ self.assertFullResponse(response)
+
+ def testIfNoneMatch(self):
+ self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
+ response = self.client.get('/condition/')
+ self.assertNotModified(response)
+ self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
+ response = self.client.get('/condition/')
+ self.assertFullResponse(response)
+
+ # Several etags in If-None-Match is a bit exotic but why not?
+ self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s", "%s"' % (ETAG, EXPIRED_ETAG)
+ response = self.client.get('/condition/')
+ self.assertNotModified(response)
+
+ def testBothHeaders(self):
+ self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
+ self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
+ response = self.client.get('/condition/')
+ self.assertNotModified(response)
+
+ self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
+ self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
+ response = self.client.get('/condition/')
+ self.assertFullResponse(response)
+
+ self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
+ self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
+ response = self.client.get('/condition/')
+ self.assertFullResponse(response)
+
+ def testSingleCondition1(self):
+ self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
+ response = self.client.get('/condition/last_modified/')
+ self.assertNotModified(response)
+ response = self.client.get('/condition/etag/')
+ self.assertFullResponse(response, check_last_modified=False)
+
+ def testSingleCondition2(self):
+ self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
+ response = self.client.get('/condition/etag/')
+ self.assertNotModified(response)
+ response = self.client.get('/condition/last_modified/')
+ self.assertFullResponse(response, check_etag=False)
+
+ def testSingleCondition3(self):
+ self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
+ response = self.client.get('/condition/last_modified/')
+ self.assertFullResponse(response, check_etag=False)
+
+ def testSingleCondition4(self):
+ self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
+ response = self.client.get('/condition/etag/')
+ self.assertFullResponse(response, check_last_modified=False)
+
+class ETagProcesing(TestCase):
+ def testParsing(self):
+ etags = parse_etags(r'"", "etag", "e\"t\"ag", "e\\tag", W/"weak"')
+ self.assertEquals(etags, ['', 'etag', 'e"t"ag', r'e\tag', 'weak'])
+
+ def testQuoting(self):
+ quoted_etag = quote_etag(r'e\t"ag')
+ self.assertEquals(quoted_etag, r'"e\\t\"ag"')