Example #1
0
        public async Task Start_WithMock_PauseResume_Success()
        {
            string mockId = Guid.NewGuid().ToString();

            using MockServer.MockServer mockServer = new MockServer.MockServer();
            mockServer.AddMockHandler(mockId);
            mockServer.StartAsync();

            string id = Guid.NewGuid().ToString();

            string outDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(outDir);

            var success = false;

            var cs = new CancellationTokenSource();
            var hc = new SingleSourceHTTPDownloader(new SingleSourceHTTPDownloadInfo {
                Uri = $"http://127.0.0.1:39000/{mockId}"
            });

            hc.TargetDir = outDir;
            hc.Start();

            await Task.Delay(9000);

            hc.Stop();

            await Task.Delay(2000);

            var name = hc.TargetFileName;

            hc                = new SingleSourceHTTPDownloader(hc.Id);
            hc.TargetDir      = outDir;
            hc.TargetFileName = name;
            hc.Finished      += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.Resume();

            try
            {
                await Task.Delay(Int32.MaxValue, cs.Token);
            }
            catch { }

            Assert.IsTrue(success);

            string hash1 = mockServer.GetHash(mockId);

            Assert.AreEqual(hash1, GetFileHash(hc.TargetFile));
        }
Example #2
0
        public async Task Start_WithMockAndAttachment_Success()
        {
            var name    = "filename.zip";
            var headers = new Dictionary <string, string>
            {
                ["Content-Disposition"] = $"attachment; filename=\"{name}\"",
                ["Content-Type"]        = "application/zip"
            };

            var mockId = Guid.NewGuid().ToString();

            using MockServer.MockServer mockServer = new MockServer.MockServer();
            mockServer.AddMockHandler(mockId, headers: headers);
            mockServer.StartAsync();

            string id = Guid.NewGuid().ToString();

            string outDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(outDir);

            var success = false;

            var cs = new CancellationTokenSource();

            var hc = new SingleSourceHTTPDownloader(new SingleSourceHTTPDownloadInfo {
                Uri = $"http://127.0.0.1:39000/{mockId}"
            });

            hc.Finished += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.TargetDir = outDir;
            hc.Start();
            try
            {
                await Task.Delay(Int32.MaxValue, cs.Token);
            }
            catch { }

            Assert.IsTrue(success);

            string hash1 = mockServer.GetHash(mockId);

            Assert.AreEqual(GetFileHash(hc.TargetFile), hash1);
            Assert.AreEqual(hc.TargetFileName, name);
        }
Example #3
0
        public async Task Start_OnDropout_Fails()
        {
            string mockId = Guid.NewGuid().ToString();

            using MockServer.MockServer mockServer = new MockServer.MockServer();
            mockServer.AddMockHandler(mockId);
            mockServer.StartAsync();

            string id = Guid.NewGuid().ToString();

            string outDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(outDir);

            var success = true;

            var cs = new CancellationTokenSource();

            var hc = new SingleSourceHTTPDownloader(new SingleSourceHTTPDownloadInfo {
                Uri = $"http://127.0.0.1:39000/{mockId}"
            });

            hc.Finished += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                Console.WriteLine("Test: download failed");
                success = false;
                cs.Cancel();
            };
            hc.TargetDir = outDir;
            hc.Start();
            try
            {
                await Task.Delay(3000, cs.Token);
            }
            catch { }
            mockServer.Stop();

            try
            {
                await Task.Delay(Int32.MaxValue, cs.Token);
            }
            catch { }

            Assert.IsFalse(success);
            Assert.IsFalse(hc.IsCancelled);
            var path = Path.Combine(Config.DataDir, hc.Id);

            Assert.DoesNotThrow(() => Directory.Delete(path, true));
        }
Example #4
0
        public async Task Start_WithMock_NonResumableNoContentLength_Success()
        {
            string mockId = Guid.NewGuid().ToString();

            using MockServer.MockServer mockServer = new MockServer.MockServer();
            var ret = mockServer.AddMockHandler(mockId, start: 10, end: 20);

            mockServer.NonResumable     = true;
            mockServer.HasContentLength = false;
            mockServer.StartAsync();

            Console.WriteLine("Actual file size: {0}", ret.Size);

            string id = Guid.NewGuid().ToString();

            string outDir = Path.Combine(Path.GetTempPath(), id);

            Directory.CreateDirectory(outDir);

            var success = false;

            var cs = new CancellationTokenSource();

            var hc = new SingleSourceHTTPDownloader(new SingleSourceHTTPDownloadInfo {
                Uri = $"http://127.0.0.1:39000/{mockId}"
            });

            hc.Finished += (a, b) =>
            {
                success = true;
                cs.Cancel();
            };
            hc.Failed += (a, b) =>
            {
                success = false;
                cs.Cancel();
            };
            hc.TargetDir = outDir;
            Console.WriteLine("Out path: {0}", Path.Combine(Config.DataDir, hc.Id));

            hc.Start();
            try
            {
                await Task.Delay(Int32.MaxValue, cs.Token);
            }
            catch { }

            Assert.IsTrue(success);
            Assert.IsTrue(hc.FileSize > 0);
            Assert.AreEqual(hc.FileSize, ret.Size);
            var hash1 = mockServer.GetHash(mockId);

            Assert.AreEqual(hash1, GetFileHash(hc.TargetFile));
        }