Fixed #13842 -- Added tests to verify that XViewMiddleware works with class-based views.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@14269 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Russell Keith-Magee 2010-10-18 23:58:06 +00:00
parent cb33aa1cc8
commit 2790cf482d
3 changed files with 39 additions and 15 deletions

View File

@ -8,33 +8,52 @@ class SpecialHeadersTest(TestCase):
user = User.objects.get(username='super') user = User.objects.get(username='super')
response = self.client.get('/special_headers/article/1/') response = self.client.get('/special_headers/article/1/')
# import pdb; pdb.set_trace() # import pdb; pdb.set_trace()
self.failUnless('X-Object-Type' not in response) self.assertFalse('X-Object-Type' in response)
self.client.login(username='super', password='secret') self.client.login(username='super', password='secret')
response = self.client.get('/special_headers/article/1/') response = self.client.get('/special_headers/article/1/')
self.failUnless('X-Object-Type' in response) self.assertTrue('X-Object-Type' in response)
user.is_staff = False user.is_staff = False
user.save() user.save()
response = self.client.get('/special_headers/article/1/') response = self.client.get('/special_headers/article/1/')
self.failUnless('X-Object-Type' not in response) self.assertFalse('X-Object-Type' in response)
user.is_staff = True user.is_staff = True
user.is_active = False user.is_active = False
user.save() user.save()
response = self.client.get('/special_headers/article/1/') response = self.client.get('/special_headers/article/1/')
self.failUnless('X-Object-Type' not in response) self.assertFalse('X-Object-Type' in response)
def test_xview(self): def test_xview_func(self):
user = User.objects.get(username='super') user = User.objects.get(username='super')
response = self.client.head('/special_headers/xview/') response = self.client.head('/special_headers/xview/func/')
self.failUnless('X-View' not in response) self.assertFalse('X-View' in response)
self.client.login(username='super', password='secret') self.client.login(username='super', password='secret')
response = self.client.head('/special_headers/xview/') response = self.client.head('/special_headers/xview/func/')
self.failUnless('X-View' in response) self.assertTrue('X-View' in response)
self.assertEqual(response['X-View'], 'regressiontests.special_headers.views.xview')
user.is_staff = False user.is_staff = False
user.save() user.save()
response = self.client.head('/special_headers/xview/') response = self.client.head('/special_headers/xview/func/')
self.failUnless('X-View' not in response) self.assertFalse('X-View' in response)
user.is_staff = True user.is_staff = True
user.is_active = False user.is_active = False
user.save() user.save()
response = self.client.head('/special_headers/xview/') response = self.client.head('/special_headers/xview/func/')
self.failUnless('X-View' not in response) self.assertFalse('X-View' in response)
def test_xview_class(self):
user = User.objects.get(username='super')
response = self.client.head('/special_headers/xview/class/')
self.assertFalse('X-View' in response)
self.client.login(username='super', password='secret')
response = self.client.head('/special_headers/xview/class/')
self.assertTrue('X-View' in response)
self.assertEqual(response['X-View'], 'regressiontests.special_headers.views.XViewClass')
user.is_staff = False
user.save()
response = self.client.head('/special_headers/xview/class/')
self.assertFalse('X-View' in response)
user.is_staff = True
user.is_active = False
user.save()
response = self.client.head('/special_headers/xview/class/')
self.assertFalse('X-View' in response)

View File

@ -6,5 +6,6 @@ import views
urlpatterns = patterns('', urlpatterns = patterns('',
(r'^article/(?P<object_id>\d+)/$', object_detail, {'queryset': Article.objects.all()}), (r'^article/(?P<object_id>\d+)/$', object_detail, {'queryset': Article.objects.all()}),
(r'^xview/$', views.xview), (r'^xview/func/$', views.xview_dec(views.xview)),
(r'^xview/class/$', views.xview_dec(views.XViewClass.as_view())),
) )

View File

@ -1,10 +1,14 @@
# -*- coding:utf-8 -*- # -*- coding:utf-8 -*-
from django.http import HttpResponse from django.http import HttpResponse
from django.utils.decorators import decorator_from_middleware from django.utils.decorators import decorator_from_middleware
from django.views.generic import View
from django.middleware.doc import XViewMiddleware from django.middleware.doc import XViewMiddleware
xview_dec = decorator_from_middleware(XViewMiddleware) xview_dec = decorator_from_middleware(XViewMiddleware)
def xview(request): def xview(request):
return HttpResponse() return HttpResponse()
xview = xview_dec(xview)
class XViewClass(View):
def get(request):
return HttpResponse()