Пример #1
0
    def test_popen_defaults(self, mock_subprocess, mock_socket):
        mock_socket.socket.return_value.connect_ex.return_value = 0

        mitmproxy.start('127.0.0.1', 9950, {})

        mock_subprocess.Popen.assert_called_once_with(
            ['mitmdump', *self._popen_args()])
Пример #2
0
    def test_ssl_insecure(self, mock_subprocess, mock_socket):
        mock_socket.socket.return_value.connect_ex.return_value = 0

        mitmproxy.start('127.0.0.1', 9950, {'verify_ssl': False})

        mock_subprocess.Popen.assert_called_once_with(
            ['mitmdump', *self._popen_args(ssl_insecure=False)])
Пример #3
0
    def test_confdir(self, mock_subprocess, mock_socket):
        confdir = '/tmp/.mitmproxy'
        mock_socket.socket.return_value.connect_ex.return_value = 0

        mitmproxy.start('127.0.0.1', 9950, {'mitmproxy_confdir': confdir})

        mock_subprocess.Popen.assert_called_once_with(
            ['mitmdump', *self._popen_args(confdir=confdir)])
Пример #4
0
    def test_port(self, mock_subprocess, mock_socket):
        port = 8888
        mock_socket.socket.return_value.connect_ex.return_value = 0

        mitmproxy.start('127.0.0.1', port, {})

        mock_subprocess.Popen.assert_called_once_with(
            ['mitmdump', *self._popen_args(port=port)])
Пример #5
0
    def test_upstream_proxy_auth(self):
        options = {'proxy': {'https': 'https://*****:*****@proxyserver:8080'}}

        mitmproxy.start('127.0.0.1', 9950, options)

        self.mock_subprocess.Popen.assert_called_once_with([
            'mitmdump', '--set', 'mode=upstream:https://proxyserver:8080',
            '--set', 'upstream_auth=user:pass', *self._popen_args()
        ])
Пример #6
0
    def test_exceed_max_retries(self):
        # Indicates a problem, as wait() immediately returns
        self.mock_subprocess.Popen.return_value.wait.side_effect = lambda **kwargs: None

        with self.assertRaises(RuntimeError):
            mitmproxy.start('127.0.0.1', 0, {})

        self.assertEqual(mitmproxy.RETRIES,
                         self.mock_subprocess.Popen.call_count)
Пример #7
0
    def test_upstream_proxy_auth(self, mock_subprocess, mock_socket):
        mock_socket.socket.return_value.connect_ex.return_value = 0
        options = {'proxy': {'https': 'https://*****:*****@proxyserver:8080'}}

        mitmproxy.start('127.0.0.1', 9950, options)

        mock_subprocess.Popen.assert_called_once_with([
            'mitmdump', '--set', 'mode=upstream:https://proxyserver:8080',
            '--set', 'upstream_auth=user:pass', *self._popen_args()
        ])
Пример #8
0
    def test_retry_when_failed_start(self):
        self.mock_subprocess.Popen.return_value.wait.side_effect = [
            lambda **kwargs:
            None,  # Indicates a problem, as wait() immediately returns
            subprocess.TimeoutExpired('cmd', 2)  # Success as wait() times out
        ]

        mitmproxy.start('127.0.0.1', 0, {})

        # Mitmproxy took two attempts to start
        self.assertEqual(2, self.mock_subprocess.Popen.call_count)
Пример #9
0
    def test_upstream_proxy_different(self):
        options = {
            'proxy': {
                'http': 'http://proxyserver1:8080',
                'https': 'https://proxyserver2:8080'
            }
        }

        with self.assertRaises(ValueError):
            mitmproxy.start('127.0.0.1', 9950, options)

        self.assertEqual(0, self.mock_subprocess.Popen.call_count)
Пример #10
0
    def test_upstream_proxy_different(self, mock_subprocess, mock_socket):
        mock_socket.socket.return_value.connect_ex.return_value = 0
        options = {
            'proxy': {
                'http': 'http://proxyserver1:8080',
                'https': 'https://proxyserver2:8080'
            }
        }

        with self.assertRaises(ValueError):
            mitmproxy.start('127.0.0.1', 9950, options)

        self.assertEqual(0, mock_subprocess.Popen.call_count)
Пример #11
0
    def test_upstream_proxy(self):
        options = {
            'proxy': {
                'http': 'http://proxyserver:8080',
                # We pick https when both are specified and the same
                'https': 'https://proxyserver:8080'
            }
        }

        mitmproxy.start('127.0.0.1', 9950, options)

        self.mock_subprocess.Popen.assert_called_once_with([
            'mitmdump', '--set', 'mode=upstream:https://proxyserver:8080',
            *self._popen_args()
        ])
Пример #12
0
    def test_popen_defaults(self):
        p = mitmproxy.start('127.0.0.1', 0, {})

        self.mock_subprocess.Popen.assert_called_once_with(
            ['mitmdump', *self._popen_args(port=p.port)])
        self.assertTrue(
            mitmproxy.PORT_RANGE_START <= p.port <= mitmproxy.PORT_RANGE_END)
Пример #13
0
    def test_ssl_insecure(self):
        mitmproxy.start('127.0.0.1', 9950, {'verify_ssl': False})

        self.mock_subprocess.Popen.assert_called_once_with(
            ['mitmdump', *self._popen_args(ssl_insecure=False)])
Пример #14
0
    def test_confdir(self):
        confdir = '/tmp/.mitmproxy'
        mitmproxy.start('127.0.0.1', 9950, {'mitmproxy_confdir': confdir})

        self.mock_subprocess.Popen.assert_called_once_with(
            ['mitmdump', *self._popen_args(confdir=confdir)])
Пример #15
0
    def test_port(self):
        port = 8888
        mitmproxy.start('127.0.0.1', port, {})

        self.mock_subprocess.Popen.assert_called_once_with(
            ['mitmdump', *self._popen_args(port=port)])