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

Skip default headers #486

Merged
merged 11 commits into from
Sep 3, 2015
Prev Previous commit
Next Next commit
Merge passed and default skip lists for client headers
  • Loading branch information
asvetlov committed Aug 29, 2015
commit 0b17644a24dfc2febaf05ffaeddfe42dd102aee4
10 changes: 8 additions & 2 deletions aiohttp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ def __init__(self, *, connector=None, loop=None, cookies=None,
headers = CIMultiDict()
self._default_headers = headers
if skip_auto_headers is not None:
self._skip_auto_headers = frozenset(skip_auto_headers)
self._skip_auto_headers = frozenset([upstr(i)
for i in skip_auto_headers])
else:
self._skip_auto_headers = frozenset()

Expand Down Expand Up @@ -125,10 +126,15 @@ def request(self, method, url, *,
raise ValueError("Can't combine `Authorization` header with "
"`auth` argument")

skip_headers = self._skip_auto_headers
if skip_auto_headers is not None:
for i in self.skip_auto_headers:
skip_headers.add(upstr(i))

while True:
req = self._request_class(
method, url, params=params, headers=headers,
skip_auto_headers=skip_auto_headers, data=data,
skip_auto_headers=skip_headers, data=data,
cookies=self.cookies, files=files, encoding=encoding,
auth=auth, version=version, compress=compress, chunked=chunked,
expect100=expect100,
Expand Down
32 changes: 29 additions & 3 deletions tests/test_client_session.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

import aiohttp
from aiohttp.client import ClientSession
from aiohttp.multidict import MultiDict, CIMultiDict, CIMultiDictProxy
from aiohttp.multidict import MultiDict, CIMultiDict, CIMultiDictProxy, upstr
from aiohttp.connector import BaseConnector, TCPConnector
from aiohttp.client_reqrep import ClientRequest, ClientResponse
from http.cookies import SimpleCookie
Expand Down Expand Up @@ -141,8 +141,8 @@ def test_merge_headers_with_list_of_tuples(self):
]))
session.close()

def _make_one(self):
session = ClientSession(loop=self.loop)
def _make_one(self, **kwargs):
session = ClientSession(loop=self.loop, **kwargs)
params = dict(
headers={"Authorization": "Basic ..."},
max_redirects=2,
Expand Down Expand Up @@ -375,6 +375,32 @@ def test_borrow_connector_loop(self):
session = ClientSession(connector=conn)
self.assertIs(session._loop, self.loop)

@mock.patch("aiohttp.client.ClientSession.request")
def test_skip_auto_headers(self, patched):
session, params = self._make_one()
skip = [upstr('user-agent')]
self.run(session.get("http://example.com",
skip_auto_headers=skip,
**params))
params['skip_auto_headers'] = skip
self.assertTrue(patched.called, "`ClientSession.request` not called")
self.assertEqual(patched.call_args[1]['skip_auto_headers'], skip)
session.close()

@mock.patch("aiohttp.client.ClientSession.request")
def test_skip_auto_headers_default(self, patched):
skip = ['user-agent']
uskip = [upstr(i) for i in skip]
import ipdb;ipdb.set_trace()
session, params = self._make_one(skip_auto_headers=skip)
self.run(session.get("http://example.com",
**params))
params['skip_auto_headers'] = skip
self.assertTrue(patched.called, "`ClientSession.request` not called")
print(patched.call_args)
self.assertEqual(patched.call_args[1]['skip_auto_headers'], uskip)
session.close()


class TestCLientRequest(unittest.TestCase):

Expand Down