diff options
| author | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2009-03-22 07:58:29 +0000 |
|---|---|---|
| committer | Malcolm Tredinnick <malcolm.tredinnick@gmail.com> | 2009-03-22 07:58:29 +0000 |
| commit | b203db6ec850fee9ad8f2e2c8873be986325572b (patch) | |
| tree | 349736a64d97e83f5817d79e7e12ad84973cdc0a /tests/regressiontests/conditional_processing | |
| parent | 5ac154e06568b9815e85b32f144ab4ee10190a61 (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')
4 files changed, 126 insertions, 0 deletions
diff --git a/tests/regressiontests/conditional_processing/__init__.py b/tests/regressiontests/conditional_processing/__init__.py new file mode 100644 index 0000000000..380474e035 --- /dev/null +++ b/tests/regressiontests/conditional_processing/__init__.py @@ -0,0 +1 @@ +# -*- coding:utf-8 -*- 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"') diff --git a/tests/regressiontests/conditional_processing/urls.py b/tests/regressiontests/conditional_processing/urls.py new file mode 100644 index 0000000000..938a4e4833 --- /dev/null +++ b/tests/regressiontests/conditional_processing/urls.py @@ -0,0 +1,8 @@ +from django.conf.urls.defaults import * +import views + +urlpatterns = patterns('', + ('^$', views.index), + ('^last_modified/$', views.last_modified), + ('^etag/$', views.etag), +) diff --git a/tests/regressiontests/conditional_processing/views.py b/tests/regressiontests/conditional_processing/views.py new file mode 100644 index 0000000000..c88236e21b --- /dev/null +++ b/tests/regressiontests/conditional_processing/views.py @@ -0,0 +1,17 @@ +# -*- coding:utf-8 -*- +from django.views.decorators.http import condition +from django.http import HttpResponse + +from models import FULL_RESPONSE, LAST_MODIFIED, ETAG + +@condition(lambda r: ETAG, lambda r: LAST_MODIFIED) +def index(request): + return HttpResponse(FULL_RESPONSE) + +@condition(last_modified_func=lambda r: LAST_MODIFIED) +def last_modified(request): + return HttpResponse(FULL_RESPONSE) + +@condition(etag_func=lambda r: ETAG) +def etag(request): + return HttpResponse(FULL_RESPONSE) |
