Skip to content
Myr

Test and perfection for cookie handling.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

I also fixed up some of the RequestsCookieJar methods so using jar.update(other_jar) works without a problem. This cleans up some of the code in sessions and the resolve_redirects method.

authored by Ian Cordasco · committed by Ian Cordasco · parent 0fb13e0 · Browse files · 3 files+16 added−4 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+16 added−4 removed

  • other:+16 added−4 removed

3 files+16 added−4 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+8 added-3 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 pass140139 if response.headers.get('Set-Cookie'):140 prepared_request.headers['Cookie'] = response.headers.get('Set-Cookie')141 prepared_request.prepare_cookies(cookiejar)142143 resp = self.send(144 prepared_request,145 stream=stream,
3 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+2 added-1 removed
110 unmodified lines
111 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()115 url = httpbin('cookies/set/foo/bar')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):
235 unmodified lines