Skip to content
Myr

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

3 files+21 added−1 removed

modified:requests/cookies.py+6 added
238 unmodified lines
239 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.name245246 return self._find_no_duplicates(name)247248 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.name254255 self.set(name, value)256257 def __delitem__(self, name):258 """Deletes a cookie given a name. Wraps cookielib.CookieJar's remove_cookie_by_name()."""
121 unmodified lines
modified:requests/sessions.py+9 added-1 removed
83 unmodified lines
84 prepared_request.headers = req.headers.copy()85 prepared_request.hooks = req.hooks86 prepared_request.method = req.method87 prepared_request.url = req.url88 cookiejar = resp.cookies8990 # ((resp.status_code is codes.see_other))91 while (('location' in resp.headers and resp.status_code in REDIRECT_STATI)):92
38 unmodified lines
131 del prepared_request.headers['Content-Length']132133 prepared_request.body = None134135 headers = prepared_request.headers136 try:135 del prepared_request.headers['Cookie']137 del headers['Cookie']138 except KeyError:139 pass140141 prepared_request.prepare_cookies(cookiejar)142143 resp = self.send(144 prepared_request,145 stream=stream,146 timeout=timeout,
2 unmodified lines
149 proxies=proxies,150 allow_redirects=False,151 )152153 cookiejar.update(resp.cookies)154155 i += 1156 yield resp157158 resp.cookies.update(cookiejar)159160161class Session(SessionRedirectMixin):162 """A Requests session.163164 Provides cookie persistience, connection-pooling, and configuration.
299 unmodified lines
modified:test_requests.py+6 added
109 unmodified lines
110111 r = requests.get(httpbin('get') + '?test=true', params={'q': 'test'}, headers=heads)112 self.assertEqual(r.status_code, 200)113114 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')119120 def test_user_agent_transfers(self):121122 heads = {123 'User-agent': 'Mozilla/5.0 (github.com/kennethreitz/requests)'
232 unmodified lines