PageRenderTime 63ms CodeModel.GetById 43ms app.highlight 17ms RepoModel.GetById 1ms app.codeStats 0ms

/tests/regressiontests/utils/decorators.py

https://code.google.com/p/mango-py/
Python | 106 lines | 59 code | 23 blank | 24 comment | 0 complexity | 2c50cc1ff623bb2f03c97b19b1c51c42 MD5 | raw file
  1from django.http import HttpResponse
  2from django.middleware.doc import XViewMiddleware
  3from django.template import Template, Context
  4from django.template.response import TemplateResponse
  5from django.test import TestCase, RequestFactory
  6from django.utils.decorators import decorator_from_middleware
  7
  8
  9xview_dec = decorator_from_middleware(XViewMiddleware)
 10
 11
 12@xview_dec
 13def xview(request):
 14    return HttpResponse()
 15
 16
 17class ClassXView(object):
 18    def __call__(self, request):
 19        return HttpResponse()
 20
 21class_xview = xview_dec(ClassXView())
 22
 23
 24class FullMiddleware(object):
 25    def process_request(self, request):
 26        request.process_request_reached = True
 27
 28    def process_view(sef, request, view_func, view_args, view_kwargs):
 29        request.process_view_reached = True
 30
 31    def process_template_response(self, request, response):
 32        request.process_template_response_reached = True
 33        return response
 34
 35    def process_response(self, request, response):
 36        # This should never receive unrendered content.
 37        request.process_response_content = response.content
 38        request.process_response_reached = True
 39        return response
 40
 41full_dec = decorator_from_middleware(FullMiddleware)
 42
 43
 44class DecoratorFromMiddlewareTests(TestCase):
 45    """
 46    Tests for view decorators created using
 47    ``django.utils.decorators.decorator_from_middleware``.
 48    """
 49    rf = RequestFactory()
 50
 51    def test_process_view_middleware(self):
 52        """
 53        Test a middleware that implements process_view.
 54        """
 55        xview(self.rf.get('/'))
 56
 57    def test_callable_process_view_middleware(self):
 58        """
 59        Test a middleware that implements process_view, operating on a callable class.
 60        """
 61        class_xview(self.rf.get('/'))
 62
 63    def test_full_dec_normal(self):
 64        """
 65        Test that all methods of middleware are called for normal HttpResponses
 66        """
 67
 68        @full_dec
 69        def normal_view(request):
 70            t = Template("Hello world")
 71            return HttpResponse(t.render(Context({})))
 72
 73        request = self.rf.get('/')
 74        response = normal_view(request)
 75        self.assertTrue(getattr(request, 'process_request_reached', False))
 76        self.assertTrue(getattr(request, 'process_view_reached', False))
 77        # process_template_response must not be called for HttpResponse
 78        self.assertFalse(getattr(request, 'process_template_response_reached', False))
 79        self.assertTrue(getattr(request, 'process_response_reached', False))
 80
 81    def test_full_dec_templateresponse(self):
 82        """
 83        Test that all methods of middleware are called for TemplateResponses in
 84        the right sequence.
 85        """
 86
 87        @full_dec
 88        def template_response_view(request):
 89            t = Template("Hello world")
 90            return TemplateResponse(request, t, {})
 91
 92        request = self.rf.get('/')
 93        response = template_response_view(request)
 94        self.assertTrue(getattr(request, 'process_request_reached', False))
 95        self.assertTrue(getattr(request, 'process_view_reached', False))
 96        self.assertTrue(getattr(request, 'process_template_response_reached', False))
 97        # response must not be rendered yet.
 98        self.assertFalse(response._is_rendered)
 99        # process_response must not be called until after response is rendered,
100        # otherwise some decorators like csrf_protect and gzip_page will not
101        # work correctly. See #16004
102        self.assertFalse(getattr(request, 'process_response_reached', False))
103        response.render()
104        self.assertTrue(getattr(request, 'process_response_reached', False))
105        # Check that process_response saw the rendered content
106        self.assertEqual(request.process_response_content, "Hello world")