mirror of
https://github.com/kennethreitz/requests.git
synced 2026-06-05 22:50:18 +00:00
header_expand: handle Unicode strings (closes #400)
This commit is contained in:
+5
-1
@@ -146,9 +146,13 @@ def header_expand(headers):
|
||||
|
||||
if isinstance(headers, dict):
|
||||
headers = list(headers.items())
|
||||
|
||||
elif isinstance(headers, basestring):
|
||||
return headers
|
||||
elif isinstance(headers, unicode):
|
||||
# As discussed in https://github.com/kennethreitz/requests/issues/400
|
||||
# latin-1 is the most conservative encoding used on the web. Anyone
|
||||
# who needs more can encode to a byte-string before calling
|
||||
return headers.encode("latin-1")
|
||||
|
||||
for i, (value, params) in enumerate(headers):
|
||||
|
||||
|
||||
Regular → Executable
+9
-4
@@ -55,7 +55,7 @@ class TestSetup(object):
|
||||
|
||||
class RequestsTestSuite(TestSetup, unittest.TestCase):
|
||||
"""Requests test cases."""
|
||||
|
||||
|
||||
def test_entry_points(self):
|
||||
|
||||
requests.session
|
||||
@@ -111,11 +111,16 @@ class RequestsTestSuite(TestSetup, unittest.TestCase):
|
||||
r = get(httpbin('get') + '?test=true', params={'q': 'test'}, headers=heads)
|
||||
self.assertEqual(r.status_code, 200)
|
||||
|
||||
def test_session_with_unicode_headers(self):
|
||||
heads = { u'User-Agent': u'\u30cd\u30c3\u30c8\u30ef\u30fc\u30af' }
|
||||
|
||||
def test_unicode_headers(self):
|
||||
# Simply calling requests with a unicode instance should simply work
|
||||
# when the characters are all representable using latin-1:
|
||||
heads = { u'User-Agent': u'Requests Test Suite' }
|
||||
requests.get(url=httpbin('get'), headers=heads)
|
||||
|
||||
# Characters outside latin-1 should raise an exception:
|
||||
heads = { u'User-Agent': u'\u30cd\u30c3\u30c8\u30ef\u30fc\u30af' }
|
||||
self.assertRaises(UnicodeEncodeError, requests.get,
|
||||
url=httpbin('get'), headers=heads)
|
||||
|
||||
def test_user_agent_transfers(self):
|
||||
"""Issue XX"""
|
||||
|
||||
Reference in New Issue
Block a user