def test_extract_cert_and_key_no_check(self, mock_path): mock_path.return_value.exists.return_value = True m_open = mock_open() with patch('seleniumwire.utils.open', m_open): extract_cert_and_key(Path('some', 'path'), check_exists=False) m_open.assert_called_once()
def test_extract_cert_and_key_exists(self, mock_path): mock_path.return_value.exists.return_value = True m_open = mock_open() with patch('seleniumwire.utils.open', m_open): extract_cert_and_key(Path('some', 'path')) m_open.assert_not_called()
def __init__(self, host, port, options): self.options = options # Used to stored captured requests self.storage = RequestStorage( base_dir=options.pop('request_storage_base_dir', None)) extract_cert_and_key(self.storage.home_dir) # Used to modify requests/responses passing through the server # DEPRECATED. Will be superceded by request/response interceptors. self.modifier = RequestModifier() # The scope of requests we're interested in capturing. self.scopes = [] self.request_interceptor = None self.response_interceptor = None self._event_loop = asyncio.new_event_loop() # mitmproxy specific options mitmproxy_opts = Options( confdir=self.storage.home_dir, listen_host=host, listen_port=port, ) # Create an instance of the mitmproxy server self._master = Master(self._event_loop, mitmproxy_opts) self._master.server = ProxyServer(ProxyConfig(mitmproxy_opts)) self._master.addons.add(*addons.default_addons()) self._master.addons.add(SendToLogger()) self._master.addons.add(InterceptRequestHandler(self)) # Update the options now all addons have been added mitmproxy_opts.update( ssl_insecure=options.get('verify_ssl', True), upstream_cert=DEFAULT_UPSTREAM_CERT, stream_websockets=DEFAULT_STREAM_WEBSOCKETS, suppress_connection_errors=options.get( 'suppress_connection_errors', True), **self._get_upstream_proxy_args(), ) if options.get('disable_capture', False): self.scopes = ['$^'] mitmproxy_opts.update(ignore_hosts=['.*']) # Options that are prefixed mitm_ are passed through to mitmproxy mitmproxy_opts.update( **{k[5:]: v for k, v in options.items() if k.startswith('mitm_')})
def test_extract_cert_and_key(self, mock_path, mock_pkgutil): mock_path.return_value.exists.return_value = False mock_pkgutil.get_data.side_effect = (b'cert_data', b'key_data') m_open = mock_open() with patch('seleniumwire.utils.open', m_open): extract_cert_and_key(Path('some', 'path')) mock_path.assert_called_once_with(Path('some', 'path'), 'seleniumwire-ca.pem') mock_pkgutil.get_data.assert_has_calls([ call('seleniumwire', 'ca.crt'), call('seleniumwire', 'ca.key') ]) m_open.assert_called_once_with(mock_path.return_value, 'wb') m_open.return_value.write.assert_called_once_with(b'cert_datakey_data')