From 19c80d6811336d226a260f955dabf330c3814b22 Mon Sep 17 00:00:00 2001 From: pfreixes Date: Sun, 24 Jul 2016 11:35:38 +0200 Subject: [PATCH] Avoid use mutable CIMultiDict kw param in make_mocked_request Even though is hard to get an issue, there is still chances to get unwished behaviours as a side effect because of that. --- CONTRIBUTORS.txt | 1 + aiohttp/test_utils.py | 17 ++++++++++++----- tests/test_web_request.py | 10 ++++++++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index d76b5dcebe7..c046b86db35 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -113,3 +113,4 @@ Yury Selivanov Yusuke Tsutsumi Марк Коренберг Семён Марьясин +Pau Freixes diff --git a/aiohttp/test_utils.py b/aiohttp/test_utils.py index 2c6aa17a684..825661ada83 100644 --- a/aiohttp/test_utils.py +++ b/aiohttp/test_utils.py @@ -561,7 +561,7 @@ def get_extra_info(key): _not_set = object() -def make_mocked_request(method, path, headers=CIMultiDict(), *, +def make_mocked_request(method, path, headers=None, *, version=HttpVersion(1, 1), closing=False, app=None, reader=_not_set, @@ -616,10 +616,17 @@ def make_mocked_request(method, path, headers=CIMultiDict(), *, if version < HttpVersion(1, 1): closing = True - message = RawRequestMessage(method, path, version, headers, - [(k.encode('utf-8'), v.encode('utf-8')) - for k, v in headers.items()], - closing, False) + + if headers: + hdrs = headers + raw_hdrs = [ + (k.encode('utf-8'), v.encode('utf-8')) for k, v in headers.items()] + else: + hdrs = CIMultiDict() + raw_hdrs = [] + + message = RawRequestMessage(method, path, version, hdrs, + raw_hdrs, closing, False) if app is None: app = _create_app_mock() diff --git a/tests/test_web_request.py b/tests/test_web_request.py index ff9692db54e..e15bd1b5c01 100644 --- a/tests/test_web_request.py +++ b/tests/test_web_request.py @@ -20,6 +20,8 @@ def test_ctor(make_request, warning): assert '/path/to?a=1&b=2' == req.path_qs assert '/path/to' == req.path assert 'a=1&b=2' == req.query_string + assert CIMultiDict() == req.headers + assert () == req.raw_headers get = req.GET assert MultiDict([('a', '1'), ('b', '2')]) == get @@ -29,18 +31,22 @@ def test_ctor(make_request, warning): assert req.keep_alive # just make sure that all lines of make_mocked_request covered + headers = CIMultiDict(FOO='bar') reader = mock.Mock() writer = mock.Mock() payload = mock.Mock() transport = mock.Mock() app = mock.Mock() - req = make_request('GET', '/path/to?a=1&b=2', writer=writer, reader=reader, - payload=payload, transport=transport, app=app) + req = make_request('GET', '/path/to?a=1&b=2', headers=headers, + writer=writer, reader=reader, payload=payload, + transport=transport, app=app) assert req.app is app assert req.content is payload assert req.transport is transport assert req._reader is reader assert req._writer is writer + assert req.headers == headers + assert req.raw_headers == ((b'FOO', b'bar'),) def test_doubleslashes(make_request):