1 | from django.conf import settings
|
---|
2 | from django.conf.urls.defaults import * #@UnusedWildImport
|
---|
3 | from django.contrib.auth.models import User
|
---|
4 | from django.http import HttpResponse
|
---|
5 | from django.test import TestCase, Client
|
---|
6 | from django.contrib.auth.signals import user_logged_in
|
---|
7 | from django.dispatch import receiver
|
---|
8 |
|
---|
9 | urlpatterns = patterns('',
|
---|
10 | (r'^$', 'testing.tests.http_host_view'),
|
---|
11 | )
|
---|
12 |
|
---|
13 | def http_host_view(request):
|
---|
14 | return HttpResponse(request.get_host() + request.middlewares)
|
---|
15 |
|
---|
16 | class MyMiddleware:
|
---|
17 | def process_request(self, request):
|
---|
18 | request.middlewares = '+middlewares'
|
---|
19 |
|
---|
20 | class DjangoClientRequestTests(TestCase):
|
---|
21 | urls = 'testing.tests'
|
---|
22 |
|
---|
23 | def setUp(self):
|
---|
24 | u = User.objects.create(username='tester')
|
---|
25 | u.set_password('1234')
|
---|
26 | u.save()
|
---|
27 | self.MIDDLEWARE_CLASSES = settings.MIDDLEWARE_CLASSES
|
---|
28 | settings.MIDDLEWARE_CLASSES = ['testing.tests.MyMiddleware']
|
---|
29 |
|
---|
30 | def tearDown(self):
|
---|
31 | settings.MIDDLEWARE_CLASSES = self.MIDDLEWARE_CLASSES
|
---|
32 |
|
---|
33 | def test_client_request(self):
|
---|
34 | c = Client(HTTP_HOST='myhost.com')
|
---|
35 | self.assertEqual(c.get("/").content, 'myhost.com+middlewares')
|
---|
36 |
|
---|
37 | def test_client_login(self):
|
---|
38 | c = Client(HTTP_HOST='myhost.com')
|
---|
39 |
|
---|
40 | @receiver(user_logged_in)
|
---|
41 | def login(sender, signal, **kwargs):
|
---|
42 | request = kwargs.get('request', None)
|
---|
43 | if request:
|
---|
44 | self.assertEqual(request.get_host() + request.middlewares, 'myhost.com+middlewares') # <------ This fails
|
---|
45 |
|
---|
46 | c.login(username='tester', password='1234')
|
---|
47 |
|
---|