summaryrefslogtreecommitdiff
path: root/test/test_proxymanager.py
blob: 16bf4349d12e2f1880e0c609871dd1ded2b78354 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import pytest

from urllib3.poolmanager import ProxyManager


class TestProxyManager(object):
    def test_proxy_headers(self):
        url = 'http://pypi.org/project/urllib3/'
        with ProxyManager('http://something:1234') as p:

            # Verify default headers
            default_headers = {'Accept': '*/*',
                               'Host': 'pypi.org'}
            headers = p._set_proxy_headers(url)

            assert headers == default_headers

            # Verify default headers don't overwrite provided headers
            provided_headers = {'Accept': 'application/json',
                                'custom': 'header',
                                'Host': 'test.python.org'}
            headers = p._set_proxy_headers(url, provided_headers)

            assert headers == provided_headers

            # Verify proxy with nonstandard port
            provided_headers = {'Accept': 'application/json'}
            expected_headers = provided_headers.copy()
            expected_headers.update({'Host': 'pypi.org:8080'})
            url_with_port = 'http://pypi.org:8080/project/urllib3/'
            headers = p._set_proxy_headers(url_with_port, provided_headers)

            assert headers == expected_headers

    def test_default_port(self):
        with ProxyManager('http://something') as p:
            assert p.proxy.port == 80
        with ProxyManager('https://something') as p:
            assert p.proxy.port == 443

    def test_invalid_scheme(self):
        with pytest.raises(AssertionError):
            ProxyManager('invalid://host/p')
        with pytest.raises(ValueError):
            ProxyManager('invalid://host/p')