From e7331539f0b30a14b80f0350f74e2a1981848211 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Thu, 23 Mar 2017 17:12:10 +0100 Subject: [PATCH] Fixed: Newznab default capabilities erroneously cached if indexer is unavailable. --- .../NewznabCapabilitiesProviderFixture.cs | 35 ++++++++++++++++++- .../Newznab/NewznabCapabilitiesProvider.cs | 25 ++++++++++--- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabCapabilitiesProviderFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabCapabilitiesProviderFixture.cs index b7956a212..e3a0e053c 100644 --- a/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabCapabilitiesProviderFixture.cs +++ b/src/NzbDrone.Core.Test/IndexerTests/NewznabTests/NewznabCapabilitiesProviderFixture.cs @@ -1,9 +1,12 @@ -using FluentAssertions; +using System; +using System.Xml; +using FluentAssertions; using Moq; using NUnit.Framework; using NzbDrone.Common.Http; using NzbDrone.Core.Indexers.Newznab; using NzbDrone.Core.Test.Framework; +using NzbDrone.Test.Common; namespace NzbDrone.Core.Test.IndexerTests.NewznabTests { @@ -64,5 +67,35 @@ public void should_use_default_pagesize_if_missing() caps.DefaultPageSize.Should().Be(100); caps.MaxPageSize.Should().Be(100); } + + [Test] + public void should_throw_if_failed_to_get() + { + Mocker.GetMock() + .Setup(o => o.Get(It.IsAny())) + .Throws(); + + Assert.Throws(() => Subject.GetCapabilities(_settings)); + } + + [Test] + public void should_throw_if_xml_invalid() + { + GivenCapsResponse(_caps.Replace("")); + + Assert.Throws(() => Subject.GetCapabilities(_settings)); + } + + [Test] + public void should_not_throw_on_xml_data_unexpected() + { + GivenCapsResponse(_caps.Replace("5030", "asdf")); + + var result = Subject.GetCapabilities(_settings); + + result.Should().NotBeNull(); + + ExceptionVerification.ExpectedErrors(1); + } } } diff --git a/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilitiesProvider.cs b/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilitiesProvider.cs index 9cb004f67..a3eac6d23 100644 --- a/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilitiesProvider.cs +++ b/src/NzbDrone.Core/Indexers/Newznab/NewznabCapabilitiesProvider.cs @@ -1,5 +1,7 @@ using System; using System.Collections.Generic; +using System.Net; +using System.Xml; using System.Xml.Linq; using NLog; using NzbDrone.Common.Cache; @@ -48,15 +50,30 @@ private NewznabCapabilities FetchCapabilities(NewznabSettings indexerSettings) var request = new HttpRequest(url, HttpAccept.Rss); + HttpResponse response; + try { - var response = _httpClient.Get(request); - - capabilities = ParseCapabilities(response); + response = _httpClient.Get(request); } catch (Exception ex) { - _logger.Debug(ex, string.Format("Failed to get capabilities from {0}: {1}", indexerSettings.Url, ex.Message)); + _logger.Debug(ex, "Failed to get newznab api capabilities from {0}", indexerSettings.Url); + throw; + } + + try + { + capabilities = ParseCapabilities(response); + } + catch (XmlException ex) + { + _logger.Debug(ex, "Failed to parse newznab api capabilities for {0}.", indexerSettings.Url); + throw; + } + catch (Exception ex) + { + _logger.Error(ex, "Failed to determine newznab api capabilities for {0}, using the defaults instead till Sonarr restarts.", indexerSettings.Url); } return capabilities;