Merge pull request #1180 from sigmavirus24/master
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix Session level Cookie Handling
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 1da1490, 2e31696 · Browse files · 3 files·+21 added−1 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+21 added−1 removed
- other:+21 added−1 removed
Provenance
- Commit
5d9fcc7- Tree
22a0c0a- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
1da1490- Parent 2
2e31696
3 files changed+21 added−1 removed
modified:requests/cookies.py+6 added
238 unmodified lines239 def __getitem__(self, name):240 """Dict-like __getitem__() for compatibility with client code. Throws exception241 if there are more than one cookie with name. In that case, use the more242 explicit get() method instead. Caution: operation is O(n), not O(1)."""243+ if isinstance(name, cookielib.Cookie):244+ name = name.name245+246 return self._find_no_duplicates(name)247 248 def __setitem__(self, name, value):249 """Dict-like __setitem__ for compatibility with client code. Throws exception250 if there is already a cookie of that name in the jar. In that case, use the more251 explicit set() method instead."""252+ if isinstance(name, cookielib.Cookie):253+ name = name.name254+255 self.set(name, value)256 257 def __delitem__(self, name):258 """Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_cookie_by_name()."""121 unmodified linesmodified:requests/sessions.py+9 added-1 removed
83 unmodified lines84 prepared_request.headers = req.headers.copy()85 prepared_request.hooks = req.hooks86 prepared_request.method = req.method87 prepared_request.url = req.url88+ cookiejar = resp.cookies89 90 # ((resp.status_code is codes.see_other))91 while (('location' in resp.headers and resp.status_code in REDIRECT_STATI)):92 38 unmodified lines131 del prepared_request.headers['Content-Length']132 133 prepared_request.body = None134 135+ headers = prepared_request.headers136 try:135- del prepared_request.headers['Cookie']137+ del headers['Cookie']138 except KeyError:139 pass140 141+ prepared_request.prepare_cookies(cookiejar)142+143 resp = self.send(144 prepared_request,145 stream=stream,146 timeout=timeout,2 unmodified lines149 proxies=proxies,150 allow_redirects=False,151 )152 153+ cookiejar.update(resp.cookies)154+155 i += 1156 yield resp157 158+ resp.cookies.update(cookiejar)159 160+161 class Session(SessionRedirectMixin):162 """A Requests session.163 164 Provides cookie persistience, connection-pooling, and configuration.299 unmodified linesmodified:test_requests.py+6 added
109 unmodified lines110 111 r = requests.get(httpbin('get') + '?test=true', params={'q': 'test'}, headers=heads)112 self.assertEqual(r.status_code, 200)113 114+ def test_set_cookie_on_301(self):115+ s = requests.session()116+ url = httpbin('cookies/set?foo=bar')117+ r = s.get(url)118+ self.assertTrue(s.cookies['foo'] == 'bar')119+120 def test_user_agent_transfers(self):121 122 heads = {123 'User-agent': 'Mozilla/5.0 (github.com/kennethreitz/requests)'232 unmodified lines