2009-03-22 15:58:29 +08:00
|
|
|
# -*- coding:utf-8 -*-
|
2011-03-01 22:28:06 +08:00
|
|
|
from datetime import datetime
|
2009-03-22 15:58:29 +08:00
|
|
|
|
|
|
|
from django.test import TestCase
|
2011-03-01 22:28:06 +08:00
|
|
|
from django.utils import unittest
|
|
|
|
from django.utils.http import parse_etags, quote_etag, parse_http_date
|
2009-03-22 15:58:29 +08:00
|
|
|
|
2011-10-14 02:51:33 +08:00
|
|
|
|
2009-03-22 15:58:29 +08:00
|
|
|
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'
|
2011-03-01 22:28:06 +08:00
|
|
|
LAST_MODIFIED_NEWER_STR = 'Mon, 18 Oct 2010 16:56:23 GMT'
|
|
|
|
LAST_MODIFIED_INVALID_STR = 'Mon, 32 Oct 2010 16:56:23 GMT'
|
2009-03-22 15:58:29 +08:00
|
|
|
EXPIRED_LAST_MODIFIED_STR = 'Sat, 20 Oct 2007 23:21:47 GMT'
|
|
|
|
ETAG = 'b4246ffc4f62314ca13147c9d4f76974'
|
|
|
|
EXPIRED_ETAG = '7fae4cd4b0f81e7d2914700043aa8ed6'
|
|
|
|
|
|
|
|
class ConditionalGet(TestCase):
|
2011-04-02 21:27:17 +08:00
|
|
|
urls = 'regressiontests.conditional_processing.urls'
|
|
|
|
|
2009-03-22 15:58:29 +08:00
|
|
|
def assertFullResponse(self, response, check_last_modified=True, check_etag=True):
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.content, FULL_RESPONSE)
|
2009-03-22 15:58:29 +08:00
|
|
|
if check_last_modified:
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response['Last-Modified'], LAST_MODIFIED_STR)
|
2009-03-22 15:58:29 +08:00
|
|
|
if check_etag:
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response['ETag'], '"%s"' % ETAG)
|
2009-03-22 15:58:29 +08:00
|
|
|
|
|
|
|
def assertNotModified(self, response):
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 304)
|
|
|
|
self.assertEqual(response.content, '')
|
2009-03-22 15:58:29 +08:00
|
|
|
|
|
|
|
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)
|
2011-03-01 22:28:06 +08:00
|
|
|
self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_NEWER_STR
|
|
|
|
response = self.client.get('/condition/')
|
|
|
|
self.assertNotModified(response)
|
|
|
|
self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_INVALID_STR
|
|
|
|
response = self.client.get('/condition/')
|
|
|
|
self.assertFullResponse(response)
|
2009-03-22 15:58:29 +08:00
|
|
|
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)
|
|
|
|
|
2009-03-23 07:16:03 +08:00
|
|
|
def testIfMatch(self):
|
|
|
|
self.client.defaults['HTTP_IF_MATCH'] = '"%s"' % ETAG
|
|
|
|
response = self.client.put('/condition/etag/', {'data': ''})
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2009-03-23 07:16:03 +08:00
|
|
|
self.client.defaults['HTTP_IF_MATCH'] = '"%s"' % EXPIRED_ETAG
|
|
|
|
response = self.client.put('/condition/etag/', {'data': ''})
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(response.status_code, 412)
|
2009-03-23 07:16:03 +08:00
|
|
|
|
2009-03-22 15:58:29 +08:00
|
|
|
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)
|
|
|
|
|
2009-03-24 11:01:46 +08:00
|
|
|
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)
|
|
|
|
|
2009-04-03 14:59:34 +08:00
|
|
|
def testInvalidETag(self):
|
|
|
|
self.client.defaults['HTTP_IF_NONE_MATCH'] = r'"\"'
|
|
|
|
response = self.client.get('/condition/etag/')
|
|
|
|
self.assertFullResponse(response, check_last_modified=False)
|
|
|
|
|
2009-03-24 11:01:46 +08:00
|
|
|
|
2011-03-01 22:28:06 +08:00
|
|
|
class ETagProcessing(unittest.TestCase):
|
2009-03-22 15:58:29 +08:00
|
|
|
def testParsing(self):
|
|
|
|
etags = parse_etags(r'"", "etag", "e\"t\"ag", "e\\tag", W/"weak"')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(etags, ['', 'etag', 'e"t"ag', r'e\tag', 'weak'])
|
2009-03-22 15:58:29 +08:00
|
|
|
|
|
|
|
def testQuoting(self):
|
|
|
|
quoted_etag = quote_etag(r'e\t"ag')
|
2011-03-03 23:04:39 +08:00
|
|
|
self.assertEqual(quoted_etag, r'"e\\t\"ag"')
|
2011-03-01 22:28:06 +08:00
|
|
|
|
|
|
|
|
|
|
|
class HttpDateProcessing(unittest.TestCase):
|
|
|
|
def testParsingRfc1123(self):
|
|
|
|
parsed = parse_http_date('Sun, 06 Nov 1994 08:49:37 GMT')
|
|
|
|
self.assertEqual(datetime.utcfromtimestamp(parsed),
|
|
|
|
datetime(1994, 11, 06, 8, 49, 37))
|
|
|
|
|
|
|
|
def testParsingRfc850(self):
|
|
|
|
parsed = parse_http_date('Sunday, 06-Nov-94 08:49:37 GMT')
|
|
|
|
self.assertEqual(datetime.utcfromtimestamp(parsed),
|
|
|
|
datetime(1994, 11, 06, 8, 49, 37))
|
|
|
|
|
|
|
|
def testParsingAsctime(self):
|
|
|
|
parsed = parse_http_date('Sun Nov 6 08:49:37 1994')
|
|
|
|
self.assertEqual(datetime.utcfromtimestamp(parsed),
|
|
|
|
datetime(1994, 11, 06, 8, 49, 37))
|