Skip to content

Commit

Permalink
Fix #2284: Clone web request's state
Browse files Browse the repository at this point in the history
  • Loading branch information
asvetlov committed Sep 23, 2017
1 parent 98bc1e2 commit 4dd1621
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
10 changes: 7 additions & 3 deletions aiohttp/web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ class BaseRequest(collections.MutableMapping, HeadersMixin):

def __init__(self, message, payload, protocol, writer, time_service, task,
loop,
*, secure_proxy_ssl_header=None, client_max_size=1024**2):
*, secure_proxy_ssl_header=None, client_max_size=1024**2,
state=None):
if state is None:
state = {}
self._message = message
self._protocol = protocol
self._transport = protocol.transport
Expand All @@ -78,7 +81,7 @@ def __init__(self, message, payload, protocol, writer, time_service, task,

self._secure_proxy_ssl_header = secure_proxy_ssl_header
self._time_service = time_service
self._state = {}
self._state = state
self._cache = {}
self._task = task
self._client_max_size = client_max_size
Expand Down Expand Up @@ -120,7 +123,8 @@ def clone(self, *, method=sentinel, rel_url=sentinel,
self._time_service,
self._task,
self._loop,
secure_proxy_ssl_header=self._secure_proxy_ssl_header)
secure_proxy_ssl_header=self._secure_proxy_ssl_header,
state=self._state.copy())

@property
def task(self):
Expand Down
1 change: 1 addition & 0 deletions changes/2284.bugfix
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Properly clone state of web request
9 changes: 9 additions & 0 deletions tests/test_web_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,3 +620,12 @@ def test_remote_peername_forwarded_overrides_x_forwarded():
headers={'Forwarded': 'for=11.11.11.11, for=12.12.12.12',
'X-Forwarded-For': '13.13.13.13'})
assert req.remote == '11.11.11.11'


def test_save_state_on_clone():
req = make_mocked_request('GET', '/')
req['key'] = 'val'
req2 = req.clone()
req2['key'] = 'val2'
assert req['key'] == 'val'
assert req2['key'] == 'val2'

0 comments on commit 4dd1621

Please sign in to comment.