Add tests for decode_unicode
authorJason R. Coombs <jaraco@jaraco.com>
Tue, 4 Mar 2014 22:46:58 +0000 (17:46 -0500)
committerJason R. Coombs <jaraco@jaraco.com>
Tue, 4 Mar 2014 22:46:58 +0000 (17:46 -0500)
test_requests.py

index 17de84911bb32db1df00aef1224f3bc917864e92..65eb571cf81d4ca678ad9a6ef5a32c8f1f006122 100755 (executable)
@@ -9,6 +9,7 @@ import os
 import pickle
 import unittest
 
+import io
 import requests
 import pytest
 from requests.adapters import HTTPAdapter
@@ -690,6 +691,26 @@ class RequestsTestCase(unittest.TestCase):
         assert next(iter(r))
         io.close()
 
+    def test_response_decode_unicode(self):
+        """
+        When called with decode_unicode, Response.iter_content should always
+        return unicode.
+        """
+        r = requests.Response()
+        r._content_consumed = True
+        r._content = b'the content'
+        r.encoding = 'ascii'
+
+        chunks = r.iter_content(decode_unicode=True)
+        assert all(isinstance(chunk, str) for chunk in chunks)
+
+        # also for streaming
+        r = requests.Response()
+        r.raw = io.BytesIO(b'the content')
+        r.encoding = 'ascii'
+        chunks = r.iter_content(decode_unicode=True)
+        assert all(isinstance(chunk, str) for chunk in chunks)
+
     def test_request_and_response_are_pickleable(self):
         r = requests.get(httpbin('get'))