Ticket #12662: hostname_middleware.diff

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

Test patch

  • tests/regressiontests/middleware/tests.py

     
    33from django.test import TestCase
    44from django.http import HttpRequest
    55from django.middleware.common import CommonMiddleware
     6from django.middleware.hostname import EnforceHostnameMiddleware
    67from django.conf import settings
    78
    89class CommonMiddlewareTest(TestCase):
     
    125126        self.assertEquals(r.status_code, 301)
    126127        self.assertEquals(r['Location'],
    127128                          'http://www.testserver/middleware/slash/')
     129
     130class EnforceHostnameMiddlewareTest(TestCase):
     131    def setUp(self):
     132        try:
     133            self.enforce = settings.ENFORCE_HOSTNAME
     134        except:
     135            self.enforce = None
     136        settings.ENFORCE_HOSTNAME = None
     137
     138    def tearDown(self):
     139        if self.enforce:
     140            settings.ENFORCE_HOSTNAME = self.enforce
     141   
     142    def _get_request(self, path):
     143        request = HttpRequest()
     144        request.META = {
     145            'SERVER_NAME': 'testserver',
     146            'SERVER_PORT': 80,
     147        }
     148        request.path = request.path_info = "/middleware/%s" % path
     149        return request
     150   
     151    def test_no_enforce_setting(self):
     152        """
     153        No enforce setting
     154        """
     155        request = self._get_request('test/')
     156        r = EnforceHostnameMiddleware().process_request(request)
     157        self.assertEqual(r, None)
     158   
     159    def test_no_redirect_necessary(self):
     160        """
     161        Enforce is set, but the hostname matches
     162        """
     163        settings.ENFORCE_HOSTNAME = 'testserver'
     164        request = self._get_request('test/')
     165        r = EnforceHostnameMiddleware().process_request(request)
     166        self.assertEqual(r, None)
     167   
     168    def test_single_hostname_with_redirect(self):
     169        """
     170        Enforce is set and the single hostname does not match
     171        """
     172        settings.ENFORCE_HOSTNAME = 'example.com'
     173        request = self._get_request('test/')
     174        r = EnforceHostnameMiddleware().process_request(request)
     175        self.assertEqual(r.status_code, 301)
     176        self.assertEqual(r['location'], 'http://example.com/middleware/test/')
     177   
     178    def test_single_hostname_with_redirect_and_querystring(self):
     179        """
     180        Enforce is set and the single hostname does not match (and there is a querystring)
     181        """
     182        settings.ENFORCE_HOSTNAME = 'example.com'
     183        request = self._get_request('test/')
     184        request.GET['test'] = 1
     185        request.META['QUERY_STRING'] = 'test=1'
     186        r = EnforceHostnameMiddleware().process_request(request)
     187        self.assertEqual(r.status_code, 301)
     188        self.assertEqual(r['location'], 'http://example.com/middleware/test/?test=1')
     189   
     190    def test_multiple_hostnames_with_redirect(self):
     191        """
     192        Enforce is set with multiple hostnames and the hostname does not match
     193        """
     194        settings.ENFORCE_HOSTNAME = ['example.com', 'www.example.com']
     195        request = self._get_request('test/')
     196        r = EnforceHostnameMiddleware().process_request(request)
     197        self.assertEqual(r.status_code, 301)
     198        self.assertEqual(r['location'], 'http://example.com/middleware/test/')
     199   
     200    def test_multiple_hostnames_without_redirect(self):
     201        """
     202        Enforce is set with multiple hostnames and the hostname does match
     203        """
     204        settings.ENFORCE_HOSTNAME = ['example.com', 'www.example.com', 'testserver']
     205        request = self._get_request('test/')
     206        r = EnforceHostnameMiddleware().process_request(request)
     207        self.assertEqual(r, None)
     208       
Back to Top