Skip to content

Commit

Permalink
Don't add Content-Encoding and Transfer-Encoding if no body
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel Nelson committed May 31, 2016
1 parent be9a084 commit be7bfb8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
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

0 comments on commit be7bfb8

Please sign in to comment.