diff --git a/docs/server.rst b/docs/server.rst index 9abe3e38a07..87a88a0254f 100644 --- a/docs/server.rst +++ b/docs/server.rst @@ -85,6 +85,19 @@ params. However aiohttp does provide a nice print("Passed in GET", get_params) +Sending pre-compressed data +--------------------------- + +To include data in the response that is already compressed, do not call +`enable_compression`. Instead, set the `Content-Encoding` header explicitly: + + @asyncio.coroutine + def handler(request): + headers = {'Content-Encoding': 'gzip'} + deflated_data = zlib.compress(b'mydata') + return web.Response(body=deflated_data, headers=headers) + + Handling POST data ------------------ diff --git a/tests/test_web_functional.py b/tests/test_web_functional.py index 3ed8adb920b..f9463b6e60d 100644 --- a/tests/test_web_functional.py +++ b/tests/test_web_functional.py @@ -5,6 +5,7 @@ import os.path import socket import unittest +import zlib from multidict import MultiDict from aiohttp import log, web, request, FormData, ClientSession, TCPConnector from aiohttp.protocol import HttpVersion, HttpVersion10, HttpVersion11 @@ -792,6 +793,25 @@ def go(): self.loop.run_until_complete(go()) + def test_response_with_precompressed_body(self): + @asyncio.coroutine + def handler(request): + headers = {'Content-Encoding': 'gzip'} + deflated_data = zlib.compress(b'mydata') + return web.Response(body=deflated_data, headers=headers) + + @asyncio.coroutine + def go(): + _, srv, url = yield from self.create_server('GET', '/', handler) + client = ClientSession(loop=self.loop) + resp = yield from client.get(url) + self.assertEqual(200, resp.status) + data = yield from resp.read() + self.assertEqual(b'mydata', data) + self.assertEqual(resp.headers.get('CONTENT-ENCODING'), 'deflate') + yield from resp.release() + client.close() + def test_stream_response_multiple_chunks(self): @asyncio.coroutine def handler(request):