Skip to content
Myr

test: restore Python 3.2 compatibility

Before adoption: this commit came in with the history the project adopted, so no landing records it.

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.

authored by Martin Geisler · committed by Martin Geisler · parent f4d1bbb · Browse files · 1 file+14 added−6 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+14 added−6 removed

  • other:+14 added−6 removed

1 file+14 added−6 removed

modified:test_requests.py+14 added-6 removed
14 unmodified lines
15import pytest16from requests.adapters import HTTPAdapter17from requests.auth import HTTPDigestAuth18from requests.compat import (19 Morsel, cookielib, getproxies, str, urljoin, urlparse)19 Morsel, cookielib, getproxies, str, urljoin, urlparse, is_py3)20from requests.cookies import cookiejar_from_dict, morsel_to_cookie21from requests.exceptions import InvalidURL, MissingSchema22from requests.models import PreparedRequest, Response23from requests.structures import CaseInsensitiveDict
5 unmodified lines
29 import StringIO30except ImportError:31 import io as StringIO3233if is_py3:34 def u(s):35 return s36else:37 def u(s):38 return s.decode('unicode-escape')394041HTTPBIN = os.environ.get('HTTPBIN_URL', 'http://httpbin.org/')42# Issue #1483: Make sure the URL always has a trailing slash43HTTPBIN = HTTPBIN.rstrip('/') + '/'44
371 unmodified lines
416 def test_conflicting_post_params(self):417 url = httpbin('post')418 with open('requirements.txt') as f:419 pytest.raises(ValueError, "requests.post(url, data='[{\"some\": \"data\"}]', files={'some': f})")412 pytest.raises(ValueError, "requests.post(url, data=u'[{\"some\": \"data\"}]', files={'some': f})")420 pytest.raises(ValueError, "requests.post(url, data=u('[{\"some\": \"data\"}]'), files={'some': f})")421422 def test_request_ok_set(self):423 r = requests.get(httpbin('status', '404'))424 assert not r.ok
38 unmodified lines
463 assert r.status_code == 200464465 def test_unicode_multipart_post(self):466 r = requests.post(httpbin('post'),459 data={'stuff': u'ëlïxr'},467 data={'stuff': u('ëlïxr')},468 files={'file': ('test_requests.py', open(__file__, 'rb'))})469 assert r.status_code == 200470471 r = requests.post(httpbin('post'),464 data={'stuff': u'ëlïxr'.encode('utf-8')},472 data={'stuff': u('ëlïxr').encode('utf-8')},473 files={'file': ('test_requests.py', open(__file__, 'rb'))})474 assert r.status_code == 200475476 r = requests.post(httpbin('post'),
18 unmodified lines
495 assert b'name="b\'stuff\'"' not in prep.body496497 def test_unicode_method_name(self):498 files = {'file': open('test_requests.py', 'rb')}491 r = requests.request(method=u'POST', url=httpbin('post'), files=files)499 r = requests.request(method=u('POST'), url=httpbin('post'), files=files)500 assert r.status_code == 200501502 def test_custom_content_type(self):503 r = requests.post(httpbin('post'),
368 unmodified lines
872 r = requests.Request('GET', url).prepare()873 assert r.url == url874875 def test_header_keys_are_native(self):868 headers = {u'unicode': 'blah', 'byte'.encode('ascii'): 'blah'}876 headers = {u('unicode'): 'blah', 'byte'.encode('ascii'): 'blah'}877 r = requests.Request('GET', httpbin('get'), headers=headers)878 p = r.prepare()879880 # This is testing that they are builtin strings. A bit weird, but there
494 unmodified lines