test_utils.py
This file
Path
tests/test_utils.py
231 lines · 6.9 KiB · Python
Last changed by
syntax changesKenneth Reitzbefore adoptionbbadf47
History of this fileTake it
Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 1–22why, one step back1# coding: utf-82import 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:1819 @pytest.mark.parametrize(20 'stream, value', (21 (StringIO.StringIO, 'Test'),22 (BytesIO, b'Test'),syntax changesKenneth Reitzbefore adoptionbbadf47
line 23why, one step back23 pytest.mark.skipif('cStringIO is None')((cStringIO, 'Test')),Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 24–25why, one step back24 ))25 def test_io_streams(self, stream, value):syntax changesKenneth Reitzbefore adoptionbbadf47
line 26why, one step back26 """Ensures that we properly deal with different kinds of IO streams."""Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 27–38why, one step back27 assert super_len(stream()) == 028 assert super_len(stream(value)) == 42930 def test_super_len_correctly_calculates_len_of_partially_read_file(self):31 """Ensure that we handle partially consumed file like objects."""32 s = StringIO.StringIO()33 s.write('foobarbogus')34 assert super_len(s) == 0353637class TestGetEnvironProxies:38 """Ensures that IP addresses are correctly matches with rangessyntax changesKenneth Reitzbefore adoptionbbadf47
line 39why, one step back39 in no_proxy variable."""Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 40–53why, one step back4041 @pytest.yield_fixture(scope='class', autouse=True, params=['no_proxy', 'NO_PROXY'])42 def no_proxy(self, request):43 os.environ[request.param] = '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1'44 yield45 del os.environ[request.param]4647 @pytest.mark.parametrize(48 'url', (49 'http://192.168.0.1:5000/',50 'http://192.168.0.1/',51 'http://172.16.1.1/',52 'http://172.16.1.1:5000/',53 'http://localhost.localdomain:5000/v1.0/',syntax changesKenneth Reitzbefore adoptionbbadf47
line 54why, one step back54 ))Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 55–62why, one step back55 def test_bypass(self, url):56 assert get_environ_proxies(url) == {}5758 @pytest.mark.parametrize(59 'url', (60 'http://192.168.1.1:5000/',61 'http://192.168.1.1/',62 'http://www.requests.com/',syntax changesKenneth Reitzbefore adoptionbbadf47
line 63why, one step back63 ))Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 64–89why, one step back64 def test_not_bypass(self, url):65 assert get_environ_proxies(url) != {}666768class TestIsIPv4Address:6970 def test_valid(self):71 assert is_ipv4_address('8.8.8.8')7273 @pytest.mark.parametrize('value', ('8.8.8.8.8', 'localhost.localdomain'))74 def test_invalid(self, value):75 assert not is_ipv4_address(value)767778class TestIsValidCIDR:7980 def test_valid(self):81 assert is_valid_cidr('192.168.1.0/24')8283 @pytest.mark.parametrize(84 'value', (85 '8.8.8.8',86 '192.168.1.0/a',87 '192.168.1.0/128',88 '192.168.1.0/-1',89 '192.168.1.999/24',syntax changesKenneth Reitzbefore adoptionbbadf47
line 90why, one step back90 ))Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 91–115why, one step back91 def test_invalid(self, value):92 assert not is_valid_cidr(value)939495class TestAddressInNetwork:9697 def test_valid(self):98 assert address_in_network('192.168.1.1', '192.168.1.0/24')99100 def test_invalid(self):101 assert not address_in_network('172.16.0.1', '192.168.1.0/24')102103104class TestGuessFilename:105106 @pytest.mark.parametrize(107 'value', (1, type('Fake', (object,), {'name': 1})()),108 )109 def test_guess_filename_invalid(self, value):110 assert guess_filename(value) is None111112 @pytest.mark.parametrize(113 'value, expected_type', (114 (b'value', compat.bytes),115 (b'value'.decode('utf-8'), compat.str)syntax changesKenneth Reitzbefore adoptionbbadf47
line 116why, one step back116 ))Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 117–139why, one step back117 def test_guess_filename_valid(self, value, expected_type):118 obj = type('Fake', (object,), {'name': value})()119 result = guess_filename(obj)120 assert result == value121 assert isinstance(result, expected_type)122123124class TestContentEncodingDetection:125126 def test_none(self):127 encodings = get_encodings_from_content('')128 assert not len(encodings)129130 @pytest.mark.parametrize(131 'content', (132 # HTML5 meta charset attribute133 '<meta charset="UTF-8">',134 # HTML4 pragma directive135 '<meta http-equiv="Content-type" content="text/html;charset=UTF-8">',136 # XHTML 1.x served with text/html MIME type137 '<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />',138 # XHTML 1.x served as XML139 '<?xml version="1.0" encoding="UTF-8"?>',syntax changesKenneth Reitzbefore adoptionbbadf47
line 140why, one step back140 ))Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 141–186why, one step back141 def test_pragmas(self, content):142 encodings = get_encodings_from_content(content)143 assert len(encodings) == 1144 assert encodings[0] == 'UTF-8'145146 def test_precedence(self):147 content = '''148 <?xml version="1.0" encoding="XML"?>149 <meta charset="HTML5">150 <meta http-equiv="Content-type" content="text/html;charset=HTML4" />151 '''.strip()152 assert get_encodings_from_content(content) == ['HTML5', 'HTML4', 'XML']153154155USER = PASSWORD = "%!*'();:@&=+$,/?#[] "156ENCODED_USER = compat.quote(USER, '')157ENCODED_PASSWORD = compat.quote(PASSWORD, '')158159160@pytest.mark.parametrize(161 'url, auth', (162 (163 'http://' + ENCODED_USER + ':' + ENCODED_PASSWORD + '@' +164 'request.com/url.html#test',165 (USER, PASSWORD)166 ),167 (168 'http://user:pass@complex.url.com/path?query=yes',169 ('user', 'pass')170 ),171 (172 'http://user:pass%20pass@complex.url.com/path?query=yes',173 ('user', 'pass pass')174 ),175 (176 'http://user:pass pass@complex.url.com/path?query=yes',177 ('user', 'pass pass')178 ),179 (180 'http://user%25user:pass@complex.url.com/path?query=yes',181 ('user%user', 'pass')182 ),183 (184 'http://user:pass%23pass@complex.url.com/path?query=yes',185 ('user', 'pass#pass')186 ),syntax changesKenneth Reitzbefore adoptionbbadf47
line 187why, one step back187 ))Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 188–203why, one step back188def test_get_auth_from_url(url, auth):189 assert get_auth_from_url(url) == auth190191192@pytest.mark.parametrize(193 'uri, expected', (194 (195 # Ensure requoting doesn't break expectations196 'http://example.com/fiz?buz=%25ppicture',197 'http://example.com/fiz?buz=%25ppicture',198 ),199 (200 # Ensure we handle unquoted percent signs in redirects201 'http://example.com/fiz?buz=%ppicture',202 'http://example.com/fiz?buz=%25ppicture',203 ),syntax changesKenneth Reitzbefore adoptionbbadf47
line 204why, one step back204 ))Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 205–215why, one step back205def test_requote_uri_with_unquoted_percents(uri, expected):206 """See: https://github.com/kennethreitz/requests/issues/2356207 """208 assert requote_uri(uri) == expected209210211@pytest.mark.parametrize(212 'mask, expected', (213 (8, '255.0.0.0'),214 (24, '255.255.255.0'),215 (25, '255.255.255.128'),syntax changesKenneth Reitzbefore adoptionbbadf47
line 216why, one step back216 ))Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 217–225why, one step back217def test_dotted_netmask(mask, expected):218 assert dotted_netmask(mask) == expected219220221@pytest.mark.parametrize(222 'url, expected', (223 ('hTTp://u:p@Some.Host/path', 'http://some.host.proxy'),224 ('hTTp://u:p@Other.Host/path', 'http://http.proxy'),225 ('hTTps://Other.Host', None),syntax changesKenneth Reitzbefore adoptionbbadf47
line 226why, one step back226 ))Added tests module.Dmitry Dygalobefore adoption18b26d2
lines 227–231why, one step back227def test_select_proxies(url, expected):228 """Make sure we can select per-host proxies correctly."""229 proxies = {'http': 'http://http.proxy',230 'http://some.host': 'http://some.host.proxy'}231 assert select_proxy(url, proxies) == expected