Merge branch 'keytype' of git://github.com/Lukasa/requests into 2.0
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Kenneth Reitz · committed by Kenneth Reitz · parents 22701d1, 7de6b1e · Browse files · 3 files·+43 added−16 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+43 added−16 removed
- other:+43 added−16 removed
Provenance
- Commit
1a6637c- Tree
eccafbc- Author
Kenneth Reitz <me@kennethreitz.com>- Committer
Kenneth Reitz <me@kennethreitz.com>- Parent 1
22701d1- Parent 2
7de6b1e
3 files changed+43 added−16 removed
modified:requests/models.py+2 added-3 removed
23 unmodified lines24 ChunkedEncodingError)25 from .utils import (26 guess_filename, get_auth_from_url, requote_uri,27 stream_decode_response_unicode, to_key_val_list, parse_header_links,28- iter_slices, guess_json_utf, super_len)28+ iter_slices, guess_json_utf, super_len, to_native_string)29 from .compat import (30 cookielib, urlunparse, urlsplit, urlencode, str, bytes, StringIO,31 is_py2, chardet, json, builtin_str, basestring, IncompleteRead)32 327 unmodified lines360 def prepare_headers(self, headers):361 """Prepares the given HTTP headers."""362 363 if headers:364- headers = dict((name.encode('ascii'), value) for name, value in headers.items())365- self.headers = CaseInsensitiveDict(headers)364+ self.headers = CaseInsensitiveDict((to_native_string(name), value) for name, value in headers.items())365 else:366 self.headers = CaseInsensitiveDict()367 368 def prepare_body(self, data, files):356 unmodified linesmodified:requests/utils.py+25 added-6 removed
19 unmodified lines20 21 from . import __version__22 from . import certs23 from .compat import parse_http_list as _parse_list_header24-from .compat import quote, urlparse, bytes, str, OrderedDict, urlunparse25-from .compat import getproxies, proxy_bypass24+from .compat import (quote, urlparse, bytes, str, OrderedDict, urlunparse,25+ is_py2, is_py3, builtin_str, getproxies, proxy_bypass)26 from .cookies import RequestsCookieJar, cookiejar_from_dict27 from .structures import CaseInsensitiveDict28 29 _hush_pyflakes = (RequestsCookieJar,)362 unmodified lines392 # First check whether no_proxy is defined. If it is, check that the URL393 # we're getting isn't in the no_proxy list.394 no_proxy = get_proxy('no_proxy')395 netloc = urlparse(url).netloc396- 396+397 if no_proxy:398 # We need to check whether we match here. We need to see if we match399 # the end of the netloc, both with and without the port.400 no_proxy = no_proxy.split(',')401- 401+402 for host in no_proxy:403 if netloc.endswith(host) or netloc.split(':')[0].endswith(host):404 # The URL does match something in no_proxy, so we don't want405 # to apply the proxies on this URL.406 return {}407- 407+408 # If the system proxy settings indicate that this URL should be bypassed,409 # don't proxy.410 if proxy_bypass(netloc):411 return {}1 unmodified line413 # If we get here, we either didn't have no_proxy set or we're not going414 # anywhere that no_proxy applies to, and the system settings don't require415 # bypassing the proxy for the current URL.416 return getproxies()417- 417 418+419 def default_user_agent():420 """Return a string representing the default user agent."""421 _implementation = platform.python_implementation()422 122 unmodified lines545 parsed = urlparse(url)546 return (parsed.username, parsed.password)547 else:548 return ('', '')549+550+551+def to_native_string(string, encoding='ascii'):552+ """553+ Given a string object, regardless of type, returns a representation of that554+ string in the native string type, encoding and decoding where necessary.555+ This assumes ASCII unless told otherwise.556+ """557+ out = None558+559+ if isinstance(string, builtin_str):560+ out = string561+ else:562+ if is_py2:563+ out = string.encode(encoding)564+ else:565+ out = string.decode(encoding)566+567+ return outmodified:test_requests.py+16 added-7 removed
179 unmodified lines180 # Make sure the cookie was sent181 assert r.json()['cookies']['foo'] == 'bar'182 # Make sure the session cj is still the custom one183 assert s.cookies is cj184- 184+185 def test_requests_in_history_are_not_overridden(self):186 resp = requests.get(httpbin('redirect/3'))187 urls = [r.url for r in resp.history]188 req_urls = [r.request.url for r in resp.history]189 self.assertEquals(urls, req_urls)190- 190+191 def test_user_agent_transfers(self):192 193 heads = {194 'User-agent': 'Mozilla/5.0 (github.com/kennethreitz/requests)'49 unmodified lines244 r = requests.get(url, auth=wrong_auth)245 self.assertEqual(r.status_code, 401)246 247 s = requests.session()248- 248+249 # Should use netrc and work.250 r = s.get(url)251 self.assertEqual(r.status_code, 200)252 302 unmodified lines555 s.headers.update({'ACCEPT': 'BOGUS'})556 s.headers.update({'accept': 'application/json'})557 r = s.get(httpbin('get'))558 headers = r.request.headers559- # ASCII encode because of key comparison changes in py3559 self.assertEqual(561- headers['accept'.encode('ascii')],560+ headers['accept'],561 'application/json'562 )563 self.assertEqual(565- headers['Accept'.encode('ascii')],564+ headers['Accept'],565 'application/json'566 )567 self.assertEqual(569- headers['ACCEPT'.encode('ascii')],568+ headers['ACCEPT'],569 'application/json'570 )571 572 def test_uppercase_scheme_redirect(self):63 unmodified lines636 )637 r = requests.Request('GET', url).prepare()638 self.assertEqual(r.url, url)639 640+ def test_header_keys_are_native(self):641+ headers = {u'unicode': 'blah', 'byte'.encode('ascii'): 'blah'}642+ r = requests.Request('GET', httpbin('get'), headers=headers)643+ p = r.prepare()644 645+ # This is testing that they are builtin strings. A bit weird, but there646+ # we go.647+ self.assertTrue('unicode' in p.headers.keys())648+ self.assertTrue('byte' in p.headers.keys())649+650+651 class TestCaseInsensitiveDict(unittest.TestCase):652 653 def test_mapping_init(self):654 cid = CaseInsensitiveDict({'Foo': 'foo','BAr': 'bar'})142 unmodified lines