summaryrefslogtreecommitdiff
path: root/tests/unit/utils_proxy_test.py
blob: ff0e14ba7411ae79d3518bdda8f45d7bc3ba2e0f (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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# -*- coding: utf-8 -*-

import unittest
import six

from docker.utils.proxy import ProxyConfig

HTTP = 'http://test:80'
HTTPS = 'https://test:443'
FTP = 'ftp://user:password@host:23'
NO_PROXY = 'localhost,.localdomain'
CONFIG = ProxyConfig(http=HTTP, https=HTTPS, ftp=FTP, no_proxy=NO_PROXY)
ENV = {
    'http_proxy': HTTP,
    'HTTP_PROXY': HTTP,
    'https_proxy': HTTPS,
    'HTTPS_PROXY': HTTPS,
    'ftp_proxy': FTP,
    'FTP_PROXY': FTP,
    'no_proxy': NO_PROXY,
    'NO_PROXY': NO_PROXY,
}


class ProxyConfigTest(unittest.TestCase):

    def test_from_dict(self):
        config = ProxyConfig.from_dict({
            'httpProxy': HTTP,
            'httpsProxy': HTTPS,
            'ftpProxy': FTP,
            'noProxy': NO_PROXY
        })
        self.assertEqual(CONFIG.http, config.http)
        self.assertEqual(CONFIG.https, config.https)
        self.assertEqual(CONFIG.ftp, config.ftp)
        self.assertEqual(CONFIG.no_proxy, config.no_proxy)

    def test_new(self):
        config = ProxyConfig()
        self.assertIsNone(config.http)
        self.assertIsNone(config.https)
        self.assertIsNone(config.ftp)
        self.assertIsNone(config.no_proxy)

        config = ProxyConfig(http='a', https='b', ftp='c', no_proxy='d')
        self.assertEqual(config.http, 'a')
        self.assertEqual(config.https, 'b')
        self.assertEqual(config.ftp, 'c')
        self.assertEqual(config.no_proxy, 'd')

    def test_truthiness(self):
        assert not ProxyConfig()
        assert ProxyConfig(http='non-zero')
        assert ProxyConfig(https='non-zero')
        assert ProxyConfig(ftp='non-zero')
        assert ProxyConfig(no_proxy='non-zero')

    def test_environment(self):
        self.assertDictEqual(CONFIG.get_environment(), ENV)
        empty = ProxyConfig()
        self.assertDictEqual(empty.get_environment(), {})

    def test_inject_proxy_environment(self):
        # Proxy config is non null, env is None.
        self.assertSetEqual(
            set(CONFIG.inject_proxy_environment(None)),
            set(['{}={}'.format(k, v) for k, v in six.iteritems(ENV)]))

        # Proxy config is null, env is None.
        self.assertIsNone(ProxyConfig().inject_proxy_environment(None), None)

        env = ['FOO=BAR', 'BAR=BAZ']

        # Proxy config is non null, env is non null
        actual = CONFIG.inject_proxy_environment(env)
        expected = ['{}={}'.format(k, v) for k, v in six.iteritems(ENV)] + env
        # It's important that the first 8 variables are the ones from the proxy
        # config, and the last 2 are the ones from the input environment
        self.assertSetEqual(set(actual[:8]), set(expected[:8]))
        self.assertSetEqual(set(actual[-2:]), set(expected[-2:]))

        # Proxy is null, and is non null
        self.assertListEqual(ProxyConfig().inject_proxy_environment(env), env)