From 2eff7b95dda10c527940d44f6aa057b31301c303 Mon Sep 17 00:00:00 2001 From: Qstick Date: Sat, 28 Dec 2019 20:26:07 -0500 Subject: [PATCH] Fixed: Catch NullRef On HDBits request if IMDB empty --- .../HDBitsRequestGeneratorFixture.cs | 85 +++++++++++++++++++ .../Indexers/HDBits/HDBitsRequestGenerator.cs | 12 ++- 2 files changed, 93 insertions(+), 4 deletions(-) create mode 100644 src/NzbDrone.Core.Test/IndexerTests/HDBitsTests/HDBitsRequestGeneratorFixture.cs diff --git a/src/NzbDrone.Core.Test/IndexerTests/HDBitsTests/HDBitsRequestGeneratorFixture.cs b/src/NzbDrone.Core.Test/IndexerTests/HDBitsTests/HDBitsRequestGeneratorFixture.cs new file mode 100644 index 000000000..5c05ced86 --- /dev/null +++ b/src/NzbDrone.Core.Test/IndexerTests/HDBitsTests/HDBitsRequestGeneratorFixture.cs @@ -0,0 +1,85 @@ +using System.Linq; +using FluentAssertions; +using Newtonsoft.Json; +using NUnit.Framework; +using NzbDrone.Common.Http; +using NzbDrone.Core.Indexers.HDBits; +using NzbDrone.Core.IndexerSearch.Definitions; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.IndexerTests.HDBitsTests +{ + public class HDBitsRequestGeneratorFixture : CoreTest + { + private MovieSearchCriteria _movieSearchCriteria; + + [SetUp] + public void Setup() + { + Subject.Settings = new HDBitsSettings() + { + BaseUrl = "http://127.0.0.1:1234/", + Categories = new[] { 1, 2 }, + ApiKey = "abcd", + Username = "somename" + }; + + _movieSearchCriteria = new MovieSearchCriteria + { + Movie = new Movies.Movie { ImdbId = "tt0076759", Title = "Star Wars", Year = 1977 } + }; + } + + private void MovieWithoutIMDB() + { + _movieSearchCriteria.Movie.ImdbId = null; + } + + [Test] + public void should_use_all_categories_for_feed() + { + var results = Subject.GetRecentRequests(); + + results.GetAllTiers().Should().HaveCount(1); + + var page = results.GetAllTiers().First().First(); + + var encoding = HttpHeader.GetEncodingFromContentType(page.HttpRequest.Headers.ContentType); + + var body = encoding.GetString(page.HttpRequest.ContentData); + var query = JsonConvert.DeserializeObject(body); + + query.Category.Should().HaveCount(2); + query.Username.Should().Be(Subject.Settings.Username); + query.Passkey.Should().Be(Subject.Settings.ApiKey); + } + + [Test] + public void should_search_by_imdbid_if_supported() + { + var results = Subject.GetSearchRequests(_movieSearchCriteria); + var imdbQuery = int.Parse(_movieSearchCriteria.Movie.ImdbId.Substring(2)); + + results.GetAllTiers().Should().HaveCount(1); + + var page = results.GetAllTiers().First().First(); + + var encoding = HttpHeader.GetEncodingFromContentType(page.HttpRequest.Headers.ContentType); + + var body = encoding.GetString(page.HttpRequest.ContentData); + var query = JsonConvert.DeserializeObject(body); + + query.Category.Should().HaveCount(2); + query.ImdbInfo.Id.Should().Be(imdbQuery); + } + + [Test] + public void should_return_no_results_if_no_imdb() + { + MovieWithoutIMDB(); + + var results = Subject.GetSearchRequests(_movieSearchCriteria); + results.GetTier(0).Should().HaveCount(0); + } + } +} diff --git a/src/NzbDrone.Core/Indexers/HDBits/HDBitsRequestGenerator.cs b/src/NzbDrone.Core/Indexers/HDBits/HDBitsRequestGenerator.cs index 3dbe3e526..3c4a2523a 100644 --- a/src/NzbDrone.Core/Indexers/HDBits/HDBitsRequestGenerator.cs +++ b/src/NzbDrone.Core/Indexers/HDBits/HDBitsRequestGenerator.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using NzbDrone.Common.Extensions; using NzbDrone.Common.Http; using NzbDrone.Common.Serializer; using NzbDrone.Core.IndexerSearch.Definitions; @@ -21,12 +22,10 @@ namespace NzbDrone.Core.Indexers.HDBits public virtual IndexerPageableRequestChain GetSearchRequests(MovieSearchCriteria searchCriteria) { var pageableRequests = new IndexerPageableRequestChain(); - var queryBase = new TorrentQuery(); + var query = new TorrentQuery(); - if (TryAddSearchParameters(queryBase, searchCriteria)) + if (TryAddSearchParameters(query, searchCriteria)) { - var query = queryBase.Clone(); - query.ImdbInfo.Id = int.Parse(searchCriteria.Movie.ImdbId.Substring(2)); pageableRequests.Add(GetRequest(query)); } @@ -35,6 +34,11 @@ namespace NzbDrone.Core.Indexers.HDBits private bool TryAddSearchParameters(TorrentQuery query, SearchCriteriaBase searchCriteria) { + if (searchCriteria.Movie.ImdbId.IsNullOrWhiteSpace()) + { + return false; + } + var imdbId = int.Parse(searchCriteria.Movie.ImdbId.Substring(2)); if (imdbId != 0)