Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

244 lines · 7.3 KiB · Python

Last changed by

Merge pull request #3036 from Lukasa/issue/3035Ian Cordascobefore adoption190cddd

History of this file

Take it

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 1–21why, one step back
1# coding: utf-8
2import os3from io import BytesIO45import pytest6from requests import compat7from requests.utils import (8 address_in_network, dotted_netmask,9 get_auth_from_url, get_encodings_from_content,10 get_environ_proxies, guess_filename,11 is_ipv4_address, is_valid_cidr, requote_uri,12 select_proxy, super_len)1314from .compat import StringIO, cStringIO151617class TestSuperLen:18 @pytest.mark.parametrize(19 'stream, value', (20 (StringIO.StringIO, 'Test'),21 (BytesIO, b'Test'),

syntax changesKenneth Reitzbefore adoptionbbadf47

line 22why, one step back
22 pytest.mark.skipif('cStringIO is None')((cStringIO, 'Test')),

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 23–24why, one step back
23 ))
24 def test_io_streams(self, stream, value):

syntax changesKenneth Reitzbefore adoptionbbadf47

line 25why, one step back
25 """Ensures that we properly deal with different kinds of IO streams."""

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 26–33why, one step back
26 assert super_len(stream()) == 0
27 assert super_len(stream(value)) == 42829 def test_super_len_correctly_calculates_len_of_partially_read_file(self):30 """Ensure that we handle partially consumed file like objects."""31 s = StringIO.StringIO()32 s.write('foobarbogus')33 assert super_len(s) == 0

Merge pull request #3036 from Lukasa/issue/3035Ian Cordascobefore adoption190cddd

