本文整理匯總了Python中django.middleware.gzip.GZipMiddleware方法的典型用法代碼示例。如果您正苦於以下問題:Python gzip.GZipMiddleware方法的具體用法?Python gzip.GZipMiddleware怎麽用?Python gzip.GZipMiddleware使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類django.middleware.gzip
的用法示例。
在下文中一共展示了gzip.GZipMiddleware方法的11個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: gzip_action
# 需要導入模塊: from django.middleware import gzip [as 別名]
# 或者: from django.middleware.gzip import GZipMiddleware [as 別名]
def gzip_action(func):
gz = GZipMiddleware()
@wraps(func)
def wrapper(self, request, *args, **kwargs):
resp = func(self, request, *args, **kwargs)
return gz.process_response(request, resp)
if getattr(settings, 'GZIP_MODELS'):
return wrapper
return func
示例2: test_compress_response
# 需要導入模塊: from django.middleware import gzip [as 別名]
# 或者: from django.middleware.gzip import GZipMiddleware [as 別名]
def test_compress_response(self):
"""
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)
self.assertEqual(r.get('Content-Encoding'), 'gzip')
self.assertEqual(r.get('Content-Length'), str(len(r.content)))
示例3: test_compress_streaming_response
# 需要導入模塊: from django.middleware import gzip [as 別名]
# 或者: from django.middleware.gzip import GZipMiddleware [as 別名]
def test_compress_streaming_response(self):
"""
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))
self.assertEqual(r.get('Content-Encoding'), 'gzip')
self.assertFalse(r.has_header('Content-Length'))
示例4: test_compress_streaming_response_unicode
# 需要導入模塊: from django.middleware import gzip [as 別名]
# 或者: from django.middleware.gzip import GZipMiddleware [as 別名]
def test_compress_streaming_response_unicode(self):
"""
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() for x in self.sequence_unicode)
)
self.assertEqual(r.get('Content-Encoding'), 'gzip')
self.assertFalse(r.has_header('Content-Length'))
示例5: test_compress_file_response
# 需要導入模塊: from django.middleware import gzip [as 別名]
# 或者: from django.middleware.gzip import GZipMiddleware [as 別名]
def test_compress_file_response(self):
"""
Compression is performed on FileResponse.
"""
with open(__file__, 'rb') as file1:
file_resp = FileResponse(file1)
file_resp['Content-Type'] = 'text/html; charset=UTF-8'
r = GZipMiddleware().process_response(self.req, file_resp)
with open(__file__, 'rb') as file2:
self.assertEqual(self.decompress(b''.join(r)), file2.read())
self.assertEqual(r.get('Content-Encoding'), 'gzip')
self.assertIsNot(r.file_to_stream, file1)
示例6: test_compress_non_200_response
# 需要導入模塊: from django.middleware import gzip [as 別名]
# 或者: from django.middleware.gzip import GZipMiddleware [as 別名]
def test_compress_non_200_response(self):
"""
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)
self.assertEqual(self.decompress(r.content), self.compressible_string)
self.assertEqual(r.get('Content-Encoding'), 'gzip')
示例7: test_no_compress_compressed_response
# 需要導入模塊: from django.middleware import gzip [as 別名]
# 或者: from django.middleware.gzip import GZipMiddleware [as 別名]
def test_no_compress_compressed_response(self):
"""
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')
示例8: test_no_compress_incompressible_response
# 需要導入模塊: from django.middleware import gzip [as 別名]
# 或者: from django.middleware.gzip import GZipMiddleware [as 別名]
def test_no_compress_incompressible_response(self):
"""
Compression isn't performed on responses with incompressible content.
"""
self.resp.content = self.incompressible_string
r = GZipMiddleware().process_response(self.req, self.resp)
self.assertEqual(r.content, self.incompressible_string)
self.assertIsNone(r.get('Content-Encoding'))
示例9: test_compress_deterministic
# 需要導入模塊: from django.middleware import gzip [as 別名]
# 或者: from django.middleware.gzip import GZipMiddleware [as 別名]
def test_compress_deterministic(self):
"""
Compression results are the same for the same content and don't
include a modification time (since that would make the results
of compression non-deterministic and prevent
ConditionalGetMiddleware from recognizing conditional matches
on gzipped content).
"""
r1 = GZipMiddleware().process_response(self.req, self.resp)
r2 = GZipMiddleware().process_response(self.req, self.resp)
self.assertEqual(r1.content, r2.content)
self.assertEqual(self.get_mtime(r1.content), 0)
self.assertEqual(self.get_mtime(r2.content), 0)
示例10: test_strong_etag_modified
# 需要導入模塊: from django.middleware import gzip [as 別名]
# 或者: from django.middleware.gzip import GZipMiddleware [as 別名]
def test_strong_etag_modified(self):
"""
GZipMiddleware makes a strong ETag weak.
"""
request = self.rf.get('/', HTTP_ACCEPT_ENCODING='gzip, deflate')
response = HttpResponse(self.compressible_string)
response['ETag'] = '"eggs"'
gzip_response = GZipMiddleware().process_response(request, response)
self.assertEqual(gzip_response['ETag'], 'W/"eggs"')
示例11: test_weak_etag_not_modified
# 需要導入模塊: from django.middleware import gzip [as 別名]
# 或者: from django.middleware.gzip import GZipMiddleware [as 別名]
def test_weak_etag_not_modified(self):
"""
GZipMiddleware doesn't modify a weak ETag.
"""
request = self.rf.get('/', HTTP_ACCEPT_ENCODING='gzip, deflate')
response = HttpResponse(self.compressible_string)
response['ETag'] = 'W/"eggs"'
gzip_response = GZipMiddleware().process_response(request, response)
self.assertEqual(gzip_response['ETag'], 'W/"eggs"')