Skip to content

Commit

Permalink
Merge pull request #595 from reclosedev/sess-cookies-no-resp
Browse files Browse the repository at this point in the history
Session cookies not saved when Session.request is called with return_response=False
  • Loading branch information
Kenneth Reitz committed May 8, 2012
2 parents 0308a28 + c010f58 commit 2d328d8
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,4 @@ Patches and Suggestions
- Miguel Turner
- Rohan Jain (crodjer)
- Justin Barber <barber.justin@gmail.com>
- Roman Haritonov <@reclosedev>
6 changes: 6 additions & 0 deletions requests/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,12 @@ def build(resp):
# Save cookies in Response.
response.cookies = self.cookies

# Save cookies in Session.
# (in safe mode, cookies may be None if the request didn't succeed)
if self.cookies is not None:
for cookie in self.cookies:
self.session.cookies.set_cookie(cookie)

# No exceptions were harmed in the making of this request.
response.error = getattr(resp, 'error', None)

Expand Down
6 changes: 0 additions & 6 deletions requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,12 +228,6 @@ def request(self, method, url,
# Send the HTTP Request.
r.send(prefetch=prefetch)

# Send any cookies back up the to the session.
# (in safe mode, cookies may be None if the request didn't succeed)
if r.response.cookies is not None:
for cookie in r.response.cookies:
self.cookies.set_cookie(cookie)

# Return the response.
return r.response

Expand Down
10 changes: 10 additions & 0 deletions tests/test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,16 @@ def test_session_persistent_params(self):
assert params3['b'] in r3.text
assert params3['c'] in r3.text

def test_session_cookies_with_return_response_false(self):
s = requests.session()
# return_response=False as it does requests.async.get
rq = get(httpbin('cookies', 'set', 'k', 'v'), return_response=False,
allow_redirects=True, session=s)
rq.send(prefetch=True)
c = rq.response.json.get('cookies')
assert 'k' in c
assert 'k' in s.cookies

def test_session_pickling(self):

s = requests.session(
Expand Down
10 changes: 10 additions & 0 deletions tests/test_requests_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
has_poll = hasattr(select, "poll")

from requests import async
import requests

sys.path.append('.')
from test_requests import httpbin, RequestsTestSuite, SERVICES
Expand Down Expand Up @@ -63,5 +64,14 @@ def test_select_poll(self):
"""Test to make sure we don't overwrite the poll"""
self.assertEqual(hasattr(select, "poll"), has_poll)

def test_async_with_session_cookies(self):
s = requests.Session(cookies={'initial': '42'})
r1 = get(httpbin('cookies/set/async/yes'), session=s)
r2 = get(httpbin('cookies/set/no_session/yes'))
assert 'initial' in r1.cookies
assert 'initial' not in r2.cookies and 'async' not in r2.cookies
assert 'async' in s.cookies
assert 'no_session' not in s.cookies

if __name__ == '__main__':
unittest.main()

0 comments on commit 2d328d8

Please sign in to comment.