Downloads a file from a specific internet address to a local file.
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 DownloadFile(Server.FileUri, _tempFile);
            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 TestRun()
        {
            // Download the file
            var download = new DownloadFile(Server.FileUri, _tempFile);
            download.Run();

            // Read the file
            var fileContent = File.ReadAllBytes(_tempFile);

            // Ensure the download was successful and the file is identical
            download.State.Should().Be(TaskState.Complete);
            fileContent.Should().Equal(GetTestFileStream().ToArray());
        }
Ejemplo n.º 3
0
 public void TestIncorrectSize()
 {
     var download = new DownloadFile(Server.FileUri, _tempFile, bytesTotal: 1024);
     download.Invoking(x => x.Run()).ShouldThrow<WebException>();
 }
Ejemplo n.º 4
0
 public void TestFileMissing()
 {
     var download = new DownloadFile(new Uri(Server.ServerUri, "wrong"), _tempFile);
     download.Invoking(x => x.Run()).ShouldThrow<WebException>();
 }