Ticket #7299: django_xview_middleware.diff

File django_xview_middleware.diff, 2.6 KB (added by Takanori Ishikawa, 16 years ago)
  • django/middleware/doc.py

     
    1212        indicating the view function.  This is used by the documentation module
    1313        to lookup the view function for an arbitrary page.
    1414        """
    15         if request.method == 'HEAD' and (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS or (request.user.is_authenticated() and request.user.is_staff)):
     15        if request.method == 'HEAD' and (request.META.get('REMOTE_ADDR') in settings.INTERNAL_IPS or (hasattr(request, 'user') and request.user.is_authenticated() and request.user.is_staff)):
    1616            response = http.HttpResponse()
    1717            response['X-View'] = "%s.%s" % (view_func.__module__, view_func.__name__)
    1818            return response
  • tests/regressiontests/middleware/tests.py

     
    33from django.test import TestCase
    44from django.http import HttpRequest
    55from django.middleware.common import CommonMiddleware
     6from django.middleware.doc import XViewMiddleware
     7from django.views.generic.simple import direct_to_template
    68from django.conf import settings
     9from django.conf import global_settings
    710
    811class CommonMiddlewareTest(TestCase):
    912    def _get_request(self, path):
     
    8992        self.assertEquals(
    9093            r['Location'],
    9194            'http://testserver/middleware/needsquoting%23/')
     95
     96
     97class XViewMiddlewareTest(TestCase):
     98    def tearDown(self):
     99        settings.INTERNAL_IPS = global_settings.INTERNAL_IPS
     100
     101    def test_internal_ips(self):
     102        """
     103        Tests that request method is HEAD and the IP is internal.
     104        """
     105        settings.INTERNAL_IPS = ('127.0.0.1',)
     106        request = HttpRequest()
     107        request.method, request.path = 'HEAD', "/middleware/xview"
     108       
     109        request.META = {
     110            'SERVER_NAME': 'testserver',
     111            'SERVER_PORT': 80,
     112            'REMOTE_ADDR': settings.INTERNAL_IPS[0],
     113        }
     114        response = XViewMiddleware().process_view(request, direct_to_template, (), {})
     115        self.assertEquals(response.status_code, 200)
     116        self.assertEquals(response['X-View'], 'django.views.generic.simple.direct_to_template')
     117       
     118        # No INTERNAL_IPS
     119        request.META['REMOTE_ADDR'] = '127.0.0.2'
     120        response = XViewMiddleware().process_view(request, direct_to_template, (), {})
     121        self.assert_(response is None)
Back to Top