def test_bails_if_content_encoding(self):
        request = pretend.stub()
        response = pretend.stub(
            headers={"Content-Encoding": "something"},
            vary=None,
        )

        compressor(request, response)
    def test_sets_vary(self, vary, expected):
        request = pretend.stub(accept_encoding=AcceptEncodingNoHeader())
        response = Response(body=b"foo")
        response.vary = vary

        compressor(request, response)

        assert set(response.vary) == expected
    def test_bails_if_content_encoding(self):
        request = pretend.stub()
        response = pretend.stub(
            headers={"Content-Encoding": "something"},
            vary=None,
        )

        compressor(request, response)
    def test_doesnt_compress_too_small(self):
        request = pretend.stub(accept_encoding=AcceptEncodingValidHeader("gzip"))
        response = Response(body=b"foo")

        compressor(request, response)

        assert response.content_encoding is None
        assert response.content_length == 3
        assert response.body == b"foo"
    def test_compresses_streaming(self):
        decompressed_body = b"foofoofoofoofoofoofoofoofoofoofoofoofoofoo"
        compressed_body = b"".join(list(gzip_app_iter([decompressed_body])))

        request = pretend.stub(accept_encoding=AcceptEncodingValidHeader("gzip"))
        response = Response(app_iter=iter([decompressed_body]))

        compressor(request, response)

        assert response.content_encoding == "gzip"
        assert response.content_length is None
        assert response.body == compressed_body
    def test_compresses_streaming_with_etag(self):
        decompressed_body = b"foofoofoofoofoofoofoofoofoofoofoofoofoofoo"
        compressed_body = b"".join(list(gzip_app_iter([decompressed_body])))

        request = pretend.stub(accept_encoding=Accept("gzip"))
        response = Response(app_iter=iter([decompressed_body]))
        response.etag = "foo"

        compressor(request, response)

        assert response.content_encoding == "gzip"
        assert response.content_length is None
        assert response.body == compressed_body
        assert response.etag == "rfbezwKUdGjz6VPWDLDTvA"
    def test_compresses_streaming_with_etag(self):
        decompressed_body = b"foofoofoofoofoofoofoofoofoofoofoofoofoofoo"
        compressed_body = b"".join(list(gzip_app_iter([decompressed_body])))

        request = pretend.stub(accept_encoding=Accept("gzip"))
        response = Response(app_iter=iter([decompressed_body]))
        response.etag = "foo"

        compressor(request, response)

        assert response.content_encoding == "gzip"
        assert response.content_length is None
        assert response.body == compressed_body
        assert response.etag == "rfbezwKUdGjz6VPWDLDTvA"
    def test_buffers_small_streaming(self):
        decompressed_body = b"foofoofoofoofoofoofoofoofoofoofoofoofoofoo"
        compressed_body = b"".join(list(gzip_app_iter([decompressed_body])))

        request = pretend.stub(accept_encoding=Accept("gzip"))
        response = Response(
            app_iter=iter([decompressed_body]),
            content_length=len(decompressed_body),
        )

        compressor(request, response)

        assert response.content_encoding == "gzip"
        assert response.content_length == len(compressed_body)
        assert response.body == compressed_body
    def test_buffers_small_streaming(self):
        decompressed_body = b"foofoofoofoofoofoofoofoofoofoofoofoofoofoo"
        compressed_body = b"".join(list(gzip_app_iter([decompressed_body])))

        request = pretend.stub(accept_encoding=Accept("gzip"))
        response = Response(
            app_iter=iter([decompressed_body]),
            content_length=len(decompressed_body),
        )

        compressor(request, response)

        assert response.content_encoding == "gzip"
        assert response.content_length == len(compressed_body)
        assert response.body == compressed_body
    def test_compresses_non_streaming(self):
        decompressed_body = b"foofoofoofoofoofoofoofoofoofoofoofoofoofoo"
        compressed_body = b"".join(list(gzip_app_iter([decompressed_body])))

        request = pretend.stub(accept_encoding=AcceptEncodingValidHeader("gzip"))
        response = Response(body=decompressed_body)
        response.md5_etag()

        original_etag = response.etag

        compressor(request, response)

        assert response.content_encoding == "gzip"
        assert response.content_length == len(compressed_body)
        assert response.body == compressed_body
        assert response.etag != original_etag
    def test_bails_if_vary(self, vary):
        request = pretend.stub()
        response = pretend.stub(vary=vary)

        compressor(request, response)