Added ParentUriString to get the parent site URI from any URI.

This commit is contained in:
Mark McDowall 2011-11-12 12:21:19 -08:00
parent 95cdc4b78c
commit f3f2691b4d
2 changed files with 85 additions and 1 deletions

View File

@ -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");
}
}
}

View File

@ -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);
}
}
}