From 3c335d498e284e9a3417cf51f35d13a54137a8dc Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Sat, 31 Aug 2019 12:25:13 +0200 Subject: [PATCH] Fixed tests --- .../Http/HttpClientFixture.cs | 8 +++++++- src/NzbDrone.Common/Http/HttpClient.cs | 18 +++++++++--------- src/NzbDrone.Common/Http/HttpException.cs | 2 +- 3 files changed, 17 insertions(+), 11 deletions(-) diff --git a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs index 3ff4f1444..80741132a 100644 --- a/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs +++ b/src/NzbDrone.Common.Test/Http/HttpClientFixture.cs @@ -288,6 +288,11 @@ namespace NzbDrone.Common.Test.Http Subject.DownloadFile(url, file); File.Exists(file).Should().BeTrue(); + File.Exists(file + ".part").Should().BeFalse(); + + var fileInfo = new FileInfo(file); + + fileInfo.Length.Should().Be(307054); } [Test] @@ -295,9 +300,10 @@ namespace NzbDrone.Common.Test.Http { var file = GetTempFilePath(); - Assert.Throws(() => Subject.DownloadFile("https://download.readarr.com/wrongpath", file)); + Assert.Throws(() => Subject.DownloadFile("https://download.readarr.com/wrongpath", file)); File.Exists(file).Should().BeFalse(); + File.Exists(file + ".part").Should().BeFalse(); ExceptionVerification.ExpectedWarns(1); } diff --git a/src/NzbDrone.Common/Http/HttpClient.cs b/src/NzbDrone.Common/Http/HttpClient.cs index 0e6499866..ff4522e35 100644 --- a/src/NzbDrone.Common/Http/HttpClient.cs +++ b/src/NzbDrone.Common/Http/HttpClient.cs @@ -231,6 +231,8 @@ namespace NzbDrone.Common.Http public void DownloadFile(string url, string fileName, string userAgent = null) { + var fileNamePart = fileName + ".part"; + try { var fileInfo = new FileInfo(fileName); @@ -242,7 +244,7 @@ namespace NzbDrone.Common.Http _logger.Debug("Downloading [{0}] to [{1}]", url, fileName); var stopWatch = Stopwatch.StartNew(); - using (var fileStream = new FileStream(fileName, FileMode.Create, FileAccess.ReadWrite)) + using (var fileStream = new FileStream(fileNamePart, FileMode.Create, FileAccess.ReadWrite)) { var request = new HttpRequest(url); @@ -256,17 +258,15 @@ namespace NzbDrone.Common.Http } stopWatch.Stop(); + File.Move(fileNamePart, fileName); _logger.Debug("Downloading Completed. took {0:0}s", stopWatch.Elapsed.Seconds); } - catch (WebException e) - { - _logger.Warn("Failed to get response from: {0} {1}", url, e.Message); - throw; - } - catch (Exception e) + finally { - _logger.Warn(e, "Failed to get response from: " + url); - throw; + if (File.Exists(fileNamePart)) + { + File.Delete(fileNamePart); + } } } diff --git a/src/NzbDrone.Common/Http/HttpException.cs b/src/NzbDrone.Common/Http/HttpException.cs index d78dd5b3e..c3ad7a1fa 100644 --- a/src/NzbDrone.Common/Http/HttpException.cs +++ b/src/NzbDrone.Common/Http/HttpException.cs @@ -26,7 +26,7 @@ namespace NzbDrone.Common.Http public override string ToString() { - if (Response != null) + if (Response != null && Response.ResponseData != null) { return base.ToString() + Environment.NewLine + Response.Content; }