PageRenderTime 87ms CodeModel.GetById 49ms app.highlight 33ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/middleware/tests.py

https://code.google.com/p/mango-py/
Python | 336 lines | 275 code | 36 blank | 25 comment | 6 complexity | cb14a4fe7b1c6aca284993ab38e51afc MD5 | raw file
  1# -*- coding: utf-8 -*-
  2
  3from django.conf import settings
  4from django.http import HttpRequest
  5from django.middleware.common import CommonMiddleware
  6from django.middleware.http import ConditionalGetMiddleware
  7from django.test import TestCase
  8
  9
 10class CommonMiddlewareTest(TestCase):
 11    def setUp(self):
 12        self.slash = settings.APPEND_SLASH
 13        self.www = settings.PREPEND_WWW
 14
 15    def tearDown(self):
 16        settings.APPEND_SLASH = self.slash
 17        settings.PREPEND_WWW = self.www
 18
 19    def _get_request(self, path):
 20        request = HttpRequest()
 21        request.META = {
 22            'SERVER_NAME': 'testserver',
 23            'SERVER_PORT': 80,
 24        }
 25        request.path = request.path_info = "/middleware/%s" % path
 26        return request
 27
 28    def test_append_slash_have_slash(self):
 29        """
 30        Tests that URLs with slashes go unmolested.
 31        """
 32        settings.APPEND_SLASH = True
 33        request = self._get_request('slash/')
 34        self.assertEqual(CommonMiddleware().process_request(request), None)
 35
 36    def test_append_slash_slashless_resource(self):
 37        """
 38        Tests that matches to explicit slashless URLs go unmolested.
 39        """
 40        settings.APPEND_SLASH = True
 41        request = self._get_request('noslash')
 42        self.assertEqual(CommonMiddleware().process_request(request), None)
 43
 44    def test_append_slash_slashless_unknown(self):
 45        """
 46        Tests that APPEND_SLASH doesn't redirect to unknown resources.
 47        """
 48        settings.APPEND_SLASH = True
 49        request = self._get_request('unknown')
 50        self.assertEqual(CommonMiddleware().process_request(request), None)
 51
 52    def test_append_slash_redirect(self):
 53        """
 54        Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
 55        """
 56        settings.APPEND_SLASH = True
 57        request = self._get_request('slash')
 58        r = CommonMiddleware().process_request(request)
 59        self.assertEqual(r.status_code, 301)
 60        self.assertEqual(r['Location'], 'http://testserver/middleware/slash/')
 61
 62    def test_append_slash_no_redirect_on_POST_in_DEBUG(self):
 63        """
 64        Tests that while in debug mode, an exception is raised with a warning
 65        when a failed attempt is made to POST to an URL which would normally be
 66        redirected to a slashed version.
 67        """
 68        settings.APPEND_SLASH = True
 69        settings.DEBUG = True
 70        request = self._get_request('slash')
 71        request.method = 'POST'
 72        self.assertRaises(
 73            RuntimeError,
 74            CommonMiddleware().process_request,
 75            request)
 76        try:
 77            CommonMiddleware().process_request(request)
 78        except RuntimeError, e:
 79            self.assertTrue('end in a slash' in str(e))
 80        settings.DEBUG = False
 81
 82    def test_append_slash_disabled(self):
 83        """
 84        Tests disabling append slash functionality.
 85        """
 86        settings.APPEND_SLASH = False
 87        request = self._get_request('slash')
 88        self.assertEqual(CommonMiddleware().process_request(request), None)
 89
 90    def test_append_slash_quoted(self):
 91        """
 92        Tests that URLs which require quoting are redirected to their slash
 93        version ok.
 94        """
 95        settings.APPEND_SLASH = True
 96        request = self._get_request('needsquoting#')
 97        r = CommonMiddleware().process_request(request)
 98        self.assertEqual(r.status_code, 301)
 99        self.assertEqual(
100            r['Location'],
101            'http://testserver/middleware/needsquoting%23/')
102
103    def test_prepend_www(self):
104        settings.PREPEND_WWW = True
105        settings.APPEND_SLASH = False
106        request = self._get_request('path/')
107        r = CommonMiddleware().process_request(request)
108        self.assertEqual(r.status_code, 301)
109        self.assertEqual(
110            r['Location'],
111            'http://www.testserver/middleware/path/')
112
113    def test_prepend_www_append_slash_have_slash(self):
114        settings.PREPEND_WWW = True
115        settings.APPEND_SLASH = True
116        request = self._get_request('slash/')
117        r = CommonMiddleware().process_request(request)
118        self.assertEqual(r.status_code, 301)
119        self.assertEqual(r['Location'],
120                          'http://www.testserver/middleware/slash/')
121
122    def test_prepend_www_append_slash_slashless(self):
123        settings.PREPEND_WWW = True
124        settings.APPEND_SLASH = True
125        request = self._get_request('slash')
126        r = CommonMiddleware().process_request(request)
127        self.assertEqual(r.status_code, 301)
128        self.assertEqual(r['Location'],
129                          'http://www.testserver/middleware/slash/')
130
131
132    # The following tests examine expected behavior given a custom urlconf that
133    # overrides the default one through the request object.
134
135    def test_append_slash_have_slash_custom_urlconf(self):
136      """
137      Tests that URLs with slashes go unmolested.
138      """
139      settings.APPEND_SLASH = True
140      request = self._get_request('customurlconf/slash/')
141      request.urlconf = 'regressiontests.middleware.extra_urls'
142      self.assertEqual(CommonMiddleware().process_request(request), None)
143
144    def test_append_slash_slashless_resource_custom_urlconf(self):
145      """
146      Tests that matches to explicit slashless URLs go unmolested.
147      """
148      settings.APPEND_SLASH = True
149      request = self._get_request('customurlconf/noslash')
150      request.urlconf = 'regressiontests.middleware.extra_urls'
151      self.assertEqual(CommonMiddleware().process_request(request), None)
152
153    def test_append_slash_slashless_unknown_custom_urlconf(self):
154      """
155      Tests that APPEND_SLASH doesn't redirect to unknown resources.
156      """
157      settings.APPEND_SLASH = True
158      request = self._get_request('customurlconf/unknown')
159      request.urlconf = 'regressiontests.middleware.extra_urls'
160      self.assertEqual(CommonMiddleware().process_request(request), None)
161
162    def test_append_slash_redirect_custom_urlconf(self):
163      """
164      Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
165      """
166      settings.APPEND_SLASH = True
167      request = self._get_request('customurlconf/slash')
168      request.urlconf = 'regressiontests.middleware.extra_urls'
169      r = CommonMiddleware().process_request(request)
170      self.assertFalse(r is None,
171          "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
172      self.assertEqual(r.status_code, 301)
173      self.assertEqual(r['Location'], 'http://testserver/middleware/customurlconf/slash/')
174
175    def test_append_slash_no_redirect_on_POST_in_DEBUG_custom_urlconf(self):
176      """
177      Tests that while in debug mode, an exception is raised with a warning
178      when a failed attempt is made to POST to an URL which would normally be
179      redirected to a slashed version.
180      """
181      settings.APPEND_SLASH = True
182      settings.DEBUG = True
183      request = self._get_request('customurlconf/slash')
184      request.urlconf = 'regressiontests.middleware.extra_urls'
185      request.method = 'POST'
186      self.assertRaises(
187          RuntimeError,
188          CommonMiddleware().process_request,
189          request)
190      try:
191          CommonMiddleware().process_request(request)
192      except RuntimeError, e:
193          self.assertTrue('end in a slash' in str(e))
194      settings.DEBUG = False
195
196    def test_append_slash_disabled_custom_urlconf(self):
197      """
198      Tests disabling append slash functionality.
199      """
200      settings.APPEND_SLASH = False
201      request = self._get_request('customurlconf/slash')
202      request.urlconf = 'regressiontests.middleware.extra_urls'
203      self.assertEqual(CommonMiddleware().process_request(request), None)
204
205    def test_append_slash_quoted_custom_urlconf(self):
206      """
207      Tests that URLs which require quoting are redirected to their slash
208      version ok.
209      """
210      settings.APPEND_SLASH = True
211      request = self._get_request('customurlconf/needsquoting#')
212      request.urlconf = 'regressiontests.middleware.extra_urls'
213      r = CommonMiddleware().process_request(request)
214      self.assertFalse(r is None,
215          "CommonMiddlware failed to return APPEND_SLASH redirect using request.urlconf")
216      self.assertEqual(r.status_code, 301)
217      self.assertEqual(
218          r['Location'],
219          'http://testserver/middleware/customurlconf/needsquoting%23/')
220
221    def test_prepend_www_custom_urlconf(self):
222      settings.PREPEND_WWW = True
223      settings.APPEND_SLASH = False
224      request = self._get_request('customurlconf/path/')
225      request.urlconf = 'regressiontests.middleware.extra_urls'
226      r = CommonMiddleware().process_request(request)
227      self.assertEqual(r.status_code, 301)
228      self.assertEqual(
229          r['Location'],
230          'http://www.testserver/middleware/customurlconf/path/')
231
232    def test_prepend_www_append_slash_have_slash_custom_urlconf(self):
233      settings.PREPEND_WWW = True
234      settings.APPEND_SLASH = True
235      request = self._get_request('customurlconf/slash/')
236      request.urlconf = 'regressiontests.middleware.extra_urls'
237      r = CommonMiddleware().process_request(request)
238      self.assertEqual(r.status_code, 301)
239      self.assertEqual(r['Location'],
240                        'http://www.testserver/middleware/customurlconf/slash/')
241
242    def test_prepend_www_append_slash_slashless_custom_urlconf(self):
243      settings.PREPEND_WWW = True
244      settings.APPEND_SLASH = True
245      request = self._get_request('customurlconf/slash')
246      request.urlconf = 'regressiontests.middleware.extra_urls'
247      r = CommonMiddleware().process_request(request)
248      self.assertEqual(r.status_code, 301)
249      self.assertEqual(r['Location'],
250                        'http://www.testserver/middleware/customurlconf/slash/')
251
252class ConditionalGetMiddlewareTest(TestCase):
253    urls = 'regressiontests.middleware.cond_get_urls'
254    def setUp(self):
255        self.req = HttpRequest()
256        self.req.META = {
257            'SERVER_NAME': 'testserver',
258            'SERVER_PORT': 80,
259        }
260        self.req.path = self.req.path_info = "/"
261        self.resp = self.client.get(self.req.path)
262
263    # Tests for the Date header
264
265    def test_date_header_added(self):
266        self.assertFalse('Date' in self.resp)
267        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
268        self.assertTrue('Date' in self.resp)
269
270    # Tests for the Content-Length header
271
272    def test_content_length_header_added(self):
273        content_length = len(self.resp.content)
274        self.assertFalse('Content-Length' in self.resp)
275        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
276        self.assertTrue('Content-Length' in self.resp)
277        self.assertEqual(int(self.resp['Content-Length']), content_length)
278
279    def test_content_length_header_not_changed(self):
280        bad_content_length = len(self.resp.content) + 10
281        self.resp['Content-Length'] = bad_content_length
282        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
283        self.assertEqual(int(self.resp['Content-Length']), bad_content_length)
284
285    # Tests for the ETag header
286
287    def test_if_none_match_and_no_etag(self):
288        self.req.META['HTTP_IF_NONE_MATCH'] = 'spam'
289        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
290        self.assertEqual(self.resp.status_code, 200)
291
292    def test_no_if_none_match_and_etag(self):
293        self.resp['ETag'] = 'eggs'
294        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
295        self.assertEqual(self.resp.status_code, 200)
296
297    def test_if_none_match_and_same_etag(self):
298        self.req.META['HTTP_IF_NONE_MATCH'] = self.resp['ETag'] = 'spam'
299        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
300        self.assertEqual(self.resp.status_code, 304)
301
302    def test_if_none_match_and_different_etag(self):
303        self.req.META['HTTP_IF_NONE_MATCH'] = 'spam'
304        self.resp['ETag'] = 'eggs'
305        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
306        self.assertEqual(self.resp.status_code, 200)
307
308    # Tests for the Last-Modified header
309
310    def test_if_modified_since_and_no_last_modified(self):
311        self.req.META['HTTP_IF_MODIFIED_SINCE'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
312        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
313        self.assertEqual(self.resp.status_code, 200)
314
315    def test_no_if_modified_since_and_last_modified(self):
316        self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
317        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
318        self.assertEqual(self.resp.status_code, 200)
319
320    def test_if_modified_since_and_same_last_modified(self):
321        self.req.META['HTTP_IF_MODIFIED_SINCE'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
322        self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
323        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
324        self.assertEqual(self.resp.status_code, 304)
325
326    def test_if_modified_since_and_last_modified_in_the_past(self):
327        self.req.META['HTTP_IF_MODIFIED_SINCE'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
328        self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:35:44 GMT'
329        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
330        self.assertEqual(self.resp.status_code, 304)
331
332    def test_if_modified_since_and_last_modified_in_the_future(self):
333        self.req.META['HTTP_IF_MODIFIED_SINCE'] = 'Sat, 12 Feb 2011 17:38:44 GMT'
334        self.resp['Last-Modified'] = 'Sat, 12 Feb 2011 17:41:44 GMT'
335        self.resp = ConditionalGetMiddleware().process_response(self.req, self.resp)
336        self.assertEqual(self.resp.status_code, 200)