Merge pull request #1327 from ambv/adapter_order
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fixes #1320: transport adapters stored in ordered form
authored by Kenneth Reitz · committed by Kenneth Reitz · parents e7786ec, 4c8af1f · Browse files · 3 files·+49 added−6 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+49 added−6 removed
- other:+49 added−6 removed
Provenance
- Commit
eacb91a- Tree
f3a8d43- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
e7786ec- Parent 2
4c8af1f
3 files changed+49 added−6 removed
modified:requests/sessions.py+9 added-5 removed
9 unmodified lines10 """11 import os12 from datetime import datetime13 14-from .compat import cookielib14+from .compat import cookielib, OrderedDict, urljoin, urlparse15 from .cookies import cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar16 from .models import Request, PreparedRequest17 from .hooks import default_hooks, dispatch_hook18 from .utils import from_key_val_list, default_headers19 from .exceptions import TooManyRedirects, InvalidSchema20 21-from .compat import urlparse, urljoin21 from .adapters import HTTPAdapter22 23 from .utils import requote_uri, get_environ_proxies, get_netrc_auth24 196 unmodified lines221 # Set up a CookieJar to be used by default222 self.cookies = cookiejar_from_dict({})223 224 # Default connection adapters.226- self.adapters = {}227- self.mount('http://', HTTPAdapter())225+ self.adapters = OrderedDict()226 self.mount('https://', HTTPAdapter())227+ self.mount('http://', HTTPAdapter())228 229 def __enter__(self):230 return self231 256 unmodified lines488 for _, v in self.adapters.items():489 v.close()490 491 def mount(self, prefix, adapter):493- """Registers a connection adapter to a prefix."""492+ """Registers a connection adapter to a prefix.493+494+ Adapters are sorted in descending order by key length."""495 self.adapters[prefix] = adapter496+ keys_to_move = [k for k in self.adapters if len(k) < len(prefix)]497+ for key in keys_to_move:498+ self.adapters[key] = self.adapters.pop(key)499 500 def __getstate__(self):501 return dict((attr, getattr(self, attr, None)) for attr in self.__attrs__)502 9 unmodified linesmodified:AUTHORS.rst+1 added
124 unmodified lines125 - Dmitry Medvinsky <me@dmedvinsky.name>126 - Bryce Boe <bbzbryce@gmail.com> @bboe127 - Colin Dunklau <colin.dunklau@gmail.com> @cdunklau128 - Hugo Osvaldo Barrera <hugo@osvaldobarrera.com.ar> @hobarrera129+- Łukasz Langa <lukasz@langa.pl> @llangamodified:test_requests.py+39 added-1 removed
9 unmodified lines10 import pickle11 12 import requests13 from requests.auth import HTTPDigestAuth14+from requests.adapters import HTTPAdapter15 from requests.compat import str, cookielib16 from requests.cookies import cookiejar_from_dict17 from requests.structures import CaseInsensitiveDict18 463 unmodified lines482 headers['ACCEPT'.encode('ascii')],483 'application/json'484 )485 486+ def test_transport_adapter_ordering(self):487+ s = requests.Session()488+ order = ['https://', 'http://']489+ self.assertEqual(order, list(s.adapters))490+ s.mount('http://git', HTTPAdapter())491+ s.mount('http://github', HTTPAdapter())492+ s.mount('http://github.com', HTTPAdapter())493+ s.mount('http://github.com/about/', HTTPAdapter())494+ order = [495+ 'http://github.com/about/',496+ 'http://github.com',497+ 'http://github',498+ 'http://git',499+ 'https://',500+ 'http://',501+ ]502+ self.assertEqual(order, list(s.adapters))503+ s.mount('http://gittip', HTTPAdapter())504+ s.mount('http://gittip.com', HTTPAdapter())505+ s.mount('http://gittip.com/about/', HTTPAdapter())506+ order = [507+ 'http://github.com/about/',508+ 'http://gittip.com/about/',509+ 'http://github.com',510+ 'http://gittip.com',511+ 'http://github',512+ 'http://gittip',513+ 'http://git',514+ 'https://',515+ 'http://',516+ ]517+ self.assertEqual(order, list(s.adapters))518+ s2 = requests.Session()519+ s2.adapters = {'http://': HTTPAdapter()}520+ s2.mount('https://', HTTPAdapter())521+ self.assertTrue('http://' in s2.adapters)522+ self.assertTrue('https://' in s2.adapters)523 524+525 class TestCaseInsensitiveDict(unittest.TestCase):526 527 def test_mapping_init(self):528 cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})136 unmodified lines665 self.assertEqual(frozenset(cid.keys()), keyset)666 self.assertEqual(frozenset(cid), keyset)667 668 630-669 if __name__ == '__main__':670 unittest.main()