Issue #505: Changed the store_cookie configuration to be part of the config dict...
authorArup Malakar <amalakar@gmail.com>
Wed, 23 May 2012 21:12:18 +0000 (14:12 -0700)
committerArup Malakar <amalakar@gmail.com>
Wed, 23 May 2012 21:12:18 +0000 (14:12 -0700)
requests/api.py
requests/defaults.py
requests/models.py
requests/sessions.py
tests/test_cookies.py

index cc6a1c7bbc8d7548eab663ef6d7b46103463d998..9cea79afc09b3f3edf316d14c9aeee1d53fc0a3d 100644 (file)
@@ -25,7 +25,6 @@ def request(method, url, **kwargs):
     :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
     :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
     :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
-    :param store_cookies: (optional) if ``False``, the received cookies as part of the HTTP response would be ignored.
     :param files: (optional) Dictionary of 'name': file-like-objects (or {'name': ('filename', fileobj)}) for multipart encoding upload.
     :param auth: (optional) Auth tuple to enable Basic/Digest/Custom HTTP Auth.
     :param timeout: (optional) Float describing the timeout of the request.
@@ -33,7 +32,7 @@ def request(method, url, **kwargs):
     :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
     :param return_response: (optional) If False, an un-sent Request object will returned.
     :param session: (optional) A :class:`Session` object to be used for the request.
-    :param config: (optional) A configuration dictionary.
+    :param config: (optional) A configuration dictionary. See ``request.defaults`` for allowed keys and their default values.
     :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.
     :param prefetch: (optional) if ``True``, the response content will be immediately downloaded.
     :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
index 6a7ea27004170904a8a171ad5d03df6b507c52b5..41e279df7bc0dd122f376935c05fbf27930d52dc 100644 (file)
@@ -20,6 +20,7 @@ Configurations:
 :pool_connections: The number of active HTTP connection pools to use.
 :encode_uri: If true, URIs will automatically be percent-encoded.
 :trust_env: If true, the surrouding environment will be trusted (environ, netrc).
+:param store_cookies: If false, the received cookies as part of the HTTP response would be ignored.
 
 """
 
@@ -47,5 +48,6 @@ defaults['strict_mode'] = False
 defaults['keep_alive'] = True
 defaults['encode_uri'] = True
 defaults['trust_env'] = True
+defaults['store_cookies'] = True
 
 
index 79a3bc36496db95ff45ce79dddf9b269d6bad353..7d3b1c82aa248f411b68a65b40d63acdcf1212f2 100644 (file)
@@ -59,7 +59,6 @@ class Request(object):
         params=dict(),
         auth=None,
         cookies=None,
-        store_cookies=True,
         timeout=None,
         redirect=False,
         allow_redirects=False,
@@ -110,9 +109,6 @@ class Request(object):
         # Dictionary mapping protocol to the URL of the proxy (e.g. {'http': 'foo.bar:3128'})
         self.proxies = dict(proxies or [])
 
-        #param store_cookies: (optional) if ``False``, the received cookies as part of the HTTP response would be ignored.
-        self.store_cookies = store_cookies
-
         # If no proxies are given, allow configuration by environment variables
         # HTTP_PROXY and HTTPS_PROXY.
         if not self.proxies and self.config.get('trust_env'):
@@ -201,7 +197,7 @@ class Request(object):
                 response.encoding = get_encoding_from_headers(response.headers)
 
                 # Add new cookies from the server. Don't if configured not to
-                if self.store_cookies:
+                if self.config.get('store_cookies'):
                     extract_cookies_to_jar(self.cookies, self, resp)
 
                 # Save cookies in Response.
index e047ce562d8a36bd512072085da55df7015f72ac..3113c787d6f375eec31d47e8576f247512b57688 100644 (file)
@@ -60,7 +60,6 @@ class Session(object):
     def __init__(self,
         headers=None,
         cookies=None,
-        store_cookies=True,
         auth=None,
         timeout=None,
         proxies=None,
@@ -81,7 +80,6 @@ class Session(object):
         self.prefetch = prefetch
         self.verify = verify
         self.cert = cert
-        self.store_cookies = store_cookies
 
         for (k, v) in list(defaults.items()):
             self.config.setdefault(k, deepcopy(v))
@@ -114,7 +112,6 @@ class Session(object):
         data=None,
         headers=None,
         cookies=None,
-        store_cookies=True,
         files=None,
         auth=None,
         timeout=None,
@@ -136,14 +133,13 @@ class Session(object):
         :param data: (optional) Dictionary or bytes to send in the body of the :class:`Request`.
         :param headers: (optional) Dictionary of HTTP Headers to send with the :class:`Request`.
         :param cookies: (optional) Dict or CookieJar object to send with the :class:`Request`.
-        :param store_cookies: (optional) if ``False``, the received cookies as part of the HTTP response would be ignored.
         :param files: (optional) Dictionary of 'filename': file-like-objects for multipart encoding upload.
         :param auth: (optional) Auth tuple or callable to enable Basic/Digest/Custom HTTP Auth.
         :param timeout: (optional) Float describing the timeout of the request.
         :param allow_redirects: (optional) Boolean. Set to True by default.
         :param proxies: (optional) Dictionary mapping protocol to the URL of the proxy.
         :param return_response: (optional) If False, an un-sent Request object will returned.
-        :param config: (optional) A configuration dictionary.
+        :param config: (optional) A configuration dictionary. See ``request.defaults`` for allowed keys and their default values.
         :param prefetch: (optional) if ``True``, the response content will be immediately downloaded.
         :param verify: (optional) if ``True``, the SSL cert will be verified. A CA_BUNDLE path can also be provided.
         :param cert: (optional) if String, path to ssl client cert file (.pem). If Tuple, ('cert', 'key') pair.
@@ -175,7 +171,6 @@ class Session(object):
             params=params,
             headers=headers,
             cookies=cookies,
-            store_cookies=store_cookies,
             files=files,
             auth=auth,
             hooks=hooks,
index bdc9172a15af34a45b63c67d3a3efb85f436a946..60acdd8c0e0930905d34566018e38ff57dda5d05 100755 (executable)
@@ -106,20 +106,22 @@ class CookieTests(TestBaseMixin, unittest.TestCase):
     def test_disabled_cookie_persistence(self):
         """Test that cookies are not persisted when configured accordingly."""
 
+        config = {'store_cookies' : False}
+
         # Check the case when no cookie is passed as part of the request and the one in response is ignored
-        cookies = requests.get(httpbin('cookies', 'set', 'key', 'value'), store_cookies = False).cookies
-        self.assertEqual(cookies.get("key"), None)
+        cookies = requests.get(httpbin('cookies', 'set', 'key', 'value'), config = config).cookies
+        self.assertIsNone(cookies.get("key"))
 
         # Test that the cookies passed while making the request still gets used and is available in response object.
         # only the ones received from server is not saved
-        cookies_2 = requests.get(httpbin('cookies', 'set', 'key', 'value'), store_cookies = False,\
+        cookies_2 = requests.get(httpbin('cookies', 'set', 'key', 'value'), config = config,\
                                                 cookies = {"key_2" : "value_2"}).cookies
         self.assertEqual(len(cookies_2), 1)
         self.assertEqual(cookies_2.get("key_2"), "value_2")
 
         # Use the session and make sure that the received cookie is not used in subsequent calls
         s = requests.session()
-        s.get(httpbin('cookies', 'set', 'key', 'value'), store_cookies = False)
+        s.get(httpbin('cookies', 'set', 'key', 'value'), config = config)
         r = s.get(httpbin('cookies'))
         self.assertEqual(json.loads(r.text)['cookies'], {})