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"')