Skip to content
Myr

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

3 files+49 added−6 removed

modified:requests/sessions.py+9 added-5 removed
9 unmodified lines
10"""11import os12from datetime import datetime1314from .compat import cookielib14from .compat import cookielib, OrderedDict, urljoin, urlparse15from .cookies import cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar16from .models import Request, PreparedRequest17from .hooks import default_hooks, dispatch_hook18from .utils import from_key_val_list, default_headers19from .exceptions import TooManyRedirects, InvalidSchema2021from .compat import urlparse, urljoin21from .adapters import HTTPAdapter2223from .utils import requote_uri, get_environ_proxies, get_netrc_auth24
196 unmodified lines
221 # Set up a CookieJar to be used by default222 self.cookies = cookiejar_from_dict({})223224 # 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())228229 def __enter__(self):230 return self231
256 unmodified lines
488 for _, v in self.adapters.items():489 v.close()490491 def mount(self, prefix, adapter):493 """Registers a connection adapter to a prefix."""492 """Registers a connection adapter to a prefix.493494 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)499500 def __getstate__(self):501 return dict((attr, getattr(self, attr, None)) for attr in self.__attrs__)502
9 unmodified lines
modified:AUTHORS.rst+1 added
124 unmodified lines
125- 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> @llanga
modified:test_requests.py+39 added-1 removed
9 unmodified lines
10import pickle1112import requests13from requests.auth import HTTPDigestAuth14from requests.adapters import HTTPAdapter15from requests.compat import str, cookielib16from requests.cookies import cookiejar_from_dict17from requests.structures import CaseInsensitiveDict18
463 unmodified lines
482 headers['ACCEPT'.encode('ascii')],483 'application/json'484 )485486 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)523524525class TestCaseInsensitiveDict(unittest.TestCase):526527 def test_mapping_init(self):528 cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})
136 unmodified lines
665 self.assertEqual(frozenset(cid.keys()), keyset)666 self.assertEqual(frozenset(cid), keyset)667668630669if __name__ == '__main__':670 unittest.main()