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 = CompressionMiddleware().process_response(self.req,
                                                  self.stream_resp)
     self.assertEqual(brotli.decompress(b''.join(r)),
                      b''.join(self.sequence))
     self.assertEqual(r.get('Content-Encoding'), 'br')
     self.assertFalse(r.has_header('Content-Length'))
     self.assertEqual(r.get('Vary'), 'Accept-Encoding')
 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_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_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_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('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):
     """
     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)