Merge pull request #1772 from mdbecker/make_morsel_to_cookie_more_pythonic
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Make morsel_to_cookie more pythonic
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 3235766, 837ba94 · Browse files · 3 files·+83 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
3 files+83 added−22 removed
- other:+83 added−22 removed
Provenance
- Commit
9fc0701- Tree
4597534- Author
Kenneth Reitz <me@kennethreitz.org>- Committer
Kenneth Reitz <me@kennethreitz.org>- Parent 1
3235766- Parent 2
837ba94
3 files changed+83 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: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()