Sergey Shepelev | 0112eff | 2017-05-05 06:46:43 +0300 | [diff] [blame^] | 1 | '''Warning: these tests modify os.environ global state. |
| 2 | Each test must be run in separate process. |
| 3 | Must use pytest --forked or similar technique. |
| 4 | ''' |
| 5 | import httplib2 |
| 6 | import os |
| 7 | import pytest |
| 8 | import sys |
| 9 | # import tests |
| 10 | |
| 11 | |
| 12 | def test_from_url(): |
| 13 | pi = httplib2.proxy_info_from_url('http://myproxy.example.com') |
| 14 | assert pi.proxy_host == 'myproxy.example.com' |
| 15 | assert pi.proxy_port == 80 |
| 16 | assert pi.proxy_user is None |
| 17 | |
| 18 | |
| 19 | def test_from_url_ident(): |
| 20 | pi = httplib2.proxy_info_from_url('http://zoidberg:fish@someproxy:99') |
| 21 | assert pi.proxy_host == 'someproxy' |
| 22 | assert pi.proxy_port == 99 |
| 23 | assert pi.proxy_user == 'zoidberg' |
| 24 | assert pi.proxy_pass == 'fish' |
| 25 | |
| 26 | |
| 27 | def test_from_env(): |
| 28 | os.environ['http_proxy'] = 'http://myproxy.example.com:8080' |
| 29 | pi = httplib2.proxy_info_from_environment() |
| 30 | assert pi.proxy_host == 'myproxy.example.com' |
| 31 | assert pi.proxy_port == 8080 |
| 32 | |
| 33 | |
| 34 | def test_from_env_https(): |
| 35 | os.environ['http_proxy'] = 'http://myproxy.example.com:80' |
| 36 | os.environ['https_proxy'] = 'http://myproxy.example.com:81' |
| 37 | pi = httplib2.proxy_info_from_environment('https') |
| 38 | assert pi.proxy_host == 'myproxy.example.com' |
| 39 | assert pi.proxy_port == 81 |
| 40 | |
| 41 | |
| 42 | def test_from_env_none(): |
| 43 | os.environ.clear() |
| 44 | pi = httplib2.proxy_info_from_environment() |
| 45 | assert pi is None |
| 46 | |
| 47 | |
| 48 | @pytest.mark.skipif(sys.version_info >= (3,), reason='FIXME: https://github.com/httplib2/httplib2/issues/53') |
| 49 | def test_applies_to(): |
| 50 | os.environ['http_proxy'] = 'http://myproxy.example.com:80' |
| 51 | os.environ['https_proxy'] = 'http://myproxy.example.com:81' |
| 52 | os.environ['no_proxy'] = 'localhost,otherhost.domain.local,example.com' |
| 53 | pi = httplib2.proxy_info_from_environment() |
| 54 | assert not pi.applies_to('localhost') |
| 55 | assert pi.applies_to('www.google.com') |
| 56 | assert not pi.applies_to('www.example.com') |
| 57 | |
| 58 | |
| 59 | @pytest.mark.skipif(sys.version_info >= (3,), reason='FIXME: https://github.com/httplib2/httplib2/issues/53') |
| 60 | def test_noproxy_star(): |
| 61 | os.environ['http_proxy'] = 'http://myproxy.example.com:80' |
| 62 | os.environ['NO_PROXY'] = '*' |
| 63 | pi = httplib2.proxy_info_from_environment() |
| 64 | for host in ('localhost', '169.254.38.192', 'www.google.com'): |
| 65 | assert not pi.applies_to(host) |
| 66 | |
| 67 | |
| 68 | @pytest.mark.skipif(sys.version_info >= (3,), reason='FIXME: https://github.com/httplib2/httplib2/issues/53') |
| 69 | def test_headers(): |
| 70 | headers = {'key0': 'val0', 'key1': 'val1'} |
| 71 | pi = httplib2.ProxyInfo(httplib2.socks.PROXY_TYPE_HTTP, 'localhost', 1234, proxy_headers=headers) |
| 72 | assert pi.proxy_headers == headers |