Use builtin_str for all auto-set header values.
authorCory Benfield <lukasaoz@gmail.com>
Fri, 18 Oct 2013 17:34:29 +0000 (18:34 +0100)
committerCory Benfield <lukasaoz@gmail.com>
Fri, 18 Oct 2013 17:34:29 +0000 (18:34 +0100)
requests/models.py
test_requests.py

index ee2ca9e2b99b7ced87629dd376ed1bf5ae9a4fe2..c3c9a10c6846188a2dbe53b53b1c6e4767f8ff86 100644 (file)
@@ -407,7 +407,7 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
                 raise NotImplementedError('Streamed bodies and files are mutually exclusive.')
 
             if length is not None:
-                self.headers['Content-Length'] = str(length)
+                self.headers['Content-Length'] = builtin_str(length)
             else:
                 self.headers['Transfer-Encoding'] = 'chunked'
         else:
@@ -433,12 +433,12 @@ class PreparedRequest(RequestEncodingMixin, RequestHooksMixin):
     def prepare_content_length(self, body):
         if hasattr(body, 'seek') and hasattr(body, 'tell'):
             body.seek(0, 2)
-            self.headers['Content-Length'] = str(body.tell())
+            self.headers['Content-Length'] = builtin_str(body.tell())
             body.seek(0, 0)
         elif body is not None:
             l = super_len(body)
             if l:
-                self.headers['Content-Length'] = str(l)
+                self.headers['Content-Length'] = builtin_str(l)
         elif self.method not in ('GET', 'HEAD'):
             self.headers['Content-Length'] = '0'
 
index 18fb10ed8c674f1c54d5541a210f7fa4c3d5e15b..572602753146d595248e50ba2315d1b71a3c10ba 100755 (executable)
@@ -684,6 +684,14 @@ class RequestsTestCase(unittest.TestCase):
 
         self.assertTrue('multipart/form-data' in p.headers['Content-Type'])
 
+    def test_autoset_header_values_are_native(self):
+        data = 'this is a string'
+        length = '16'
+        req = requests.Request('POST', httpbin('post'), data=data)
+        p = req.prepare()
+
+        self.assertEqual(p.headers['Content-Length'], length)
+
 
 class TestContentEncodingDetection(unittest.TestCase):