mirror of
https://github.com/django/django.git
synced 2024-12-23 01:25:58 +00:00
Converted middleware tests to use RequestFactory.
This commit is contained in:
parent
6fb9dee470
commit
bae404dea3
@ -22,26 +22,20 @@ 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 range
|
||||
from django.utils.six.moves.urllib.parse import quote
|
||||
|
||||
|
||||
@override_settings(ROOT_URLCONF='middleware.urls')
|
||||
class CommonMiddlewareTest(TestCase):
|
||||
|
||||
def _get_request(self, path):
|
||||
request = HttpRequest()
|
||||
request.META = {
|
||||
'SERVER_NAME': 'testserver',
|
||||
'SERVER_PORT': 80,
|
||||
}
|
||||
request.path = request.path_info = "/%s" % path
|
||||
return request
|
||||
rf = RequestFactory()
|
||||
|
||||
@override_settings(APPEND_SLASH=True)
|
||||
def test_append_slash_have_slash(self):
|
||||
"""
|
||||
Tests that URLs with slashes go unmolested.
|
||||
"""
|
||||
request = self._get_request('slash/')
|
||||
request = self.rf.get('/slash/')
|
||||
self.assertEqual(CommonMiddleware().process_request(request), None)
|
||||
|
||||
@override_settings(APPEND_SLASH=True)
|
||||
@ -49,7 +43,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
"""
|
||||
Tests that matches to explicit slashless URLs go unmolested.
|
||||
"""
|
||||
request = self._get_request('noslash')
|
||||
request = self.rf.get('/noslash')
|
||||
self.assertEqual(CommonMiddleware().process_request(request), None)
|
||||
|
||||
@override_settings(APPEND_SLASH=True)
|
||||
@ -57,7 +51,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
"""
|
||||
Tests that APPEND_SLASH doesn't redirect to unknown resources.
|
||||
"""
|
||||
request = self._get_request('unknown')
|
||||
request = self.rf.get('/unknown')
|
||||
self.assertEqual(CommonMiddleware().process_request(request), None)
|
||||
|
||||
@override_settings(APPEND_SLASH=True)
|
||||
@ -65,7 +59,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
"""
|
||||
Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
|
||||
"""
|
||||
request = self._get_request('slash')
|
||||
request = self.rf.get('/slash')
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(r.url, 'http://testserver/slash/')
|
||||
@ -77,7 +71,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
when a failed attempt is made to POST to an URL which would normally be
|
||||
redirected to a slashed version.
|
||||
"""
|
||||
request = self._get_request('slash')
|
||||
request = self.rf.get('/slash')
|
||||
request.method = 'POST'
|
||||
with six.assertRaisesRegex(self, RuntimeError, 'end in a slash'):
|
||||
CommonMiddleware().process_request(request)
|
||||
@ -87,7 +81,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
"""
|
||||
Tests disabling append slash functionality.
|
||||
"""
|
||||
request = self._get_request('slash')
|
||||
request = self.rf.get('/slash')
|
||||
self.assertEqual(CommonMiddleware().process_request(request), None)
|
||||
|
||||
@override_settings(APPEND_SLASH=True)
|
||||
@ -96,7 +90,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
Tests that URLs which require quoting are redirected to their slash
|
||||
version ok.
|
||||
"""
|
||||
request = self._get_request('needsquoting#')
|
||||
request = self.rf.get(quote('/needsquoting#'))
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(
|
||||
@ -105,7 +99,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
|
||||
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
|
||||
def test_prepend_www(self):
|
||||
request = self._get_request('path/')
|
||||
request = self.rf.get('/path/')
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(
|
||||
@ -114,7 +108,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
|
||||
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
||||
def test_prepend_www_append_slash_have_slash(self):
|
||||
request = self._get_request('slash/')
|
||||
request = self.rf.get('/slash/')
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(r.url,
|
||||
@ -122,7 +116,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
|
||||
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
||||
def test_prepend_www_append_slash_slashless(self):
|
||||
request = self._get_request('slash')
|
||||
request = self.rf.get('/slash')
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(r.url,
|
||||
@ -136,7 +130,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
"""
|
||||
Tests that URLs with slashes go unmolested.
|
||||
"""
|
||||
request = self._get_request('customurlconf/slash/')
|
||||
request = self.rf.get('/customurlconf/slash/')
|
||||
request.urlconf = 'middleware.extra_urls'
|
||||
self.assertEqual(CommonMiddleware().process_request(request), None)
|
||||
|
||||
@ -145,7 +139,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
"""
|
||||
Tests that matches to explicit slashless URLs go unmolested.
|
||||
"""
|
||||
request = self._get_request('customurlconf/noslash')
|
||||
request = self.rf.get('/customurlconf/noslash')
|
||||
request.urlconf = 'middleware.extra_urls'
|
||||
self.assertEqual(CommonMiddleware().process_request(request), None)
|
||||
|
||||
@ -154,7 +148,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
"""
|
||||
Tests that APPEND_SLASH doesn't redirect to unknown resources.
|
||||
"""
|
||||
request = self._get_request('customurlconf/unknown')
|
||||
request = self.rf.get('/customurlconf/unknown')
|
||||
request.urlconf = 'middleware.extra_urls'
|
||||
self.assertEqual(CommonMiddleware().process_request(request), None)
|
||||
|
||||
@ -163,7 +157,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
"""
|
||||
Tests that APPEND_SLASH redirects slashless URLs to a valid pattern.
|
||||
"""
|
||||
request = self._get_request('customurlconf/slash')
|
||||
request = self.rf.get('/customurlconf/slash')
|
||||
request.urlconf = 'middleware.extra_urls'
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertIsNotNone(r,
|
||||
@ -178,7 +172,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
when a failed attempt is made to POST to an URL which would normally be
|
||||
redirected to a slashed version.
|
||||
"""
|
||||
request = self._get_request('customurlconf/slash')
|
||||
request = self.rf.get('/customurlconf/slash')
|
||||
request.urlconf = 'middleware.extra_urls'
|
||||
request.method = 'POST'
|
||||
with six.assertRaisesRegex(self, RuntimeError, 'end in a slash'):
|
||||
@ -189,7 +183,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
"""
|
||||
Tests disabling append slash functionality.
|
||||
"""
|
||||
request = self._get_request('customurlconf/slash')
|
||||
request = self.rf.get('/customurlconf/slash')
|
||||
request.urlconf = 'middleware.extra_urls'
|
||||
self.assertEqual(CommonMiddleware().process_request(request), None)
|
||||
|
||||
@ -199,7 +193,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
Tests that URLs which require quoting are redirected to their slash
|
||||
version ok.
|
||||
"""
|
||||
request = self._get_request('customurlconf/needsquoting#')
|
||||
request = self.rf.get(quote('/customurlconf/needsquoting#'))
|
||||
request.urlconf = 'middleware.extra_urls'
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertIsNotNone(r,
|
||||
@ -211,7 +205,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
|
||||
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
|
||||
def test_prepend_www_custom_urlconf(self):
|
||||
request = self._get_request('customurlconf/path/')
|
||||
request = self.rf.get('/customurlconf/path/')
|
||||
request.urlconf = 'middleware.extra_urls'
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
@ -221,7 +215,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
|
||||
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
||||
def test_prepend_www_append_slash_have_slash_custom_urlconf(self):
|
||||
request = self._get_request('customurlconf/slash/')
|
||||
request = self.rf.get('/customurlconf/slash/')
|
||||
request.urlconf = 'middleware.extra_urls'
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
@ -230,7 +224,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
|
||||
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
||||
def test_prepend_www_append_slash_slashless_custom_urlconf(self):
|
||||
request = self._get_request('customurlconf/slash')
|
||||
request = self.rf.get('/customurlconf/slash')
|
||||
request.urlconf = 'middleware.extra_urls'
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
@ -242,7 +236,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
@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 = self.rf.get('/slash')
|
||||
request.META['HTTP_USER_AGENT'] = 'foo'
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 403)
|
||||
@ -250,13 +244,13 @@ class CommonMiddlewareTest(TestCase):
|
||||
|
||||
def test_non_ascii_query_string_does_not_crash(self):
|
||||
"""Regression test for #15152"""
|
||||
request = self._get_request('slash')
|
||||
request = self.rf.get('/slash')
|
||||
request.META['QUERY_STRING'] = force_str('drink=café')
|
||||
response = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(response.status_code, 301)
|
||||
|
||||
def test_response_redirect_class(self):
|
||||
request = self._get_request('slash')
|
||||
request = self.rf.get('/slash')
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(r.url, 'http://testserver/slash/')
|
||||
@ -266,7 +260,7 @@ class CommonMiddlewareTest(TestCase):
|
||||
class MyCommonMiddleware(CommonMiddleware):
|
||||
response_redirect_class = HttpResponseRedirect
|
||||
|
||||
request = self._get_request('slash')
|
||||
request = self.rf.get('/slash')
|
||||
r = MyCommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 302)
|
||||
self.assertEqual(r.url, 'http://testserver/slash/')
|
||||
@ -279,13 +273,10 @@ class CommonMiddlewareTest(TestCase):
|
||||
)
|
||||
class BrokenLinkEmailsMiddlewareTest(TestCase):
|
||||
|
||||
rf = RequestFactory()
|
||||
|
||||
def setUp(self):
|
||||
self.req = HttpRequest()
|
||||
self.req.META = {
|
||||
'SERVER_NAME': 'testserver',
|
||||
'SERVER_PORT': 80,
|
||||
}
|
||||
self.req.path = self.req.path_info = 'regular_url/that/does/not/exist'
|
||||
self.req = self.rf.get('/regular_url/that/does/not/exist')
|
||||
self.resp = self.client.get(self.req.path)
|
||||
|
||||
def test_404_error_reporting(self):
|
||||
@ -337,13 +328,8 @@ class BrokenLinkEmailsMiddlewareTest(TestCase):
|
||||
class ConditionalGetMiddlewareTest(TestCase):
|
||||
|
||||
def setUp(self):
|
||||
self.req = HttpRequest()
|
||||
self.req.META = {
|
||||
'SERVER_NAME': 'testserver',
|
||||
'SERVER_PORT': 80,
|
||||
}
|
||||
self.req.path = self.req.path_info = "/"
|
||||
self.resp = self.client.get(self.req.path)
|
||||
self.req = RequestFactory().get('/')
|
||||
self.resp = self.client.get(self.req.path_info)
|
||||
|
||||
# Tests for the Date header
|
||||
|
||||
@ -606,12 +592,7 @@ class GZipMiddlewareTest(TestCase):
|
||||
sequence = [b'a' * 500, b'b' * 200, b'a' * 300]
|
||||
|
||||
def setUp(self):
|
||||
self.req = HttpRequest()
|
||||
self.req.META = {
|
||||
'SERVER_NAME': 'testserver',
|
||||
'SERVER_PORT': 80,
|
||||
}
|
||||
self.req.path = self.req.path_info = "/"
|
||||
self.req = RequestFactory().get('/')
|
||||
self.req.META['HTTP_ACCEPT_ENCODING'] = 'gzip, deflate'
|
||||
self.req.META['HTTP_USER_AGENT'] = 'Mozilla/5.0 (Windows NT 5.1; rv:9.0.1) Gecko/20100101 Firefox/9.0.1'
|
||||
self.resp = HttpResponse()
|
||||
@ -686,11 +667,9 @@ class ETagGZipMiddlewareTest(TestCase):
|
||||
"""
|
||||
Tests if the ETag middleware behaves correctly with GZip middleware.
|
||||
"""
|
||||
rf = RequestFactory()
|
||||
compressible_string = b'a' * 500
|
||||
|
||||
def setUp(self):
|
||||
self.rf = RequestFactory()
|
||||
|
||||
def test_compress_response(self):
|
||||
"""
|
||||
Tests that ETag is changed after gzip compression is performed.
|
||||
|
Loading…
Reference in New Issue
Block a user