summaryrefslogtreecommitdiff
path: root/parts/django/tests/regressiontests/conditional_processing
diff options
context:
space:
mode:
Diffstat (limited to 'parts/django/tests/regressiontests/conditional_processing')
-rw-r--r--parts/django/tests/regressiontests/conditional_processing/__init__.py1
-rw-r--r--parts/django/tests/regressiontests/conditional_processing/models.py128
-rw-r--r--parts/django/tests/regressiontests/conditional_processing/urls.py10
-rw-r--r--parts/django/tests/regressiontests/conditional_processing/views.py26
4 files changed, 165 insertions, 0 deletions
diff --git a/parts/django/tests/regressiontests/conditional_processing/__init__.py b/parts/django/tests/regressiontests/conditional_processing/__init__.py
new file mode 100644
index 0000000..380474e
--- /dev/null
+++ b/parts/django/tests/regressiontests/conditional_processing/__init__.py
@@ -0,0 +1 @@
+# -*- coding:utf-8 -*-
diff --git a/parts/django/tests/regressiontests/conditional_processing/models.py b/parts/django/tests/regressiontests/conditional_processing/models.py
new file mode 100644
index 0000000..b291aed
--- /dev/null
+++ b/parts/django/tests/regressiontests/conditional_processing/models.py
@@ -0,0 +1,128 @@
+# -*- 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 testIfMatch(self):
+ self.client.defaults['HTTP_IF_MATCH'] = '"%s"' % ETAG
+ response = self.client.put('/condition/etag/', {'data': ''})
+ self.assertEquals(response.status_code, 200)
+ self.client.defaults['HTTP_IF_MATCH'] = '"%s"' % EXPIRED_ETAG
+ response = self.client.put('/condition/etag/', {'data': ''})
+ self.assertEquals(response.status_code, 412)
+
+ 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)
+
+ def testSingleCondition5(self):
+ self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
+ response = self.client.get('/condition/last_modified2/')
+ self.assertNotModified(response)
+ response = self.client.get('/condition/etag2/')
+ self.assertFullResponse(response, check_last_modified=False)
+
+ def testSingleCondition6(self):
+ self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
+ response = self.client.get('/condition/etag2/')
+ self.assertNotModified(response)
+ response = self.client.get('/condition/last_modified2/')
+ self.assertFullResponse(response, check_etag=False)
+
+ def testInvalidETag(self):
+ self.client.defaults['HTTP_IF_NONE_MATCH'] = r'"\"'
+ 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"')
diff --git a/parts/django/tests/regressiontests/conditional_processing/urls.py b/parts/django/tests/regressiontests/conditional_processing/urls.py
new file mode 100644
index 0000000..4dbe11a
--- /dev/null
+++ b/parts/django/tests/regressiontests/conditional_processing/urls.py
@@ -0,0 +1,10 @@
+from django.conf.urls.defaults import *
+import views
+
+urlpatterns = patterns('',
+ ('^$', views.index),
+ ('^last_modified/$', views.last_modified_view1),
+ ('^last_modified2/$', views.last_modified_view2),
+ ('^etag/$', views.etag_view1),
+ ('^etag2/$', views.etag_view2),
+)
diff --git a/parts/django/tests/regressiontests/conditional_processing/views.py b/parts/django/tests/regressiontests/conditional_processing/views.py
new file mode 100644
index 0000000..df49281
--- /dev/null
+++ b/parts/django/tests/regressiontests/conditional_processing/views.py
@@ -0,0 +1,26 @@
+# -*- coding:utf-8 -*-
+from django.views.decorators.http import condition, etag, last_modified
+from django.http import HttpResponse
+
+from models import FULL_RESPONSE, LAST_MODIFIED, ETAG
+
+def index(request):
+ return HttpResponse(FULL_RESPONSE)
+index = condition(lambda r: ETAG, lambda r: LAST_MODIFIED)(index)
+
+def last_modified_view1(request):
+ return HttpResponse(FULL_RESPONSE)
+last_modified_view1 = condition(last_modified_func=lambda r: LAST_MODIFIED)(last_modified_view1)
+
+def last_modified_view2(request):
+ return HttpResponse(FULL_RESPONSE)
+last_modified_view2 = last_modified(lambda r: LAST_MODIFIED)(last_modified_view2)
+
+def etag_view1(request):
+ return HttpResponse(FULL_RESPONSE)
+etag_view1 = condition(etag_func=lambda r: ETAG)(etag_view1)
+
+def etag_view2(request):
+ return HttpResponse(FULL_RESPONSE)
+etag_view2 = etag(lambda r: ETAG)(etag_view2)
+