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)))
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'))
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')
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'))
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')
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)
def test_compress_streaming_response_unicode(self): """ Compression is performed on responses with streaming Unicode content. """ r = CompressionMiddleware().process_response(self.req, self.stream_resp_unicode) self.assertEqual( brotli.decompress(b''.join(r)), b''.join(x.encode('utf-8') for x in self.sequence_unicode)) self.assertEqual(r.get('Content-Encoding'), 'br') self.assertFalse(r.has_header('Content-Length')) self.assertEqual(r.get('Vary'), 'Accept-Encoding')
def test_middleware_compress_response(self): fake_request = FakeRequestAcceptsBrotli() response_content = UTF8_LOREM_IPSUM_IN_CZECH fake_response = FakeResponse(content=response_content) compression_middleware = CompressionMiddleware() response = compression_middleware.process_response( fake_request, fake_response) decompressed_response = brotli.decompress( response.content) # type: bytes self.assertEqual(response_content, decompressed_response.decode(encoding='utf-8')) self.assertEqual(response.get('Vary'), 'Accept-Encoding')
def test_middleware_wont_compress_response_if_response_is_small(self): fake_request = FakeRequestAcceptsBrotli() response_content = "Hello World" self.assertLess(len(response_content), 200) # a < b fake_response = FakeResponse(content=response_content) compression_middleware = CompressionMiddleware() response = compression_middleware.process_response( fake_request, fake_response) self.assertEqual(response_content, response.content.decode(encoding='utf-8')) self.assertFalse(response.has_header('Vary'))
def test_middleware_wont_compress_if_client_not_accept(self): fake_request = FakeLegacyRequest() response_content = UTF8_LOREM_IPSUM_IN_CZECH fake_response = FakeResponse(content=response_content) compression_middleware = CompressionMiddleware() response = compression_middleware.process_response( fake_request, fake_response) django_gzip_middleware = GZipMiddleware() gzip_response = django_gzip_middleware.process_response( fake_request, fake_response) self.assertEqual(response_content, response.content.decode(encoding='utf-8')) self.assertEqual(response.get('Vary'), 'Accept-Encoding')
def test_middleware_wont_compress_if_response_is_already_compressed(self): fake_request = FakeRequestAcceptsBrotli() response_content = UTF8_LOREM_IPSUM_IN_CZECH fake_response = FakeResponse(content=response_content) compression_middleware = CompressionMiddleware() django_gzip_middleware = GZipMiddleware() gzip_response = django_gzip_middleware.process_response( fake_request, fake_response) response = compression_middleware.process_response( fake_request, gzip_response) self.assertEqual( response_content, gzip_decompress(response.content).decode(encoding='utf-8')) self.assertEqual(response.get('Vary'), 'Accept-Encoding')
def test_etag_is_updated_if_present(self): fake_request = FakeRequestAcceptsBrotli() response_content = UTF8_LOREM_IPSUM_IN_CZECH * 5 fake_etag_content = "\"foo\"" fake_response = FakeResponse(content=response_content, headers={"ETag": fake_etag_content}) self.assertEqual(fake_response['ETag'], fake_etag_content) compression_middleware = CompressionMiddleware() response = compression_middleware.process_response( fake_request, fake_response) decompressed_response = brotli.decompress( response.content) # type: bytes self.assertEqual(response_content, decompressed_response.decode(encoding='utf-8')) # note: this is where we differ from django-brotli # django-brotli's expectation: ### self.assertEqual(response['ETag'], '"foo;br\\"') # Django's expectation: self.assertEqual(response['ETag'], 'W/"foo"')