Skip to content
Myr

test_utils.py

This file

Path

tests/test_utils.py

232 lines · 6.9 KiB · Python

Last changed by

Remove unwanted changesBraulio Valdivielso Martínezbefore adoption3c3bde5

History of this file

Take it

Merged masterBraulio Valdivielso Martínezbefore adoption141db28

lines 1–231why, 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, cStringIO15161718class TestSuperLen:1920 @pytest.mark.parametrize(21 'stream, value', (22 (StringIO.StringIO, 'Test'),23 (BytesIO, b'Test'),24 pytest.mark.skipif('cStringIO is None')((cStringIO, 'Test')),25 ))26 def test_io_streams(self, stream, value):27 """Ensures that we properly deal with different kinds of IO streams."""28 assert super_len(stream()) == 029 assert super_len(stream(value)) == 43031 def test_super_len_correctly_calculates_len_of_partially_read_file(self):32 """Ensure that we handle partially consumed file like objects."""33 s = StringIO.StringIO()34 s.write('foobarbogus')35 assert super_len(s) == 0363738class TestGetEnvironProxies:39 """Ensures that IP addresses are correctly matches with ranges40 in no_proxy variable."""4142 @pytest.yield_fixture(scope='class', autouse=True, params=['no_proxy', 'NO_PROXY'])43 def no_proxy(self, request):44 os.environ[request.param] = '192.168.0.0/24,127.0.0.1,localhost.localdomain,172.16.1.1'45 yield46 del os.environ[request.param]4748 @pytest.mark.parametrize(49 'url', (50 'http://192.168.0.1:5000/',51 'http://192.168.0.1/',52 'http://172.16.1.1/',53 'http://172.16.1.1:5000/',54 'http://localhost.localdomain:5000/v1.0/',55 ))56 def test_bypass(self, url):57 assert get_environ_proxies(url) == {}5859 @pytest.mark.parametrize(60 'url', (61 'http://192.168.1.1:5000/',62 'http://192.168.1.1/',63 'http://www.requests.com/',64 ))65 def test_not_bypass(self, url):66 assert get_environ_proxies(url) != {}676869class TestIsIPv4Address:7071 def test_valid(self):72 assert is_ipv4_address('8.8.8.8')7374 @pytest.mark.parametrize('value', ('8.8.8.8.8', 'localhost.localdomain'))75 def test_invalid(self, value):76 assert not is_ipv4_address(value)777879class TestIsValidCIDR:8081 def test_valid(self):82 assert is_valid_cidr('192.168.1.0/24')8384 @pytest.mark.parametrize(85 'value', (86 '8.8.8.8',87 '192.168.1.0/a',88 '192.168.1.0/128',89 '192.168.1.0/-1',90 '192.168.1.999/24',91 ))92 def test_invalid(self, value):93 assert not is_valid_cidr(value)949596class TestAddressInNetwork:9798 def test_valid(self):99 assert address_in_network('192.168.1.1', '192.168.1.0/24')100101 def test_invalid(self):102 assert not address_in_network('172.16.0.1', '192.168.1.0/24')103104105class TestGuessFilename:106107 @pytest.mark.parametrize(108 'value', (1, type('Fake', (object,), {'name': 1})()),109 )110 def test_guess_filename_invalid(self, value):111 assert guess_filename(value) is None112113 @pytest.mark.parametrize(114 'value, expected_type', (115 (b'value', compat.bytes),116 (b'value'.decode('utf-8'), compat.str)117 ))118 def test_guess_filename_valid(self, value, expected_type):119 obj = type('Fake', (object,), {'name': value})()120 result = guess_filename(obj)121 assert result == value122 assert isinstance(result, expected_type)123124125class TestContentEncodingDetection:126127 def test_none(self):128 encodings = get_encodings_from_content('')129 assert not len(encodings)130131 @pytest.mark.parametrize(132 'content', (133 # HTML5 meta charset attribute134 '<meta charset="UTF-8">',135 # HTML4 pragma directive136 '<meta http-equiv="Content-type" content="text/html;charset=UTF-8">',137 # XHTML 1.x served with text/html MIME type138 '<meta http-equiv="Content-type" content="text/html;charset=UTF-8" />',139 # XHTML 1.x served as XML140 '<?xml version="1.0" encoding="UTF-8"?>',141 ))142 def test_pragmas(self, content):143 encodings = get_encodings_from_content(content)144 assert len(encodings) == 1145 assert encodings[0] == 'UTF-8'146147 def test_precedence(self):148 content = '''149 <?xml version="1.0" encoding="XML"?>150 <meta charset="HTML5">151 <meta http-equiv="Content-type" content="text/html;charset=HTML4" />152 '''.strip()153 assert get_encodings_from_content(content) == ['HTML5', 'HTML4', 'XML']154155USER = 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 ),187 ))188def 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 ),204 ))205def 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'),216 ))217def 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),226 ))227def 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

Remove unwanted changesBraulio Valdivielso Martínezbefore adoption3c3bde5

line 232why, one step back
232