lines 34–47why, one step back
34
35 @pytest.mark.parametrize('error', [IOError, OSError])36 def test_super_len_handles_files_raising_weird_errors_in_tell(self, error):37 """38 If tell() raises errors, assume the cursor is at position zero.39 """40 class BoomFile(object):41 def __len__(self):42 return 54344 def tell(self):45 raise error()4647 assert super_len(BoomFile()) == 0

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 48–51why, one step back
48
4950class TestGetEnvironProxies:51 """Ensures that IP addresses are correctly matches with ranges

syntax changesKenneth Reitzbefore adoptionbbadf47

line 52why, one step back
52 in no_proxy variable."""

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 53–66why, one step back
53
54 @pytest.yield_fixture(scope='class', autouse=True, params=['no_proxy', 'NO_PROXY'])55 def no_proxy(self, request):56 os.environ[request.param] = '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1'57 yield58 del os.environ[request.param]5960 @pytest.mark.parametrize(61 'url', (62 'http://192.168.0.1:5000/',63 'http://192.168.0.1/',64 'http://172.16.1.1/',65 'http://172.16.1.1:5000/',66 'http://localhost.localdomain:5000/v1.0/',

syntax changesKenneth Reitzbefore adoptionbbadf47

line 67why, one step back
67 ))

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 68–75why, one step back
68 def test_bypass(self, url):
69 assert get_environ_proxies(url) == {}7071 @pytest.mark.parametrize(72 'url', (73 'http://192.168.1.1:5000/',74 'http://192.168.1.1/',75 'http://www.requests.com/',

syntax changesKenneth Reitzbefore adoptionbbadf47

line 76why, one step back
76 ))

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 77–102why, one step back
77 def test_not_bypass(self, url):
78 assert get_environ_proxies(url) != {}798081class TestIsIPv4Address:8283 def test_valid(self):84 assert is_ipv4_address('8.8.8.8')8586 @pytest.mark.parametrize('value', ('8.8.8.8.8', 'localhost.localdomain'))87 def test_invalid(self, value):88 assert not is_ipv4_address(value)899091class TestIsValidCIDR:9293 def test_valid(self):94 assert is_valid_cidr('192.168.1.0/24')9596 @pytest.mark.parametrize(97 'value', (98 '8.8.8.8',99 '192.168.1.0/a',100 '192.168.1.0/128',101 '192.168.1.0/-1',102 '192.168.1.999/24',

syntax changesKenneth Reitzbefore adoptionbbadf47

line 103why, one step back
103 ))

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 104–128why, one step back
104 def test_invalid(self, value):
105 assert not is_valid_cidr(value)106107108class TestAddressInNetwork:109110 def test_valid(self):111 assert address_in_network('192.168.1.1', '192.168.1.0/24')112113 def test_invalid(self):114 assert not address_in_network('172.16.0.1', '192.168.1.0/24')115116117class TestGuessFilename:118119 @pytest.mark.parametrize(120 'value', (1, type('Fake', (object,), {'name': 1})()),121 )122 def test_guess_filename_invalid(self, value):123 assert guess_filename(value) is None124125 @pytest.mark.parametrize(126 'value, expected_type', (127 (b'value', compat.bytes),128 (b'value'.decode('utf-8'), compat.str)

syntax changesKenneth Reitzbefore adoptionbbadf47

line 129why, one step back
129 ))

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 130–152why, one step back
130 def test_guess_filename_valid(self, value, expected_type):
131 obj = type('Fake', (object,), {'name': value})()132 result = guess_filename(obj)133 assert result == value134 assert isinstance(result, expected_type)135136137class TestContentEncodingDetection:138139 def test_none(self):140 encodings = get_encodings_from_content('')141 assert not len(encodings)142143 @pytest.mark.parametrize(144 'content', (145 # HTML5 meta charset attribute146 '<meta charset="UTF-8">',147 # HTML4 pragma directive148 '<meta http-equiv="Content-type" content="text/html;charset=UTF-8">',149 # XHTML 1.x served with text/html MIME type150 '<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />',151 # XHTML 1.x served as XML152 '<?xml version="1.0" encoding="UTF-8"?>',

syntax changesKenneth Reitzbefore adoptionbbadf47

line 153why, one step back
153 ))

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 154–199why, one step back
154 def test_pragmas(self, content):
155 encodings = get_encodings_from_content(content)156 assert len(encodings) == 1157 assert encodings[0] == 'UTF-8'158159 def test_precedence(self):160 content = '''161 <?xml version="1.0" encoding="XML"?>162 <meta charset="HTML5">163 <meta http-equiv="Content-type" content="text/html;charset=HTML4" />164 '''.strip()165 assert get_encodings_from_content(content) == ['HTML5', 'HTML4', 'XML']166167168USER = PASSWORD = "%!*'();:@&=+$,/?#[] "169ENCODED_USER = compat.quote(USER, '')170ENCODED_PASSWORD = compat.quote(PASSWORD, '')171172173@pytest.mark.parametrize(174 'url, auth', (175 (176 'http://' + ENCODED_USER + ':' + ENCODED_PASSWORD + '@' +177 'request.com/url.html#test',178 (USER, PASSWORD)179 ),180 (181 'http://user:pass@complex.url.com/path?query=yes',182 ('user', 'pass')183 ),184 (185 'http://user:pass%20pass@complex.url.com/path?query=yes',186 ('user', 'pass pass')187 ),188 (189 'http://user:pass pass@complex.url.com/path?query=yes',190 ('user', 'pass pass')191 ),192 (193 'http://user%25user:pass@complex.url.com/path?query=yes',194 ('user%user', 'pass')195 ),196 (197 'http://user:pass%23pass@complex.url.com/path?query=yes',198 ('user', 'pass#pass')199 ),

syntax changesKenneth Reitzbefore adoptionbbadf47

line 200why, one step back
200 ))

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 201–216why, one step back
201def test_get_auth_from_url(url, auth):
202 assert get_auth_from_url(url) == auth203204205@pytest.mark.parametrize(206 'uri, expected', (207 (208 # Ensure requoting doesn't break expectations209 'http://example.com/fiz?buz=%25ppicture',210 'http://example.com/fiz?buz=%25ppicture',211 ),212 (213 # Ensure we handle unquoted percent signs in redirects214 'http://example.com/fiz?buz=%ppicture',215 'http://example.com/fiz?buz=%25ppicture',216 ),

syntax changesKenneth Reitzbefore adoptionbbadf47

line 217why, one step back
217 ))

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 218–228why, one step back
218def test_requote_uri_with_unquoted_percents(uri, expected):
219 """See: https://github.com/kennethreitz/requests/issues/2356220 """221 assert requote_uri(uri) == expected222223224@pytest.mark.parametrize(225 'mask, expected', (226 (8, '255.0.0.0'),227 (24, '255.255.255.0'),228 (25, '255.255.255.128'),

syntax changesKenneth Reitzbefore adoptionbbadf47

line 229why, one step back
229 ))

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 230–238why, one step back
230def test_dotted_netmask(mask, expected):
231 assert dotted_netmask(mask) == expected232233234@pytest.mark.parametrize(235 'url, expected', (236 ('hTTp://u:p@Some.Host/path', 'http://some.host.proxy'),237 ('hTTp://u:p@Other.Host/path', 'http://http.proxy'),238 ('hTTps://Other.Host', None),

syntax changesKenneth Reitzbefore adoptionbbadf47

line 239why, one step back
239 ))

Added tests module.Dmitry Dygalobefore adoption18b26d2

lines 240–244why, one step back
240def test_select_proxies(url, expected):
241 """Make sure we can select per-host proxies correctly."""242 proxies = {'http': 'http://http.proxy',243 'http://some.host': 'http://some.host.proxy'}244 assert select_proxy(url, proxies) == expected