Skip to content
Myr

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

3 files+83 added−22 removed

modified:requests/cookies.py+18 added-18 removed
376 unmodified lines
377378379def morsel_to_cookie(morsel):380 """Convert a Morsel object into a Cookie containing the one k/v pair."""381382 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 )404405406def cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True):407 """Returns a CookieJar from a key/value dictionary.
38 unmodified lines
modified:AUTHORS.rst+1 added
142 unmodified lines
143- 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> @beckerfuffle
modified:test_requests.py+64 added-4 removed
4 unmodified lines
56from __future__ import division7import json8import os9import unittest9import pickle10import unittest1112import requests13import pytest14from requests.auth import HTTPDigestAuth14from requests.adapters import HTTPAdapter16from requests.compat import str, cookielib, getproxies, urljoin, urlparse17from requests.cookies import cookiejar_from_dict15from requests.auth import HTTPDigestAuth16from requests.compat import (17 Morsel, cookielib, getproxies, str, urljoin, urlparse)18from requests.cookies import cookiejar_from_dict, morsel_to_cookie19from requests.exceptions import InvalidURL, MissingSchema20from requests.structures import CaseInsensitiveDict2122try:
981 unmodified lines
1004 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')1006100710081009class TestMorselToCookieExpires(unittest.TestCase):10101011 """Tests for morsel_to_cookie when morsel contains expires."""10121013 def test_expires_valid_str(self):1014 """Test case where we convert expires from string time."""10151016 morsel = Morsel()1017 morsel['expires'] = 'Thu, 01-Jan-1970 00:00:01 GMT'1018 cookie = morsel_to_cookie(morsel)1019 assert cookie.expires == 110201021 def test_expires_invalid_int(self):1022 """Test case where an invalid type is passed for expires."""10231024 morsel = Morsel()1025 morsel['expires'] = 1001026 with pytest.raises(TypeError):1027 morsel_to_cookie(morsel)10281029 def test_expires_invalid_str(self):1030 """Test case where an invalid string is input."""10311032 morsel = Morsel()1033 morsel['expires'] = 'woops'1034 with pytest.raises(ValueError):1035 morsel_to_cookie(morsel)10361037 def test_expires_none(self):1038 """Test case where expires is None."""10391040 morsel = Morsel()1041 morsel['expires'] = None1042 cookie = morsel_to_cookie(morsel)1043 assert cookie.expires is None104410451046class TestMorselToCookieMaxAge(unittest.TestCase):10471048 """Tests for morsel_to_cookie when morsel contains max-age."""10491050 def test_max_age_valid_int(self):1051 """Test case where a valid max age in seconds is passed."""10521053 morsel = Morsel()1054 morsel['max-age'] = 601055 cookie = morsel_to_cookie(morsel)1056 assert isinstance(cookie.expires, int)10571058 def test_max_age_invalid_str(self):1059 """Test case where a invalid max age is passed."""10601061 morsel = Morsel()1062 morsel['max-age'] = 'woops'1063 with pytest.raises(TypeError):1064 morsel_to_cookie(morsel)106510661067if __name__ == '__main__':1068 unittest.main()