From f3f2691b4d21db94a1cee4974097f450ac70fcd6 Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Sat, 12 Nov 2011 12:21:19 -0800 Subject: [PATCH] Added ParentUriString to get the parent site URI from any URI. --- NzbDrone.Core.Test/FluentTest.cs | 84 ++++++++++++++++++++++++++++++++ NzbDrone.Core/Fluent.cs | 2 +- 2 files changed, 85 insertions(+), 1 deletion(-) diff --git a/NzbDrone.Core.Test/FluentTest.cs b/NzbDrone.Core.Test/FluentTest.cs index 26d88c907..a6e7be4cc 100644 --- a/NzbDrone.Core.Test/FluentTest.cs +++ b/NzbDrone.Core.Test/FluentTest.cs @@ -80,5 +80,89 @@ namespace NzbDrone.Core.Test Console.WriteLine(dateTime.DayOfWeek); dateTime.ToBestDateString().Should().Be(dateTime.ToShortDateString()); } + + [Test] + public void ParentUriString_should_return_self_if_already_parent() + { + //Setup + var url = "http://www.nzbdrone.com"; + var uri = new Uri(url); + + //Act + var result = uri.ParentUriString(); + + //Resolve + result.Should().Be(url); + } + + [Test] + public void ParentUriString_should_return_parent_url_when_path_is_passed() + { + //Setup + var url = "http://www.nzbdrone.com/test/"; + var uri = new Uri(url); + + //Act + var result = uri.ParentUriString(); + + //Resolve + result.Should().Be("http://www.nzbdrone.com"); + } + + [Test] + public void ParentUriString_should_return_parent_url_when_multiple_paths_are_passed() + { + //Setup + var url = "http://www.nzbdrone.com/test/test2"; + var uri = new Uri(url); + + //Act + var result = uri.ParentUriString(); + + //Resolve + result.Should().Be("http://www.nzbdrone.com"); + } + + [Test] + public void ParentUriString_should_return_parent_url_when_url_with_query_string_is_passed() + { + //Setup + var url = "http://www.nzbdrone.com/test.aspx?test=10"; + var uri = new Uri(url); + + //Act + var result = uri.ParentUriString(); + + //Resolve + result.Should().Be("http://www.nzbdrone.com"); + } + + [Test] + public void ParentUriString_should_return_parent_url_when_url_with_path_and_query_strings_is_passed() + { + //Setup + var url = "http://www.nzbdrone.com/tester/test.aspx?test=10"; + var uri = new Uri(url); + + //Act + var result = uri.ParentUriString(); + + //Resolve + result.Should().Be("http://www.nzbdrone.com"); + } + + [Test] + public void ParentUriString_should_return_parent_url_when_url_with_query_strings_is_passed() + { + //Setup + var url = "http://www.nzbdrone.com/test.aspx?test=10&test2=5"; + var uri = new Uri(url); + + //Act + var result = uri.ParentUriString(); + + //Resolve + result.Should().Be("http://www.nzbdrone.com"); + } } } diff --git a/NzbDrone.Core/Fluent.cs b/NzbDrone.Core/Fluent.cs index 9d9cabdf6..fe611d249 100644 --- a/NzbDrone.Core/Fluent.cs +++ b/NzbDrone.Core/Fluent.cs @@ -49,7 +49,7 @@ namespace NzbDrone.Core public static string ParentUriString(this Uri uri) { - return uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - String.Join("", uri.Segments).Length); + return uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - String.Join("", uri.Segments).Length - uri.Query.Length); } } }