Skip to content

Commit dbbc6db

Browse files
committed
Verify that server can send pre-compressed data
1 parent 5779f23 commit dbbc6db

File tree

2 files changed

+33
-0
lines changed

2 files changed

+33
-0
lines changed

docs/server.rst

+13
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,19 @@ params. However aiohttp does provide a nice
8585
print("Passed in GET", get_params)
8686

8787

88+
Sending pre-compressed data
89+
---------------------------
90+
91+
To include data in the response that is already compressed, do not call
92+
`enable_compression`. Instead, set the `Content-Encoding` header explicitly:
93+
94+
@asyncio.coroutine
95+
def handler(request):
96+
headers = {'Content-Encoding': 'gzip'}
97+
deflated_data = zlib.compress(b'mydata')
98+
return web.Response(body=deflated_data, headers=headers)
99+
100+
88101
Handling POST data
89102
------------------
90103

tests/test_web_functional.py

+20
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import os.path
66
import socket
77
import unittest
8+
import zlib
89
from multidict import MultiDict
910
from aiohttp import log, web, request, FormData, ClientSession, TCPConnector
1011
from aiohttp.protocol import HttpVersion, HttpVersion10, HttpVersion11
@@ -792,6 +793,25 @@ def go():
792793

793794
self.loop.run_until_complete(go())
794795

796+
def test_response_with_precompressed_body(self):
797+
@asyncio.coroutine
798+
def handler(request):
799+
headers = {'Content-Encoding': 'gzip'}
800+
deflated_data = zlib.compress(b'mydata')
801+
return web.Response(body=deflated_data, headers=headers)
802+
803+
@asyncio.coroutine
804+
def go():
805+
_, srv, url = yield from self.create_server('GET', '/', handler)
806+
client = ClientSession(loop=self.loop)
807+
resp = yield from client.get(url)
808+
self.assertEqual(200, resp.status)
809+
data = yield from resp.read()
810+
self.assertEqual(b'mydata', data)
811+
self.assertEqual(resp.headers.get('CONTENT-ENCODING'), 'deflate')
812+
yield from resp.release()
813+
client.close()
814+
795815
def test_stream_response_multiple_chunks(self):
796816
@asyncio.coroutine
797817
def handler(request):

0 commit comments

Comments
 (0)