summaryrefslogtreecommitdiff
path: root/tests/test_proxy.py
blob: fe6e551bddef9c5446ca32aa1c1ca7385c2b00bf (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""
Tests for the proxy support in pip.

TODO shouldn't need to hack sys.path in here.

"""

import os
import sys
sys.path.insert(0, os.path.dirname(os.path.dirname(__file__)))

import os
import pip
import getpass
from pip.basecommand import get_proxy
from tests.test_pip import here


def new_getpass(prompt, answer='passwd'):
    print('%s%s' % (prompt, answer))
    return answer


def test_correct_pip_version():
    """
    Check we are importing pip from the right place.

    """
    base = os.path.dirname(here)
    assert pip.__file__.startswith(base), pip.__file__


def test_remove_proxy():
    """
    Test removing proxy from environ.

    """
    if 'HTTP_PROXY' in os.environ:
        del os.environ['HTTP_PROXY']
    assert get_proxy() == None
    os.environ['HTTP_PROXY'] = 'user:pwd@server.com:port'
    assert get_proxy() == 'user:pwd@server.com:port'
    del os.environ['HTTP_PROXY']
    assert get_proxy('server.com') == 'server.com'
    assert get_proxy('server.com:80') == 'server.com:80'
    assert get_proxy('user:passwd@server.com:3128') == 'user:passwd@server.com:3128'


def test_get_proxy():
    """
    Test get_proxy returns correct proxy info.

    """
    # monkeypatch getpass.getpass, to avoid asking for a password
    old_getpass = getpass.getpass
    getpass.getpass = new_getpass

    # Test it:
    assert get_proxy('user:@server.com:3128') == 'user:@server.com:3128'
    assert get_proxy('user@server.com:3128') == 'user:passwd@server.com:3128'

    # Undo monkeypatch
    getpass.getpass = old_getpass