Issue #505: Allow disabling of cookie persistence/sending
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Arup Malakar · committed by Arup Malakar · parent 2d328d8 · Browse files · 5 files·+36 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+36 added−3 removed
- tests:+21 added−0 removed
- other:+15 added−3 removed
Provenance
- Commit
c501784- Tree
4e61976- Author
Arup Malakar <amalakar@gmail.com>- Committer
Arup Malakar <amalakar@gmail.com>- Parent
2d328d8
5 files changed+36 added−3 removed
modified:requests/api.py+1 added
21 unmodified lines22 :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.23 :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.24 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.25 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.26+ :param store_cookies: (optional) if ``False``, the received cookies as part of the HTTP response would be ignored.27 :param files: (optional) Dictionary of 'name': file-like-objects (or {'name': ('filename', fileobj)}) for multipart encoding upload.28 :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.29 :param timeout: (optional) Float describing the timeout of the request.30 :param allow_redirects: (optional) Boolean. Set to True if POST/PUT/DELETE redirect following is allowed.88 unmodified linesmodified:requests/models.py+8 added-3 removed
58 unmodified lines59 data=dict(),60 params=dict(),61 auth=None,62 cookies=None,63+ store_cookies=True,64 timeout=None,65 redirect=False,66 allow_redirects=False,67 proxies=None,42 unmodified lines110 111 # Dictionary mapping protocol to the URL of the proxy (e.g. {'http': 'foo.bar:3128'})112 self.proxies = dict(proxies or [])113 114+ #param store_cookies: (optional) if ``False``, the received cookies as part of the HTTP response would be ignored.115+ self.store_cookies = store_cookies116+117 # If no proxies are given, allow configuration by environment variables118 # HTTP_PROXY and HTTPS_PROXY.119 if not self.proxies and self.config.get('trust_env'):120 self.proxies = get_environ_proxies()79 unmodified lines200 201 # Set encoding.202 response.encoding = get_encoding_from_headers(response.headers)203 200- # Add new cookies from the server.201- extract_cookies_to_jar(self.cookies, self, resp)204+ # Add new cookies from the server. Don't if configured not to205+ if self.store_cookies:206+ extract_cookies_to_jar(self.cookies, self, resp)207 208 # Save cookies in Response.209 response.cookies = self.cookies210 253 unmodified lines464 except ValueError:465 return False466 467 def send(self, anyway=False, prefetch=False):463- """Sends the request. Returns True of successful, False if not.468+ """Sends the request. Returns True if successful, False if not.469 If there was an HTTPError during transmission,470 self.response.status_code will contain the HTTPError code.471 472 Once a request is successfully sent, `sent` will equal True.385 unmodified linesmodified:requests/sessions.py+5 added
57 unmodified lines58 59 def __init__(self,60 headers=None,61 cookies=None,62+ store_cookies=True,63 auth=None,64 timeout=None,65 proxies=None,66 hooks=None,12 unmodified lines79 self.config = config or {}80 self.prefetch = prefetch81 self.verify = verify82 self.cert = cert83+ self.store_cookies = store_cookies84 85 for (k, v) in list(defaults.items()):86 self.config.setdefault(k, v)87 24 unmodified lines112 params=None,113 data=None,114 headers=None,115 cookies=None,116+ store_cookies=True,117 files=None,118 auth=None,119 timeout=None,120 allow_redirects=True,13 unmodified lines134 :param params: (optional) Dictionary or bytes to be sent in the query string for the :class:`Request`.135 :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.136 :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.137 :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.138+ :param store_cookies: (optional) if ``False``, the received cookies as part of the HTTP response would be ignored.139 :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.140 :param auth: (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth.141 :param timeout: (optional) Float describing the timeout of the request.142 :param allow_redirects: (optional) Boolean. Set to True by default.30 unmodified lines173 data=data,174 params=params,175 headers=headers,176 cookies=cookies,177+ store_cookies=store_cookies,178 files=files,179 auth=auth,180 hooks=hooks,181 timeout=timeout,147 unmodified linesmodified:tests/test_cookies.py+21 added
100 unmodified lines101 insecure_resp = requests.get('http://httpbin.org/cookies', cookies=cookies)102 insecure_cookies_sent = json.loads(insecure_resp.text)['cookies']103 self.assertEqual(insecure_cookies_sent, {})104 105+ def test_disabled_cookie_persistence(self):106+ """Test that cookies are not persisted when configured accordingly."""107+108+ # Check the case when no cookie is passed as part of the request and the one in response is ignored109+ cookies = requests.get(httpbin('cookies', 'set', 'key', 'value'), store_cookies = False).cookies110+ self.assertIsNone(cookies.get("key"))111+112+ # Test that the cookies passed while making the request still gets used and is available in response object.113+ # only the ones received from server is not saved114+ cookies_2 = requests.get(httpbin('cookies', 'set', 'key', 'value'), store_cookies = False,\115+ cookies = {"key_2" : "value_2"}).cookies116+ self.assertEqual(len(cookies_2), 1)117+ self.assertEqual(cookies_2.get("key_2"), "value_2")118+119+ # Use the session and make sure that the received cookie is not used in subsequent calls120+ s = requests.session()121+ s.get(httpbin('cookies', 'set', 'key', 'value'), store_cookies = False)122+ r = s.get(httpbin('cookies'))123+ self.assertEqual(json.loads(r.text)['cookies'], {})124+125+126 class LWPCookieJarTest(TestBaseMixin, unittest.TestCase):127 """Check store/load of cookies to FileCookieJar's, specifically LWPCookieJar's."""128 129 COOKIEJAR_CLASS = cookielib.LWPCookieJar99 unmodified linesmodified:AUTHORS.rst+1 added
98 unmodified lines99 - Miguel Turner100 - Rohan Jain (crodjer)101 - Justin Barber <barber.justin@gmail.com>102 - Roman Haritonov <@reclosedev>103+- Arup Malakar <amalakar@gmail.com>