PageRenderTime 28ms CodeModel.GetById 16ms app.highlight 9ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/conditional_processing/models.py

https://code.google.com/p/mango-py/
Python | 154 lines | 148 code | 5 blank | 1 comment | 2 complexity | b698d9f63336f11e2686415fba72c898 MD5 | raw file
  1# -*- coding:utf-8 -*-
  2from datetime import datetime
  3
  4from django.test import TestCase
  5from django.utils import unittest
  6from django.utils.http import parse_etags, quote_etag, parse_http_date
  7
  8FULL_RESPONSE = 'Test conditional get response'
  9LAST_MODIFIED = datetime(2007, 10, 21, 23, 21, 47)
 10LAST_MODIFIED_STR = 'Sun, 21 Oct 2007 23:21:47 GMT'
 11LAST_MODIFIED_NEWER_STR = 'Mon, 18 Oct 2010 16:56:23 GMT'
 12LAST_MODIFIED_INVALID_STR = 'Mon, 32 Oct 2010 16:56:23 GMT'
 13EXPIRED_LAST_MODIFIED_STR = 'Sat, 20 Oct 2007 23:21:47 GMT'
 14ETAG = 'b4246ffc4f62314ca13147c9d4f76974'
 15EXPIRED_ETAG = '7fae4cd4b0f81e7d2914700043aa8ed6'
 16
 17
 18class ConditionalGet(TestCase):
 19    def assertFullResponse(self, response, check_last_modified=True, check_etag=True):
 20        self.assertEqual(response.status_code, 200)
 21        self.assertEqual(response.content, FULL_RESPONSE)
 22        if check_last_modified:
 23            self.assertEqual(response['Last-Modified'], LAST_MODIFIED_STR)
 24        if check_etag:
 25            self.assertEqual(response['ETag'], '"%s"' % ETAG)
 26
 27    def assertNotModified(self, response):
 28        self.assertEqual(response.status_code, 304)
 29        self.assertEqual(response.content, '')
 30
 31    def testWithoutConditions(self):
 32        response = self.client.get('/condition/')
 33        self.assertFullResponse(response)
 34
 35    def testIfModifiedSince(self):
 36        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
 37        response = self.client.get('/condition/')
 38        self.assertNotModified(response)
 39        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_NEWER_STR
 40        response = self.client.get('/condition/')
 41        self.assertNotModified(response)
 42        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_INVALID_STR
 43        response = self.client.get('/condition/')
 44        self.assertFullResponse(response)
 45        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
 46        response = self.client.get('/condition/')
 47        self.assertFullResponse(response)
 48
 49    def testIfNoneMatch(self):
 50        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
 51        response = self.client.get('/condition/')
 52        self.assertNotModified(response)
 53        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
 54        response = self.client.get('/condition/')
 55        self.assertFullResponse(response)
 56
 57        # Several etags in If-None-Match is a bit exotic but why not?
 58        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s", "%s"' % (ETAG, EXPIRED_ETAG)
 59        response = self.client.get('/condition/')
 60        self.assertNotModified(response)
 61
 62    def testIfMatch(self):
 63        self.client.defaults['HTTP_IF_MATCH'] = '"%s"' % ETAG
 64        response = self.client.put('/condition/etag/', {'data': ''})
 65        self.assertEqual(response.status_code, 200)
 66        self.client.defaults['HTTP_IF_MATCH'] = '"%s"' % EXPIRED_ETAG
 67        response = self.client.put('/condition/etag/', {'data': ''})
 68        self.assertEqual(response.status_code, 412)
 69
 70    def testBothHeaders(self):
 71        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
 72        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
 73        response = self.client.get('/condition/')
 74        self.assertNotModified(response)
 75
 76        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
 77        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
 78        response = self.client.get('/condition/')
 79        self.assertFullResponse(response)
 80
 81        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
 82        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
 83        response = self.client.get('/condition/')
 84        self.assertFullResponse(response)
 85
 86    def testSingleCondition1(self):
 87        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
 88        response = self.client.get('/condition/last_modified/')
 89        self.assertNotModified(response)
 90        response = self.client.get('/condition/etag/')
 91        self.assertFullResponse(response, check_last_modified=False)
 92
 93    def testSingleCondition2(self):
 94        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
 95        response = self.client.get('/condition/etag/')
 96        self.assertNotModified(response)
 97        response = self.client.get('/condition/last_modified/')
 98        self.assertFullResponse(response, check_etag=False)
 99
100    def testSingleCondition3(self):
101        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = EXPIRED_LAST_MODIFIED_STR
102        response = self.client.get('/condition/last_modified/')
103        self.assertFullResponse(response, check_etag=False)
104
105    def testSingleCondition4(self):
106        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % EXPIRED_ETAG
107        response = self.client.get('/condition/etag/')
108        self.assertFullResponse(response, check_last_modified=False)
109
110    def testSingleCondition5(self):
111        self.client.defaults['HTTP_IF_MODIFIED_SINCE'] = LAST_MODIFIED_STR
112        response = self.client.get('/condition/last_modified2/')
113        self.assertNotModified(response)
114        response = self.client.get('/condition/etag2/')
115        self.assertFullResponse(response, check_last_modified=False)
116
117    def testSingleCondition6(self):
118        self.client.defaults['HTTP_IF_NONE_MATCH'] = '"%s"' % ETAG
119        response = self.client.get('/condition/etag2/')
120        self.assertNotModified(response)
121        response = self.client.get('/condition/last_modified2/')
122        self.assertFullResponse(response, check_etag=False)
123
124    def testInvalidETag(self):
125        self.client.defaults['HTTP_IF_NONE_MATCH'] = r'"\"'
126        response = self.client.get('/condition/etag/')
127        self.assertFullResponse(response, check_last_modified=False)
128
129
130class ETagProcessing(unittest.TestCase):
131    def testParsing(self):
132        etags = parse_etags(r'"", "etag", "e\"t\"ag", "e\\tag", W/"weak"')
133        self.assertEqual(etags, ['', 'etag', 'e"t"ag', r'e\tag', 'weak'])
134
135    def testQuoting(self):
136        quoted_etag = quote_etag(r'e\t"ag')
137        self.assertEqual(quoted_etag, r'"e\\t\"ag"')
138
139
140class HttpDateProcessing(unittest.TestCase):
141    def testParsingRfc1123(self):
142        parsed = parse_http_date('Sun, 06 Nov 1994 08:49:37 GMT')
143        self.assertEqual(datetime.utcfromtimestamp(parsed),
144                         datetime(1994, 11, 06, 8, 49, 37))
145
146    def testParsingRfc850(self):
147        parsed = parse_http_date('Sunday, 06-Nov-94 08:49:37 GMT')
148        self.assertEqual(datetime.utcfromtimestamp(parsed),
149                         datetime(1994, 11, 06, 8, 49, 37))
150
151    def testParsingAsctime(self):
152        parsed = parse_http_date('Sun Nov  6 08:49:37 1994')
153        self.assertEqual(datetime.utcfromtimestamp(parsed),
154                         datetime(1994, 11, 06, 8, 49, 37))