Skip to content

Commit

Permalink
Fixes psf#1320: transport adapters stored in ordered form
Browse files Browse the repository at this point in the history
  • Loading branch information
ambv committed May 15, 2013
1 parent e7786ec commit 4c8af1f
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 6 deletions.
1 change: 1 addition & 0 deletions AUTHORS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -126,3 +126,4 @@ Patches and Suggestions
- Bryce Boe <[email protected]> @bboe
- Colin Dunklau <[email protected]> @cdunklau
- Hugo Osvaldo Barrera <[email protected]> @hobarrera
- Łukasz Langa <[email protected]> @llanga
14 changes: 9 additions & 5 deletions requests/sessions.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,13 @@
import os
from datetime import datetime

from .compat import cookielib
from .compat import cookielib, OrderedDict, urljoin, urlparse
from .cookies import cookiejar_from_dict, extract_cookies_to_jar, RequestsCookieJar
from .models import Request, PreparedRequest
from .hooks import default_hooks, dispatch_hook
from .utils import from_key_val_list, default_headers
from .exceptions import TooManyRedirects, InvalidSchema

from .compat import urlparse, urljoin
from .adapters import HTTPAdapter

from .utils import requote_uri, get_environ_proxies, get_netrc_auth
Expand Down Expand Up @@ -223,9 +222,9 @@ def __init__(self):
self.cookies = cookiejar_from_dict({})

# Default connection adapters.
self.adapters = {}
self.mount('http://', HTTPAdapter())
self.adapters = OrderedDict()
self.mount('https://', HTTPAdapter())
self.mount('http://', HTTPAdapter())

def __enter__(self):
return self
Expand Down Expand Up @@ -490,8 +489,13 @@ def close(self):
v.close()

def mount(self, prefix, adapter):
"""Registers a connection adapter to a prefix."""
"""Registers a connection adapter to a prefix.
Adapters are sorted in descending order by key length."""
self.adapters[prefix] = adapter
keys_to_move = [k for k in self.adapters if len(k) < len(prefix)]
for key in keys_to_move:
self.adapters[key] = self.adapters.pop(key)

def __getstate__(self):
return dict((attr, getattr(self, attr, None)) for attr in self.__attrs__)
Expand Down
40 changes: 39 additions & 1 deletion test_requests.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import requests
from requests.auth import HTTPDigestAuth
from requests.adapters import HTTPAdapter
from requests.compat import str, cookielib
from requests.cookies import cookiejar_from_dict
from requests.structures import CaseInsensitiveDict
Expand Down Expand Up @@ -482,6 +483,44 @@ def test_fixes_1329(self):
'application/json'
)

def test_transport_adapter_ordering(self):
s = requests.Session()
order = ['https://', 'http://']
self.assertEqual(order, list(s.adapters))
s.mount('http://git', HTTPAdapter())
s.mount('http://github', HTTPAdapter())
s.mount('http://github.com', HTTPAdapter())
s.mount('http://github.com/about/', HTTPAdapter())
order = [
'http://github.com/about/',
'http://github.com',
'http://github',
'http://git',
'https://',
'http://',
]
self.assertEqual(order, list(s.adapters))
s.mount('http://gittip', HTTPAdapter())
s.mount('http://gittip.com', HTTPAdapter())
s.mount('http://gittip.com/about/', HTTPAdapter())
order = [
'http://github.com/about/',
'http://gittip.com/about/',
'http://github.com',
'http://gittip.com',
'http://github',
'http://gittip',
'http://git',
'https://',
'http://',
]
self.assertEqual(order, list(s.adapters))
s2 = requests.Session()
s2.adapters = {'http://': HTTPAdapter()}
s2.mount('https://', HTTPAdapter())
self.assertTrue('http://' in s2.adapters)
self.assertTrue('https://' in s2.adapters)


class TestCaseInsensitiveDict(unittest.TestCase):

Expand Down Expand Up @@ -627,6 +666,5 @@ def test_preserve_last_key_case(self):
self.assertEqual(frozenset(cid), keyset)



if __name__ == '__main__':
unittest.main()

0 comments on commit 4c8af1f

Please sign in to comment.