def test_error(self): if sys.version_info < (3, ): self.skipTest('Unsupported Mock in Python 2.7') m = Mock(side_effect=DirHuntError) with patch('sys.stderr.write') as mock_write: catch(m)() m.assert_called_once() mock_write.assert_called_once()
def test_error(self): if sys.version_info < (3, ): self.skipTest('Unsupported Mock in Python 2.7') m = Mock(side_effect=KeyError) with patch('traceback.print_exc') as mock_print_exc: with self.assertRaises(KeyError): reraise_with_stack(m)() m.assert_called_once() mock_print_exc.assert_called_once()
def test_proxy_db(self): if sys.version_info < (3, ): self.skipTest('Unsupported Mock in Python 2.7') mock = Mock() mock.proxies_lists.__getitem__ = Mock() mock.proxies_lists.__getitem__.return_value.__next__ = Mock() normalize_proxy('Random', mock) mock.proxies_lists.__getitem__.assert_called_once() mock.proxies_lists.__getitem__.return_value.__next__.assert_called_once( )
def test_random_proxy_positive(self, m): proxy_instance = Mock() with patch('dirhunt.sessions.normalize_proxy', return_value=proxy_instance): sessions = Sessions() session = sessions.sessions[0] self.assertIs(session.proxy, proxy_instance) session_mock = Mock() session.session = session_mock session.get(self.url) proxy_instance.positive.assert_called_once()
def _test_random_proxy_negative(self, votes): proxy_instance = Mock() proxy_instance.get_updated_proxy.return_value.votes = votes with patch('dirhunt.sessions.normalize_proxy', return_value=proxy_instance): sessions = Sessions() session = sessions.sessions[0] self.assertIs(session.proxy, proxy_instance) session_mock = Mock(**{'get.side_effect': ProxyError}) session.session = session_mock with self.assertRaises(ProxyError): session.get(self.url) return proxy_instance
def test_keyboard_interrupt(self): m = Mock(side_effect=KeyboardInterrupt) with patch('dirhunt.utils.confirm_close', side_effect=KeyboardInterrupt) as mock_confirm_close: with self.assertRaises(KeyboardInterrupt): catch_keyboard_interrupt(m)() mock_confirm_close.assert_called_once()
def test_session_read_exception(self): crawler = self.get_crawler() crawler.sessions = Mock() crawler.sessions.get_session.return_value.get.return_value.status_code = 200 crawler.sessions.get_session.return_value.get.return_value.raw.read.side_effect = \ requests.exceptions.ConnectTimeout() with patch('dirhunt.crawler_url.CrawlerUrl.close') as m: crawler_url = CrawlerUrl(crawler, self.url) self.assertEqual(crawler_url.start(), crawler_url) self.assertEqual(crawler.current_processed_count, 1) m.assert_called_once()
def test_proxy(self): proxy = 'http://10.1.2.3:3128' sessions = Sessions([proxy]) session = sessions.sessions[0] m = Mock() session.session = m session.get(self.url) m.get.assert_called_once_with(self.url, proxies={ 'http': proxy, 'https': proxy })
def test_erase_tty(self): crawler = self.get_crawler() crawler.std = Mock(**{'isatty.return_value': True}) crawler.erase()
def test_echo(self): mstdout = Mock(**{'isatty.return_value': True}) UrlsInfo([], Sessions(), std=mstdout).echo('Foo') mstdout.write.assert_called()
def test_erase(self): mstdout = Mock(**{'isatty.return_value': True}) UrlsInfo([], Sessions(), std=mstdout).erase() mstdout.write.assert_called_once()
def test_ok(self): if sys.version_info < (3, ): self.skipTest('Unsupported Mock in Python 2.7') m = Mock() reraise_with_stack(m)() m.assert_called_once()
def test_ok(self): m = Mock() catch(m)() m.assert_called_once()