Downloads a file from a specific internet address to an in-memory array.
Inheritance: DownloadTask
Ejemplo n.º 1
0
        public void TestCancel()
        {
            // Prepare a very slow download of the file and monitor for a cancellation exception
            Server.Slow = true;
            var download = new DownloadMemory(Server.FileUri);
            bool exceptionThrown = false;
            var cancellationTokenSource = new CancellationTokenSource();
            var downloadThread = new Thread(() =>
            {
                try
                {
                    download.Run(cancellationTokenSource.Token);
                }
                catch (OperationCanceledException)
                {
                    exceptionThrown = true;
                }
            });

            // Start and then cancel the download
            downloadThread.Start();
            Thread.Sleep(100);
            cancellationTokenSource.Cancel();
            downloadThread.Join();

            exceptionThrown.Should().BeTrue(because: "Should throw OperationCanceledException");
        }
Ejemplo n.º 2
0
        public void TestCancel()
        {
            // Prepare a very slow download of the file and monitor for a cancellation exception
            Server.Slow = true;
            var  download                = new DownloadMemory(Server.FileUri);
            bool exceptionThrown         = false;
            var  cancellationTokenSource = new CancellationTokenSource();
            var  downloadThread          = new Thread(() =>
            {
                try
                {
                    download.Run(cancellationTokenSource.Token);
                }
                catch (OperationCanceledException)
                {
                    exceptionThrown = true;
                }
            });

            // Start and then cancel the download
            downloadThread.Start();
            Thread.Sleep(100);
            cancellationTokenSource.Cancel();
            downloadThread.Join();

            exceptionThrown.Should().BeTrue(because: "Should throw OperationCanceledException");
        }
Ejemplo n.º 3
0
        public void TestRun()
        {
            // Download the file
            var download = new DownloadMemory(Server.FileUri);
            download.Run();

            // Ensure the download was successful and the file is identical
            download.State.Should().Be(TaskState.Complete);
            download.GetData().Should().Equal(GetTestFileStream().ToArray());
        }
Ejemplo n.º 4
0
        public void TestRun()
        {
            // Download the file
            var download = new DownloadMemory(Server.FileUri);

            download.Run();

            // Ensure the download was successful and the file is identical
            download.State.Should().Be(TaskState.Complete);
            download.GetData().Should().Equal(GetTestFileStream().ToArray());
        }
Ejemplo n.º 5
0
        /// <inheritdoc/>
        public Catalog DownloadCatalog(FeedUri source)
        {
            #region Sanity checks
            if (source == null) throw new ArgumentNullException("source");
            #endregion

            if (source.IsFile) return XmlStorage.LoadXml<Catalog>(source.LocalPath);

            var download = new DownloadMemory(source);
            _handler.RunTask(download);
            var data = download.GetData();
            _trustManager.CheckTrust(data, source);
            return XmlStorage.LoadXml<Catalog>(new MemoryStream(data));
        }
Ejemplo n.º 6
0
 /// <summary>
 /// Downloads and imports a remote key file.
 /// </summary>
 /// <exception cref="WebException">The key file could not be downloaded from the internet.</exception>
 /// <exception cref="SignatureException">The downloaded key file is damaged.</exception>
 /// <exception cref="IOException">A problem occurs while writing trust configuration.</exception>
 /// <exception cref="UnauthorizedAccessException">Write access to the trust configuration is not permitted.</exception>
 private void DownloadKey([NotNull] Uri keyUri)
 {
     var download = new DownloadMemory(keyUri);
     _handler.RunTask(download);
     try
     {
         _openPgp.ImportKey(download.GetData());
     }
         #region Error handling
     catch (InvalidDataException ex)
     {
         // Wrap exception since only certain exception types are allowed
         throw new SignatureException(ex.Message, ex);
     }
     #endregion
 }
Ejemplo n.º 7
0
        private void Download(FeedUri feedUri)
        {
            SetLastCheckAttempt(feedUri);

            try
            {
                var download = new DownloadMemory(feedUri);
                _handler.RunTask(download);
                ImportFeed(download.GetData(), feedUri);
            }
            catch (WebException ex)
            {
                if (feedUri.IsLoopback) throw;
                Log.Warn(string.Format(Resources.FeedDownloadError, feedUri) + " " + Resources.TryingFeedMirror);
                try
                {
                    var download = new DownloadMemory(GetMirrorUrl(feedUri));
                    _handler.RunTask(download);
                    ImportFeed(download.GetData(), feedUri);
                }
                catch (WebException)
                {
                    // Report the original problem instead of mirror errors
                    ex.Rethrow();
                }
            }
        }
Ejemplo n.º 8
0
 public void TestIncorrectSize()
 {
     var download = new DownloadMemory(Server.FileUri, bytesTotal: 1024);
     download.Invoking(x => x.Run()).ShouldThrow<WebException>();
 }
Ejemplo n.º 9
0
 public void TestFileMissing()
 {
     var download = new DownloadMemory(new Uri(Server.ServerUri, "wrong"));
     download.Invoking(x => x.Run()).ShouldThrow<WebException>();
 }
Ejemplo n.º 10
0
        public void TestIncorrectSize()
        {
            var download = new DownloadMemory(Server.FileUri, bytesTotal: 1024);

            download.Invoking(x => x.Run()).ShouldThrow <WebException>();
        }
Ejemplo n.º 11
0
        public void TestFileMissing()
        {
            var download = new DownloadMemory(new Uri(Server.ServerUri, "wrong"));

            download.Invoking(x => x.Run()).ShouldThrow <WebException>();
        }