From bf227639b548b4ca804b2313547c94981b82be98 Mon Sep 17 00:00:00 2001 From: Andrew Svetlov Date: Sun, 28 Feb 2016 23:12:10 +0200 Subject: [PATCH] Fix #758: Handle empty body with gzipped encoding --- aiohttp/protocol.py | 3 ++- aiohttp/web_reqrep.py | 4 ++++ tests/conftest.py | 6 ++++++ tests/test_client_functional.py | 19 +++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/aiohttp/protocol.py b/aiohttp/protocol.py index 2928f90aa7c..b70a9f8bd42 100644 --- a/aiohttp/protocol.py +++ b/aiohttp/protocol.py @@ -286,7 +286,8 @@ def __call__(self, out, buf): length = 8 # payload decompression wrapper - if self.compression and self.message.compression: + if (self.response_with_body and + self.compression and self.message.compression): out = DeflateBuffer(out, self.message.compression) # payload parser diff --git a/aiohttp/web_reqrep.py b/aiohttp/web_reqrep.py index d661029588d..f47fa32ee4c 100644 --- a/aiohttp/web_reqrep.py +++ b/aiohttp/web_reqrep.py @@ -482,6 +482,10 @@ def enable_compression(self, force=None): # Backwards compatibility for when force was a bool <0.17. if type(force) == bool: force = ContentCoding.deflate if force else ContentCoding.identity + elif force is not None: + assert isinstance(force, ContentCoding), ("force should one of " + "None, bool or " + "ContentEncoding") self._compression = True self._compression_force = force diff --git a/tests/conftest.py b/tests/conftest.py index 7e2e01bad01..822d091f6fa 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -222,6 +222,12 @@ def post(self, path, **kwargs): url = self._url + path return self._session.post(url, **kwargs) + def delete(self, path, **kwargs): + while path.startswith('/'): + path = path[1:] + url = self._url + path + return self._session.delete(url) + def ws_connect(self, path, **kwargs): while path.startswith('/'): path = path[1:] diff --git a/tests/test_client_functional.py b/tests/test_client_functional.py index dbb27d8e8ab..8cf5a3ebbcf 100644 --- a/tests/test_client_functional.py +++ b/tests/test_client_functional.py @@ -432,3 +432,22 @@ def handler(request): resp = yield from client.get('/', version=aiohttp.HttpVersion11) assert resp.status == 200 resp.close() + + +@pytest.mark.run_loop +def test_204_with_gzipped_content_encoding(create_app_and_client): + @asyncio.coroutine + def handler(request): + resp = web.StreamResponse(status=204) + resp.content_length = 0 + resp.content_type = 'application/json' + # resp.enable_compression(web.ContentCoding.gzip) + resp.headers['Content-Encoding'] = 'gzip' + yield from resp.prepare(request) + return resp + + app, client = yield from create_app_and_client() + app.router.add_route('DELETE', '/', handler) + resp = yield from client.delete('/') + assert resp.status == 204 + yield from resp.release()