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