Skip to content

Commit

Permalink
Verify that server can send pre-compressed data
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed Jun 3, 2016
1 parent 5779f23 commit dbbc6db
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
13 changes: 13 additions & 0 deletions docs/server.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------------

Expand Down
20 changes: 20 additions & 0 deletions tests/test_web_functional.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down

0 comments on commit dbbc6db

Please sign in to comment.