From 311feae8ba2a7b47e998764a1b0a26b5132e56f0 Mon Sep 17 00:00:00 2001 From: Sean Wang Date: Wed, 12 Nov 2014 21:08:35 -0800 Subject: [PATCH] Fixed #23806 -- Added a test case for settings.DISALLOWED_USER_AGENTS. --- tests/middleware/tests.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/tests/middleware/tests.py b/tests/middleware/tests.py index f0a89d719ac..6fb5cb42807 100644 --- a/tests/middleware/tests.py +++ b/tests/middleware/tests.py @@ -18,6 +18,7 @@ from django.middleware.common import CommonMiddleware, BrokenLinkEmailsMiddlewar from django.middleware.http import ConditionalGetMiddleware from django.middleware.gzip import GZipMiddleware from django.test import TestCase, RequestFactory, override_settings +from django.test.utils import patch_logger from django.utils import six from django.utils.encoding import force_str from django.utils.six.moves import xrange @@ -238,6 +239,15 @@ class CommonMiddlewareTest(TestCase): # Other tests + @override_settings(DISALLOWED_USER_AGENTS=[re.compile(r'foo')]) + def test_disallowed_user_agents(self): + with patch_logger('django.request', 'warning') as log_messages: + request = self._get_request('slash') + request.META['HTTP_USER_AGENT'] = 'foo' + r = CommonMiddleware().process_request(request) + self.assertEqual(r.status_code, 403) + self.assertEqual(log_messages, ['Forbidden (User agent): /slash']) + def test_non_ascii_query_string_does_not_crash(self): """Regression test for #15152""" request = self._get_request('slash')