Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't add Content-Encoding and Transfer-Encoding if no body #891

Merged
merged 1 commit into from
Jun 3, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions aiohttp/client_reqrep.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def __init__(self, method, url, *,
self.update_headers(headers)
self.update_auto_headers(skip_auto_headers)
self.update_cookies(cookies)
self.update_content_encoding()
self.update_content_encoding(data)
self.update_auth(auth)

self.update_body_from_data(data, skip_auto_headers)
Expand Down Expand Up @@ -230,8 +230,11 @@ def update_cookies(self, cookies):

self.headers[hdrs.COOKIE] = c.output(header='', sep=';').strip()

def update_content_encoding(self):
def update_content_encoding(self, data):
"""Set request content encoding."""
if not data:
return

enc = self.headers.get(hdrs.CONTENT_ENCODING, '').lower()
if enc:
if self.compress is not False:
Expand Down
14 changes: 12 additions & 2 deletions tests/test_client_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,7 +528,7 @@ def test_bytes_data(self):

@unittest.mock.patch('aiohttp.client_reqrep.aiohttp')
def test_content_encoding(self, m_http):
req = ClientRequest('get', 'http://python.org/',
req = ClientRequest('get', 'http://python.org/', data='foo',
compress='deflate', loop=self.loop)
resp = req.send(self.transport, self.protocol)
self.assertEqual(req.headers['TRANSFER-ENCODING'], 'chunked')
Expand All @@ -538,10 +538,20 @@ def test_content_encoding(self, m_http):
self.loop.run_until_complete(req.close())
resp.close()

@unittest.mock.patch('aiohttp.client_reqrep.aiohttp')
def test_content_encoding_dont_set_headers_if_no_body(self, m_http):
req = ClientRequest('get', 'http://python.org/',
compress='deflate', loop=self.loop)
resp = req.send(self.transport, self.protocol)
self.assertNotIn('TRANSFER-ENCODING', req.headers)
self.assertNotIn('CONTENT-ENCODING', req.headers)
self.loop.run_until_complete(req.close())
resp.close()

@unittest.mock.patch('aiohttp.client_reqrep.aiohttp')
def test_content_encoding_header(self, m_http):
req = ClientRequest(
'get', 'http://python.org/',
'get', 'http://python.org/', data='foo',
headers={'Content-Encoding': 'deflate'}, loop=self.loop)
resp = req.send(self.transport, self.protocol)
self.assertEqual(req.headers['TRANSFER-ENCODING'], 'chunked')
Expand Down