test: restore Python 3.2 compatibility
authorMartin Geisler <martin@geisler.net>
Sun, 25 May 2014 22:31:10 +0000 (00:31 +0200)
committerMartin Geisler <martin@geisler.net>
Mon, 26 May 2014 14:20:39 +0000 (16:20 +0200)
The tests were broken because they used the u'...' Unicode literal
syntax which disappeared in Python 3.0 to 3.2.

We can work around this by conditionally defining a "u" function which
will produce a Unicode literal on Python 2.x. This is basically the
same approach as taken by the six library often used for writing
cross-version compatible code.

test_requests.py

index a92b22c69ff1b767c6bcef196e04aed91e087382..4871dc5109b3ad9d908d516e767e93ee3dda314d 100755 (executable)
@@ -16,7 +16,7 @@ import pytest
 from requests.adapters import HTTPAdapter
 from requests.auth import HTTPDigestAuth
 from requests.compat import (
-    Morsel, cookielib, getproxies, str, urljoin, urlparse)
+    Morsel, cookielib, getproxies, str, urljoin, urlparse, is_py3)
 from requests.cookies import cookiejar_from_dict, morsel_to_cookie
 from requests.exceptions import InvalidURL, MissingSchema
 from requests.models import PreparedRequest, Response
@@ -30,6 +30,14 @@ try:
 except ImportError:
     import io as StringIO
 
+if is_py3:
+    def u(s):
+        return s
+else:
+    def u(s):
+        return s.decode('unicode-escape')
+
+
 HTTPBIN = os.environ.get('HTTPBIN_URL', 'http://httpbin.org/')
 # Issue #1483: Make sure the URL always has a trailing slash
 HTTPBIN = HTTPBIN.rstrip('/') + '/'
@@ -409,7 +417,7 @@ class RequestsTestCase(unittest.TestCase):
         url = httpbin('post')
         with open('requirements.txt') as f:
             pytest.raises(ValueError, "requests.post(url, data='[{\"some\": \"data\"}]', files={'some': f})")
-            pytest.raises(ValueError, "requests.post(url, data=u'[{\"some\": \"data\"}]', files={'some': f})")
+            pytest.raises(ValueError, "requests.post(url, data=u('[{\"some\": \"data\"}]'), files={'some': f})")
 
     def test_request_ok_set(self):
         r = requests.get(httpbin('status', '404'))
@@ -456,12 +464,12 @@ class RequestsTestCase(unittest.TestCase):
 
     def test_unicode_multipart_post(self):
         r = requests.post(httpbin('post'),
-                          data={'stuff': u'ëlïxr'},
+                          data={'stuff': u('ëlïxr')},
                           files={'file': ('test_requests.py', open(__file__, 'rb'))})
         assert r.status_code == 200
 
         r = requests.post(httpbin('post'),
-                          data={'stuff': u'ëlïxr'.encode('utf-8')},
+                          data={'stuff': u('ëlïxr').encode('utf-8')},
                           files={'file': ('test_requests.py', open(__file__, 'rb'))})
         assert r.status_code == 200
 
@@ -488,7 +496,7 @@ class RequestsTestCase(unittest.TestCase):
 
     def test_unicode_method_name(self):
         files = {'file': open('test_requests.py', 'rb')}
-        r = requests.request(method=u'POST', url=httpbin('post'), files=files)
+        r = requests.request(method=u('POST'), url=httpbin('post'), files=files)
         assert r.status_code == 200
 
     def test_custom_content_type(self):
@@ -865,7 +873,7 @@ class RequestsTestCase(unittest.TestCase):
         assert r.url == url
 
     def test_header_keys_are_native(self):
-        headers = {u'unicode': 'blah', 'byte'.encode('ascii'): 'blah'}
+        headers = {u('unicode'): 'blah', 'byte'.encode('ascii'): 'blah'}
         r = requests.Request('GET', httpbin('get'), headers=headers)
         p = r.prepare()