Code

Ticket #12662: hostname_middleware.diff

File hostname_middleware.diff, 3.7 KB (added by robballou, 4 years ago)

Test patch

Line 
1Index: tests/regressiontests/middleware/tests.py
2===================================================================
3--- tests/regressiontests/middleware/tests.py   (revision 12110)
4+++ tests/regressiontests/middleware/tests.py   (working copy)
5@@ -3,6 +3,7 @@
6 from django.test import TestCase
7 from django.http import HttpRequest
8 from django.middleware.common import CommonMiddleware
9+from django.middleware.hostname import EnforceHostnameMiddleware
10 from django.conf import settings
11 
12 class CommonMiddlewareTest(TestCase):
13@@ -125,3 +126,83 @@
14         self.assertEquals(r.status_code, 301)
15         self.assertEquals(r['Location'],
16                           'http://www.testserver/middleware/slash/')
17+
18+class EnforceHostnameMiddlewareTest(TestCase):
19+    def setUp(self):
20+        try:
21+            self.enforce = settings.ENFORCE_HOSTNAME
22+        except:
23+            self.enforce = None
24+        settings.ENFORCE_HOSTNAME = None
25+
26+    def tearDown(self):
27+        if self.enforce:
28+            settings.ENFORCE_HOSTNAME = self.enforce
29+   
30+    def _get_request(self, path):
31+        request = HttpRequest()
32+        request.META = {
33+            'SERVER_NAME': 'testserver',
34+            'SERVER_PORT': 80,
35+        }
36+        request.path = request.path_info = "/middleware/%s" % path
37+        return request
38+   
39+    def test_no_enforce_setting(self):
40+        """
41+        No enforce setting
42+        """
43+        request = self._get_request('test/')
44+        r = EnforceHostnameMiddleware().process_request(request)
45+        self.assertEqual(r, None)
46+   
47+    def test_no_redirect_necessary(self):
48+        """
49+        Enforce is set, but the hostname matches
50+        """
51+        settings.ENFORCE_HOSTNAME = 'testserver'
52+        request = self._get_request('test/')
53+        r = EnforceHostnameMiddleware().process_request(request)
54+        self.assertEqual(r, None)
55+   
56+    def test_single_hostname_with_redirect(self):
57+        """
58+        Enforce is set and the single hostname does not match
59+        """
60+        settings.ENFORCE_HOSTNAME = 'example.com'
61+        request = self._get_request('test/')
62+        r = EnforceHostnameMiddleware().process_request(request)
63+        self.assertEqual(r.status_code, 301)
64+        self.assertEqual(r['location'], 'http://example.com/middleware/test/')
65+   
66+    def test_single_hostname_with_redirect_and_querystring(self):
67+        """
68+        Enforce is set and the single hostname does not match (and there is a querystring)
69+        """
70+        settings.ENFORCE_HOSTNAME = 'example.com'
71+        request = self._get_request('test/')
72+        request.GET['test'] = 1
73+        request.META['QUERY_STRING'] = 'test=1'
74+        r = EnforceHostnameMiddleware().process_request(request)
75+        self.assertEqual(r.status_code, 301)
76+        self.assertEqual(r['location'], 'http://example.com/middleware/test/?test=1')
77+   
78+    def test_multiple_hostnames_with_redirect(self):
79+        """
80+        Enforce is set with multiple hostnames and the hostname does not match
81+        """
82+        settings.ENFORCE_HOSTNAME = ['example.com', 'www.example.com']
83+        request = self._get_request('test/')
84+        r = EnforceHostnameMiddleware().process_request(request)
85+        self.assertEqual(r.status_code, 301)
86+        self.assertEqual(r['location'], 'http://example.com/middleware/test/')
87+   
88+    def test_multiple_hostnames_without_redirect(self):
89+        """
90+        Enforce is set with multiple hostnames and the hostname does match
91+        """
92+        settings.ENFORCE_HOSTNAME = ['example.com', 'www.example.com', 'testserver']
93+        request = self._get_request('test/')
94+        r = EnforceHostnameMiddleware().process_request(request)
95+        self.assertEqual(r, None)
96+