public async Task DownloadFile_GivenAvailableDownload_WhenNoExistingFile_ShouldDownloadEntireFile() { //---------------Set up test pack------------------- using (var http = new HttpServer()) { var folder = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString()); Directory.CreateDirectory(folder); var someBytes = RandomValueGen.GetRandomBytes(100, 200); var srcPath = RandomValueGen.GetRandomString(10, 20); http.AddFileHandler((processor, stream) => processor.Path == "/" + srcPath ? someBytes : null); var sut = Create(); var linkUrl = http.GetFullUrlFor(srcPath); var expectedFile = Path.Combine(folder, srcPath); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var result = await sut.Download(linkUrl, expectedFile); //---------------Test Result ----------------------- Assert.That(result, Is.True); Assert.IsTrue(File.Exists(expectedFile)); CollectionAssert.AreEqual(someBytes, File.ReadAllBytes(expectedFile)); } }
public async Task Download_GivenUrl_WhenCanDownload_ShouldReturnData() { //---------------Set up test pack------------------- using (var server = new HttpServer()) using (var tempFile = new AutoTempFile()) { var relativeUrl = GetRandomString(); var expected = GetRandomBytes(); server.AddFileHandler((processor, stream) => { if (processor.Path != "/" + relativeUrl) { throw new Exception($"Unexpected request {processor.Path}"); // test should fail if incorrect url is used } return(expected); }); var sut = Create(); //---------------Assert Precondition---------------- //---------------Execute Test ---------------------- var result = await sut.DownloadDataAsync(server.GetFullUrlFor(relativeUrl)); //---------------Test Result ----------------------- Assert.IsTrue(result.Success); CollectionAssert.AreEqual(expected, result.Data); } }