header_expand: handle Unicode strings (closes #400)
authorChris Adams <chris@improbable.org>
Mon, 13 Feb 2012 15:52:25 +0000 (10:52 -0500)
committerChris Adams <chris@improbable.org>
Mon, 13 Feb 2012 16:10:05 +0000 (11:10 -0500)
requests/utils.py
test_requests.py [changed mode: 0644->0755]

index 0f23a52790b532a837f289e8d1fe752ed6fdadc9..8865ae24fdac43d2ece5ff3310106366d1caef26 100644 (file)
@@ -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):
 
old mode 100644 (file)
new mode 100755 (executable)
index eda9f79..59069f3
@@ -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"""