public void ShouldCauseException() { string targetFolder = "tmp"; using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); ILogger <FileDownloadService> logger = loggerFactory.CreateLogger <FileDownloadService>(); FileDownloadService service = new FileDownloadService(logger, null); FileDownload fd = Task.Run(async() => await service.GetFileFromUrl(new System.Uri("https://localhost/fake.txt"), "tmp", true).ConfigureAwait(true)).Result; Assert.ThrowsAsync <AggregateException>(() => Task.Run(async() => await service.GetFileFromUrl(new System.Uri("https://localhost/fake.txt"), targetFolder, true).ConfigureAwait(true))); Directory.Delete(Path.Combine(Directory.GetCurrentDirectory(), targetFolder)); }
public void ShouldGetFile() { string response = "A simple file"; string md5Hash = "ur2T7CGMUXLkYT+IgN4Xy8r33EktY9JrTOm0FZwhh1A="; // This hash has to change if you change the response above string targetFolder = "tmp"; using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddConsole()); ILogger <FileDownloadService> logger = loggerFactory.CreateLogger <FileDownloadService>(); var handlerMock = new Mock <HttpMessageHandler>(); handlerMock .Protected() .Setup <Task <HttpResponseMessage> >( "SendAsync", ItExpr.IsAny <HttpRequestMessage>(), ItExpr.IsAny <CancellationToken>() ) .ReturnsAsync(new HttpResponseMessage() { StatusCode = HttpStatusCode.OK, Content = new StringContent(response), }) .Verifiable(); Mock <IHttpClientService> mockHttpClientService = new Mock <IHttpClientService>(); mockHttpClientService.Setup(s => s.CreateDefaultHttpClient()).Returns(() => new HttpClient(handlerMock.Object)); FileDownloadService service = new FileDownloadService(logger, mockHttpClientService.Object); FileDownload fd = Task.Run(async() => await service.GetFileFromUrl(new System.Uri("https://localhost/fake.txt"), targetFolder, true).ConfigureAwait(true)).Result; string filename = Path.Combine(fd.LocalFilePath, fd.Name); // Clean up physical artifacts File.Delete(filename); Directory.Delete(Path.Combine(Directory.GetCurrentDirectory(), targetFolder)); Assert.True(fd.Hash == md5Hash); }