Made cosmetic cleanups in middleware tests.
This commit is contained in:
parent
f14ab700c3
commit
89616f0c79
|
@ -93,15 +93,15 @@ class CommonMiddlewareTest(SimpleTestCase):
|
|||
request = self.rf.get('/slash')
|
||||
request.method = 'POST'
|
||||
response = HttpResponseNotFound()
|
||||
with six.assertRaisesRegex(self, RuntimeError, msg % request.method):
|
||||
with self.assertRaisesMessage(RuntimeError, msg % request.method):
|
||||
CommonMiddleware().process_response(request, response)
|
||||
request = self.rf.get('/slash')
|
||||
request.method = 'PUT'
|
||||
with six.assertRaisesRegex(self, RuntimeError, msg % request.method):
|
||||
with self.assertRaisesMessage(RuntimeError, msg % request.method):
|
||||
CommonMiddleware().process_response(request, response)
|
||||
request = self.rf.get('/slash')
|
||||
request.method = 'PATCH'
|
||||
with six.assertRaisesRegex(self, RuntimeError, msg % request.method):
|
||||
with self.assertRaisesMessage(RuntimeError, msg % request.method):
|
||||
CommonMiddleware().process_response(request, response)
|
||||
|
||||
@override_settings(APPEND_SLASH=False)
|
||||
|
@ -116,40 +116,34 @@ class CommonMiddlewareTest(SimpleTestCase):
|
|||
@override_settings(APPEND_SLASH=True)
|
||||
def test_append_slash_quoted(self):
|
||||
"""
|
||||
URLs which require quoting should be redirected to their slash version ok.
|
||||
URLs which require quoting should be redirected to their slash version.
|
||||
"""
|
||||
request = self.rf.get(quote('/needsquoting#'))
|
||||
response = HttpResponseNotFound()
|
||||
r = CommonMiddleware().process_response(request, response)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(
|
||||
r.url,
|
||||
'/needsquoting%23/')
|
||||
self.assertEqual(r.url, '/needsquoting%23/')
|
||||
|
||||
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
|
||||
def test_prepend_www(self):
|
||||
request = self.rf.get('/path/')
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(
|
||||
r.url,
|
||||
'http://www.testserver/path/')
|
||||
self.assertEqual(r.url, 'http://www.testserver/path/')
|
||||
|
||||
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
||||
def test_prepend_www_append_slash_have_slash(self):
|
||||
request = self.rf.get('/slash/')
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(r.url,
|
||||
'http://www.testserver/slash/')
|
||||
self.assertEqual(r.url, 'http://www.testserver/slash/')
|
||||
|
||||
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
||||
def test_prepend_www_append_slash_slashless(self):
|
||||
request = self.rf.get('/slash')
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(r.url,
|
||||
'http://www.testserver/slash/')
|
||||
self.assertEqual(r.url, 'http://www.testserver/slash/')
|
||||
|
||||
# The following tests examine expected behavior given a custom URLconf that
|
||||
# overrides the default one through the request object.
|
||||
|
@ -196,8 +190,7 @@ class CommonMiddlewareTest(SimpleTestCase):
|
|||
request.urlconf = 'middleware.extra_urls'
|
||||
response = HttpResponseNotFound()
|
||||
r = CommonMiddleware().process_response(request, response)
|
||||
self.assertIsNotNone(r,
|
||||
"CommonMiddleware failed to return APPEND_SLASH redirect using request.urlconf")
|
||||
self.assertIsNotNone(r, "CommonMiddleware failed to return APPEND_SLASH redirect using request.urlconf")
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(r.url, '/customurlconf/slash/')
|
||||
|
||||
|
@ -212,7 +205,7 @@ class CommonMiddlewareTest(SimpleTestCase):
|
|||
request.urlconf = 'middleware.extra_urls'
|
||||
request.method = 'POST'
|
||||
response = HttpResponseNotFound()
|
||||
with six.assertRaisesRegex(self, RuntimeError, 'end in a slash'):
|
||||
with self.assertRaisesMessage(RuntimeError, 'end in a slash'):
|
||||
CommonMiddleware().process_response(request, response)
|
||||
|
||||
@override_settings(APPEND_SLASH=False)
|
||||
|
@ -229,18 +222,15 @@ class CommonMiddlewareTest(SimpleTestCase):
|
|||
@override_settings(APPEND_SLASH=True)
|
||||
def test_append_slash_quoted_custom_urlconf(self):
|
||||
"""
|
||||
URLs which require quoting should be redirected to their slash version ok.
|
||||
URLs which require quoting should be redirected to their slash version.
|
||||
"""
|
||||
request = self.rf.get(quote('/customurlconf/needsquoting#'))
|
||||
request.urlconf = 'middleware.extra_urls'
|
||||
response = HttpResponseNotFound()
|
||||
r = CommonMiddleware().process_response(request, response)
|
||||
self.assertIsNotNone(r,
|
||||
"CommonMiddleware failed to return APPEND_SLASH redirect using request.urlconf")
|
||||
self.assertIsNotNone(r, "CommonMiddleware failed to return APPEND_SLASH redirect using request.urlconf")
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(
|
||||
r.url,
|
||||
'/customurlconf/needsquoting%23/')
|
||||
self.assertEqual(r.url, '/customurlconf/needsquoting%23/')
|
||||
|
||||
@override_settings(APPEND_SLASH=False, PREPEND_WWW=True)
|
||||
def test_prepend_www_custom_urlconf(self):
|
||||
|
@ -248,9 +238,7 @@ class CommonMiddlewareTest(SimpleTestCase):
|
|||
request.urlconf = 'middleware.extra_urls'
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(
|
||||
r.url,
|
||||
'http://www.testserver/customurlconf/path/')
|
||||
self.assertEqual(r.url, 'http://www.testserver/customurlconf/path/')
|
||||
|
||||
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
||||
def test_prepend_www_append_slash_have_slash_custom_urlconf(self):
|
||||
|
@ -258,8 +246,7 @@ class CommonMiddlewareTest(SimpleTestCase):
|
|||
request.urlconf = 'middleware.extra_urls'
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(r.url,
|
||||
'http://www.testserver/customurlconf/slash/')
|
||||
self.assertEqual(r.url, 'http://www.testserver/customurlconf/slash/')
|
||||
|
||||
@override_settings(APPEND_SLASH=True, PREPEND_WWW=True)
|
||||
def test_prepend_www_append_slash_slashless_custom_urlconf(self):
|
||||
|
@ -267,8 +254,7 @@ class CommonMiddlewareTest(SimpleTestCase):
|
|||
request.urlconf = 'middleware.extra_urls'
|
||||
r = CommonMiddleware().process_request(request)
|
||||
self.assertEqual(r.status_code, 301)
|
||||
self.assertEqual(r.url,
|
||||
'http://www.testserver/customurlconf/slash/')
|
||||
self.assertEqual(r.url, 'http://www.testserver/customurlconf/slash/')
|
||||
|
||||
# ETag + If-Not-Modified support tests
|
||||
|
||||
|
@ -387,16 +373,14 @@ class BrokenLinkEmailsMiddlewareTest(SimpleTestCase):
|
|||
|
||||
def test_custom_request_checker(self):
|
||||
class SubclassedMiddleware(BrokenLinkEmailsMiddleware):
|
||||
ignored_user_agent_patterns = (re.compile(r'Spider.*'),
|
||||
re.compile(r'Robot.*'))
|
||||
ignored_user_agent_patterns = (re.compile(r'Spider.*'), re.compile(r'Robot.*'))
|
||||
|
||||
def is_ignorable_request(self, request, uri, domain, referer):
|
||||
'''Check user-agent in addition to normal checks.'''
|
||||
if super(SubclassedMiddleware, self).is_ignorable_request(request, uri, domain, referer):
|
||||
return True
|
||||
user_agent = request.META['HTTP_USER_AGENT']
|
||||
return any(pattern.search(user_agent) for pattern in
|
||||
self.ignored_user_agent_patterns)
|
||||
return any(pattern.search(user_agent) for pattern in self.ignored_user_agent_patterns)
|
||||
|
||||
self.req.META['HTTP_REFERER'] = '/another/url/'
|
||||
self.req.META['HTTP_USER_AGENT'] = 'Spider machine 3.4'
|
||||
|
@ -558,88 +542,78 @@ class XFrameOptionsMiddlewareTest(SimpleTestCase):
|
|||
|
||||
def test_same_origin(self):
|
||||
"""
|
||||
Tests that the X_FRAME_OPTIONS setting can be set to SAMEORIGIN to
|
||||
have the middleware use that value for the HTTP header.
|
||||
The X_FRAME_OPTIONS setting can be set to SAMEORIGIN to have the
|
||||
middleware use that value for the HTTP header.
|
||||
"""
|
||||
with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
||||
HttpResponse())
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
|
||||
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
||||
|
||||
with override_settings(X_FRAME_OPTIONS='sameorigin'):
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
||||
HttpResponse())
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
|
||||
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
||||
|
||||
def test_deny(self):
|
||||
"""
|
||||
Tests that the X_FRAME_OPTIONS setting can be set to DENY to
|
||||
have the middleware use that value for the HTTP header.
|
||||
The X_FRAME_OPTIONS setting can be set to DENY to have the middleware
|
||||
use that value for the HTTP header.
|
||||
"""
|
||||
with override_settings(X_FRAME_OPTIONS='DENY'):
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
||||
HttpResponse())
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
|
||||
self.assertEqual(r['X-Frame-Options'], 'DENY')
|
||||
|
||||
with override_settings(X_FRAME_OPTIONS='deny'):
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
||||
HttpResponse())
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
|
||||
self.assertEqual(r['X-Frame-Options'], 'DENY')
|
||||
|
||||
def test_defaults_sameorigin(self):
|
||||
"""
|
||||
Tests that if the X_FRAME_OPTIONS setting is not set then it defaults
|
||||
to SAMEORIGIN.
|
||||
If the X_FRAME_OPTIONS setting is not set then it defaults to
|
||||
SAMEORIGIN.
|
||||
"""
|
||||
with override_settings(X_FRAME_OPTIONS=None):
|
||||
del settings.X_FRAME_OPTIONS # restored by override_settings
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
||||
HttpResponse())
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
|
||||
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
||||
|
||||
def test_dont_set_if_set(self):
|
||||
"""
|
||||
Tests that if the X-Frame-Options header is already set then the
|
||||
middleware does not attempt to override it.
|
||||
If the X-Frame-Options header is already set then the middleware does
|
||||
not attempt to override it.
|
||||
"""
|
||||
with override_settings(X_FRAME_OPTIONS='DENY'):
|
||||
response = HttpResponse()
|
||||
response['X-Frame-Options'] = 'SAMEORIGIN'
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
||||
response)
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
|
||||
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
||||
|
||||
with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
|
||||
response = HttpResponse()
|
||||
response['X-Frame-Options'] = 'DENY'
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
||||
response)
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
|
||||
self.assertEqual(r['X-Frame-Options'], 'DENY')
|
||||
|
||||
def test_response_exempt(self):
|
||||
"""
|
||||
Tests that if the response has a xframe_options_exempt attribute set
|
||||
to False then it still sets the header, but if it's set to True then
|
||||
it does not.
|
||||
If the response has an xframe_options_exempt attribute set to False
|
||||
then it still sets the header, but if it's set to True then it doesn't.
|
||||
"""
|
||||
with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
|
||||
response = HttpResponse()
|
||||
response.xframe_options_exempt = False
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
||||
response)
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
|
||||
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
||||
|
||||
response = HttpResponse()
|
||||
response.xframe_options_exempt = True
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(),
|
||||
response)
|
||||
self.assertEqual(r.get('X-Frame-Options', None), None)
|
||||
r = XFrameOptionsMiddleware().process_response(HttpRequest(), response)
|
||||
self.assertIsNone(r.get('X-Frame-Options'))
|
||||
|
||||
def test_is_extendable(self):
|
||||
"""
|
||||
Tests that the XFrameOptionsMiddleware method that determines the
|
||||
X-Frame-Options header value can be overridden based on something in
|
||||
the request or response.
|
||||
The XFrameOptionsMiddleware method that determines the X-Frame-Options
|
||||
header value can be overridden based on something in the request or
|
||||
response.
|
||||
"""
|
||||
class OtherXFrameOptionsMiddleware(XFrameOptionsMiddleware):
|
||||
# This is just an example for testing purposes...
|
||||
|
@ -653,29 +627,26 @@ class XFrameOptionsMiddlewareTest(SimpleTestCase):
|
|||
with override_settings(X_FRAME_OPTIONS='DENY'):
|
||||
response = HttpResponse()
|
||||
response.sameorigin = True
|
||||
r = OtherXFrameOptionsMiddleware().process_response(HttpRequest(),
|
||||
response)
|
||||
r = OtherXFrameOptionsMiddleware().process_response(HttpRequest(), response)
|
||||
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
||||
|
||||
request = HttpRequest()
|
||||
request.sameorigin = True
|
||||
r = OtherXFrameOptionsMiddleware().process_response(request,
|
||||
HttpResponse())
|
||||
r = OtherXFrameOptionsMiddleware().process_response(request, HttpResponse())
|
||||
self.assertEqual(r['X-Frame-Options'], 'SAMEORIGIN')
|
||||
|
||||
with override_settings(X_FRAME_OPTIONS='SAMEORIGIN'):
|
||||
r = OtherXFrameOptionsMiddleware().process_response(HttpRequest(),
|
||||
HttpResponse())
|
||||
r = OtherXFrameOptionsMiddleware().process_response(HttpRequest(), HttpResponse())
|
||||
self.assertEqual(r['X-Frame-Options'], 'DENY')
|
||||
|
||||
|
||||
class GZipMiddlewareTest(SimpleTestCase):
|
||||
"""
|
||||
Tests the GZip middleware.
|
||||
Tests the GZipMiddleware.
|
||||
"""
|
||||
short_string = b"This string is too short to be worth compressing."
|
||||
compressible_string = b'a' * 500
|
||||
uncompressible_string = b''.join(six.int2byte(random.randint(0, 255)) for _ in range(500))
|
||||
incompressible_string = b''.join(six.int2byte(random.randint(0, 255)) for _ in range(500))
|
||||
sequence = [b'a' * 500, b'b' * 200, b'a' * 300]
|
||||
sequence_unicode = ['a' * 500, 'é' * 200, 'a' * 300]
|
||||
|
||||
|
@ -699,7 +670,7 @@ class GZipMiddlewareTest(SimpleTestCase):
|
|||
|
||||
def test_compress_response(self):
|
||||
"""
|
||||
Tests that compression is performed on responses with compressible content.
|
||||
Compression is performed on responses with compressible content.
|
||||
"""
|
||||
r = GZipMiddleware().process_response(self.req, self.resp)
|
||||
self.assertEqual(self.decompress(r.content), self.compressible_string)
|
||||
|
@ -708,7 +679,7 @@ class GZipMiddlewareTest(SimpleTestCase):
|
|||
|
||||
def test_compress_streaming_response(self):
|
||||
"""
|
||||
Tests that compression is performed on responses with streaming content.
|
||||
Compression is performed on responses with streaming content.
|
||||
"""
|
||||
r = GZipMiddleware().process_response(self.req, self.stream_resp)
|
||||
self.assertEqual(self.decompress(b''.join(r)), b''.join(self.sequence))
|
||||
|
@ -717,16 +688,19 @@ class GZipMiddlewareTest(SimpleTestCase):
|
|||
|
||||
def test_compress_streaming_response_unicode(self):
|
||||
"""
|
||||
Tests that compression is performed on responses with streaming Unicode content.
|
||||
Compression is performed on responses with streaming Unicode content.
|
||||
"""
|
||||
r = GZipMiddleware().process_response(self.req, self.stream_resp_unicode)
|
||||
self.assertEqual(self.decompress(b''.join(r)), b''.join(x.encode('utf-8') for x in self.sequence_unicode))
|
||||
self.assertEqual(
|
||||
self.decompress(b''.join(r)),
|
||||
b''.join(x.encode('utf-8') for x in self.sequence_unicode)
|
||||
)
|
||||
self.assertEqual(r.get('Content-Encoding'), 'gzip')
|
||||
self.assertFalse(r.has_header('Content-Length'))
|
||||
|
||||
def test_compress_file_response(self):
|
||||
"""
|
||||
Tests that compression is performed on FileResponse.
|
||||
Compression is performed on FileResponse.
|
||||
"""
|
||||
open_file = lambda: open(__file__, 'rb')
|
||||
with open_file() as file1:
|
||||
|
@ -740,8 +714,8 @@ class GZipMiddlewareTest(SimpleTestCase):
|
|||
|
||||
def test_compress_non_200_response(self):
|
||||
"""
|
||||
Tests that compression is performed on responses with a status other than 200.
|
||||
See #10762.
|
||||
Compression is performed on responses with a status other than 200
|
||||
(#10762).
|
||||
"""
|
||||
self.resp.status_code = 404
|
||||
r = GZipMiddleware().process_response(self.req, self.resp)
|
||||
|
@ -750,54 +724,52 @@ class GZipMiddlewareTest(SimpleTestCase):
|
|||
|
||||
def test_no_compress_short_response(self):
|
||||
"""
|
||||
Tests that compression isn't performed on responses with short content.
|
||||
Compression isn't performed on responses with short content.
|
||||
"""
|
||||
self.resp.content = self.short_string
|
||||
r = GZipMiddleware().process_response(self.req, self.resp)
|
||||
self.assertEqual(r.content, self.short_string)
|
||||
self.assertEqual(r.get('Content-Encoding'), None)
|
||||
self.assertIsNone(r.get('Content-Encoding'))
|
||||
|
||||
def test_no_compress_compressed_response(self):
|
||||
"""
|
||||
Tests that compression isn't performed on responses that are already compressed.
|
||||
Compression isn't performed on responses that are already compressed.
|
||||
"""
|
||||
self.resp['Content-Encoding'] = 'deflate'
|
||||
r = GZipMiddleware().process_response(self.req, self.resp)
|
||||
self.assertEqual(r.content, self.compressible_string)
|
||||
self.assertEqual(r.get('Content-Encoding'), 'deflate')
|
||||
|
||||
def test_no_compress_uncompressible_response(self):
|
||||
def test_no_compress_incompressible_response(self):
|
||||
"""
|
||||
Tests that compression isn't performed on responses with uncompressible content.
|
||||
Compression isn't performed on responses with incompressible content.
|
||||
"""
|
||||
self.resp.content = self.uncompressible_string
|
||||
self.resp.content = self.incompressible_string
|
||||
r = GZipMiddleware().process_response(self.req, self.resp)
|
||||
self.assertEqual(r.content, self.uncompressible_string)
|
||||
self.assertEqual(r.get('Content-Encoding'), None)
|
||||
self.assertEqual(r.content, self.incompressible_string)
|
||||
self.assertIsNone(r.get('Content-Encoding'))
|
||||
|
||||
|
||||
@override_settings(USE_ETAGS=True)
|
||||
class ETagGZipMiddlewareTest(SimpleTestCase):
|
||||
"""
|
||||
Tests if the ETag middleware behaves correctly with GZip middleware.
|
||||
Tests if the ETagMiddleware behaves correctly with GZipMiddleware.
|
||||
"""
|
||||
rf = RequestFactory()
|
||||
compressible_string = b'a' * 500
|
||||
|
||||
def test_compress_response(self):
|
||||
"""
|
||||
Tests that ETag is changed after gzip compression is performed.
|
||||
ETag is changed after gzip compression is performed.
|
||||
"""
|
||||
request = self.rf.get('/', HTTP_ACCEPT_ENCODING='gzip, deflate')
|
||||
response = GZipMiddleware().process_response(request,
|
||||
CommonMiddleware().process_response(request,
|
||||
HttpResponse(self.compressible_string)))
|
||||
CommonMiddleware().process_response(request, HttpResponse(self.compressible_string)))
|
||||
gzip_etag = response.get('ETag')
|
||||
|
||||
request = self.rf.get('/', HTTP_ACCEPT_ENCODING='')
|
||||
response = GZipMiddleware().process_response(request,
|
||||
CommonMiddleware().process_response(request,
|
||||
HttpResponse(self.compressible_string)))
|
||||
CommonMiddleware().process_response(request, HttpResponse(self.compressible_string)))
|
||||
nogzip_etag = response.get('ETag')
|
||||
|
||||
self.assertNotEqual(gzip_etag, nogzip_etag)
|
||||
|
|
Loading…
Reference in New Issue