Merge remote-tracking branch 'origin/master'
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 39d1281, 0b68037 · Browse files · 4 files·+84 added−22 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+84 added−22 removed
- other:+84 added−22 removed
Provenance
- Commit
1832e33- Tree
2cb5ae0- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
39d1281- Parent 2
0b68037
4 files changed+84 added−22 removed
modified:requests/cookies.py+18 added-18 removed
376 unmodified lines377 378 379 def morsel_to_cookie(morsel):380 """Convert a Morsel object into a Cookie containing the one k/v pair."""381+382 expires = None382- if morsel["max-age"]:383- expires = time.time() + morsel["max-age"]383+ if morsel['max-age']:384+ expires = time.time() + morsel['max-age']385 elif morsel['expires']:385- expires = morsel['expires']386- if type(expires) == type(""):387- time_template = "%a, %d-%b-%Y %H:%M:%S GMT"388- expires = time.mktime(time.strptime(expires, time_template))389- c = create_cookie(390- name=morsel.key,391- value=morsel.value,392- version=morsel['version'] or 0,393- port=None,394- domain=morsel['domain'],395- path=morsel['path'],396- secure=bool(morsel['secure']),397- expires=expires,398- discard=False,386+ time_template = '%a, %d-%b-%Y %H:%M:%S GMT'387+ expires = time.mktime(388+ time.strptime(morsel['expires'], time_template)) - time.timezone389+ return create_cookie(390 comment=morsel['comment'],391 comment_url=bool(morsel['comment']),392+ discard=False,393+ domain=morsel['domain'],394+ expires=expires,395+ name=morsel.key,396+ path=morsel['path'],397+ port=None,398 rest={'HttpOnly': morsel['httponly']},402- rfc2109=False,)403- return c399+ rfc2109=False,400+ secure=bool(morsel['secure']),401+ value=morsel.value,402+ version=morsel['version'] or 0,403+ )404 405 406 def cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True):407 """Returns a CookieJar from a key/value dictionary.38 unmodified linesmodified:AUTHORS.rst+1 added
142 unmodified lines143 - Thomas Weißschuh <thomas@t-8ch.de> @t-8ch144 - Jayson Vantuyl <jayson@aggressive.ly> @kagato145 - Pengfei.X <pengphy@gmail.com>146 - Kamil Madac <kamil.madac@gmail.com>147+- Michael Becker <mike@beckerfuffle.com> @beckerfufflemodified:HISTORY.rst+1 added
5 unmodified lines6 2.1.0 (2013-12-05)7 ++++++++++++++++++8 9 - Updated CA Bundle, of course.10+- Cookies set on individual Requests through a ``Session`` (e.g. via ``Session.get()``) are no longer persisted to the ``Session``.11 - Clean up connections when we hit problems during chunked upload, rather than leaking them.12 - Return connections to the pool when a chunked upload is successful, rather than leaking it.13 - Match the HTTPbis recommendation for HTTP 301 redirects.14 - Prevent hanging when using streaming uploads and Digest Auth when a 401 is received.730 unmodified linesmodified:test_requests.py+64 added-4 removed
4 unmodified lines5 6 from __future__ import division7 import json8 import os9-import unittest9 import pickle10+import unittest11 12 import requests13 import pytest14-from requests.auth import HTTPDigestAuth14 from requests.adapters import HTTPAdapter16-from requests.compat import str, cookielib, getproxies, urljoin, urlparse17-from requests.cookies import cookiejar_from_dict15+from requests.auth import HTTPDigestAuth16+from requests.compat import (17+ Morsel, cookielib, getproxies, str, urljoin, urlparse)18+from requests.cookies import cookiejar_from_dict, morsel_to_cookie19 from requests.exceptions import InvalidURL, MissingSchema20 from requests.structures import CaseInsensitiveDict21 22 try:981 unmodified lines1004 assert address_in_network('192.168.1.1', '192.168.1.0/24')1005 assert not address_in_network('172.16.0.1', '192.168.1.0/24')1006 1007 1008+1009+class TestMorselToCookieExpires(unittest.TestCase):1010+1011+ """Tests for morsel_to_cookie when morsel contains expires."""1012+1013+ def test_expires_valid_str(self):1014+ """Test case where we convert expires from string time."""1015+1016+ morsel = Morsel()1017+ morsel['expires'] = 'Thu, 01-Jan-1970 00:00:01 GMT'1018+ cookie = morsel_to_cookie(morsel)1019+ assert cookie.expires == 11020+1021+ def test_expires_invalid_int(self):1022+ """Test case where an invalid type is passed for expires."""1023+1024+ morsel = Morsel()1025+ morsel['expires'] = 1001026+ with pytest.raises(TypeError):1027+ morsel_to_cookie(morsel)1028+1029+ def test_expires_invalid_str(self):1030+ """Test case where an invalid string is input."""1031+1032+ morsel = Morsel()1033+ morsel['expires'] = 'woops'1034+ with pytest.raises(ValueError):1035+ morsel_to_cookie(morsel)1036+1037+ def test_expires_none(self):1038+ """Test case where expires is None."""1039+1040+ morsel = Morsel()1041+ morsel['expires'] = None1042+ cookie = morsel_to_cookie(morsel)1043+ assert cookie.expires is None1044+1045+1046+class TestMorselToCookieMaxAge(unittest.TestCase):1047+1048+ """Tests for morsel_to_cookie when morsel contains max-age."""1049+1050+ def test_max_age_valid_int(self):1051+ """Test case where a valid max age in seconds is passed."""1052+1053+ morsel = Morsel()1054+ morsel['max-age'] = 601055+ cookie = morsel_to_cookie(morsel)1056+ assert isinstance(cookie.expires, int)1057+1058+ def test_max_age_invalid_str(self):1059+ """Test case where a invalid max age is passed."""1060+1061+ morsel = Morsel()1062+ morsel['max-age'] = 'woops'1063+ with pytest.raises(TypeError):1064+ morsel_to_cookie(morsel)1065+1066+1067 if __name__ == '__main__':1068 unittest.main()