class HttpDownloaderTests(base_downloader.BaseDownloaderTests):
    def setUp(self):
        super(HttpDownloaderTests, self).setUp()
        self.dist = samples.get_repo(url=URL)
        self.downloader = HttpDownloader(self.repo, None, self.config, self.mock_cancelled_callback)

    @mock.patch('pycurl.Curl')
    def test_download_resources(self, mock_curl_constructor):
        # Setup
        indexes = self.dist.get_indexes()

        mock_curl = mock.MagicMock()
        mock_curl.getinfo.return_value = 200 # simulate a successful download
        mock_curl_constructor.return_value = mock_curl

        # Test
        resources = self.downloader.download_resources(indexes, self.mock_progress_report)

        # Verify
        self.assertEqual(3, len(resources))

        self._ensure_path_exists(resources)

        # Progress indicators
        self.assertEqual(self.mock_progress_report.query_finished_count, 3)
        self.assertEqual(self.mock_progress_report.query_total_count, 3)
        self.assertEqual(self.mock_progress_report.update_progress.call_count, 4)

    @mock.patch('pycurl.Curl')
    def test_download_resources_404(self, mock_curl_constructor):
        # Setup
        indexes = self.dist.get_indexes()
        indexes[0]['url'] = indexes[0]['url'] + '_'

        mock_curl = mock.MagicMock()
        mock_curl.getinfo.return_value = 404 # simulate an error
        mock_curl_constructor.return_value = mock_curl

        # Test & Verify
        try:
            self.downloader.download_resources(indexes, self.mock_progress_report)
            self.fail()
        except exceptions.FileNotFoundException, e:
            self.assertEqual(indexes[0]['url'], e.location)
            self.assertEqual('path' in indexes[0], False)
 def setUp(self):
     super(HttpDownloaderTests, self).setUp()
     self.dist = samples.get_repo(url=URL)
     self.downloader = HttpDownloader(self.repo, None, self.config, self.mock_cancelled_callback)