def test_merge(self): options = { 'proxy': { 'https': 'https://*****:*****@server3:8888', 'no_proxy': 'localhost' } } with self.set_env(HTTP_PROXY='http://*****:*****@server1:8888', HTTPS_PROXY='https://*****:*****@server2:8888', NO_PROXY='127.0.0.1'): proxy = get_upstream_proxy(options) http = proxy['http'] self.assertEqual('http', http.scheme) self.assertEqual('username1', http.username) self.assertEqual('password1', http.password) self.assertEqual('server1:8888', http.hostport) # The dict config overrides that defined in env variables https = proxy['https'] self.assertEqual('https', https.scheme) self.assertEqual('username3', https.username) self.assertEqual('password3', https.password) self.assertEqual('server3:8888', https.hostport) self.assertEqual(['localhost'], proxy['no_proxy'])
def _get_upstream_proxy_args(self): proxy_config = get_upstream_proxy(self.options) http_proxy = proxy_config.get('http') https_proxy = proxy_config.get('https') conf = None if http_proxy and https_proxy: if http_proxy.hostport != https_proxy.hostport: # We only support a single upstream proxy server raise ValueError( 'Different settings for http and https proxy servers not supported' ) conf = https_proxy elif http_proxy: conf = http_proxy elif https_proxy: conf = https_proxy args = {} if conf: scheme, username, password, hostport = conf args['mode'] = 'upstream:{}://{}'.format(scheme, hostport) if username: args['upstream_auth'] = '{}:{}'.format(username, password) custom_auth = proxy_config.get('custom_authorization') if custom_auth: args['upstream_custom_auth'] = custom_auth return args
def _get_upstream_proxy_args(self): proxy_config = get_upstream_proxy(self.options) http_proxy = proxy_config.get('http') https_proxy = proxy_config.get('https') conf = None if http_proxy and https_proxy: if http_proxy.hostport != https_proxy.hostport: # We only support a single upstream mitmproxy server raise ValueError('Cannot specify both http AND https ' 'mitmproxy settings with mitmproxy backend') conf = https_proxy elif http_proxy: conf = http_proxy elif https_proxy: conf = https_proxy args = {} if conf: scheme, username, password, hostport = conf args['mode'] = 'upstream:{}://{}'.format(scheme, hostport) if username: args['upstream_auth'] = '{}:{}'.format(username, password) return args
def proxy(self, proxy_conf: Dict[str, Any]): """Set the proxy configuration for the driver. The configuration should be a dictionary: webdriver.proxy = { 'https': 'https://*****:*****@server:port', 'no_proxy': 'localhost,127.0.0.1', } Args: proxy_conf: The proxy configuration. """ options = self.backend.master.options if proxy_conf: options.update(**utils.build_proxy_args( utils.get_upstream_proxy({'proxy': proxy_conf}))) else: options.update( **{ utils.MITM_MODE: options.default(utils.MITM_MODE), utils.MITM_UPSTREAM_AUTH: options.default(utils.MITM_UPSTREAM_AUTH), utils.MITM_UPSTREAM_CUSTOM_AUTH: options.default(utils.MITM_UPSTREAM_CUSTOM_AUTH), utils.MITM_NO_PROXY: options.default(utils.MITM_NO_PROXY), })
def test_empty_password(self): options = { 'proxy': { 'https': 'https://username:@server:8888', } } proxy = get_upstream_proxy(options) https = proxy['https'] self.assertEqual('https', https.scheme) self.assertEqual('username', https.username) self.assertEqual('', https.password) self.assertEqual('server:8888', https.hostport)
def test_no_proxy_as_list(self): options = { 'proxy': { 'https': 'https://username:@server:8888', 'no_proxy': ['localhost:8081', 'example.com', 'test.com:80'] } } proxy = get_upstream_proxy(options) self.assertEqual([ 'localhost:8081', 'example.com', 'test.com:80' ], proxy['no_proxy'])
def test_get_from_env(self): with self.set_env(HTTP_PROXY='http://*****:*****@server1:8888', HTTPS_PROXY='https://*****:*****@server2:8888', NO_PROXY='localhost'): proxy = get_upstream_proxy({}) http = proxy['http'] self.assertEqual('http', http.scheme) self.assertEqual('username1', http.username) self.assertEqual('password1', http.password) self.assertEqual('server1:8888', http.hostport) https = proxy['https'] self.assertEqual('https', https.scheme) self.assertEqual('username2', https.username) self.assertEqual('password2', https.password) self.assertEqual('server2:8888', https.hostport) self.assertEqual(['localhost'], proxy['no_proxy'])
def test_get_config(self): options = { 'proxy': { 'http': 'http://*****:*****@server1:8888', 'https': 'https://*****:*****@server2:8888', 'no_proxy': 'localhost' } } proxy = get_upstream_proxy(options) http = proxy['http'] self.assertEqual('http', http.scheme) self.assertEqual('username1', http.username) self.assertEqual('password1', http.password) self.assertEqual('server1:8888', http.hostport) https = proxy['https'] self.assertEqual('https', https.scheme) self.assertEqual('username2', https.username) self.assertEqual('password2', https.password) self.assertEqual('server2:8888', https.hostport) self.assertEqual(['localhost'], proxy['no_proxy'])
def test_none(self): options = None proxy = get_upstream_proxy(options) self.assertEqual({}, proxy)