1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-16 11:37:58 +02:00

CombinePath now simple, uri resolve done via operator and CombineRelativePath.

This commit is contained in:
Taloth Saldono 2016-04-04 21:28:59 +02:00
parent 502298aab9
commit 3a162be265
2 changed files with 90 additions and 12 deletions

View File

@ -7,23 +7,78 @@ namespace NzbDrone.Common.Test.Http
{
public class HttpUriFixture : TestBase
{
private HttpUri GivenHttpUri(string basePath)
[TestCase("", "", "")]
[TestCase("/", "", "/")]
[TestCase("base", "", "base")]
[TestCase("/base", "", "/base")]
[TestCase("/base/", "", "/base/")]
[TestCase("", "relative", "relative")]
[TestCase("", "/relative", "/relative")]
[TestCase("/", "relative", "/relative")]
[TestCase("/", "/relative", "/relative")]
[TestCase("base", "relative", "relative")]
[TestCase("base", "/relative", "/relative")]
[TestCase("/base", "relative", "/relative")]
[TestCase("/base", "/relative", "/relative")]
[TestCase("/base/", "relative", "/base/relative")]
[TestCase("/base/", "/relative", "/relative")]
[TestCase("base/sub", "relative", "base/relative")]
[TestCase("base/sub", "/relative", "/relative")]
[TestCase("/base/sub", "relative", "/base/relative")]
[TestCase("/base/sub", "/relative", "/relative")]
[TestCase("/base/sub/", "relative", "/base/sub/relative")]
[TestCase("/base/sub/", "/relative", "/relative")]
[TestCase("abc://host.com:8080/root/file.xml", "relative/path", "abc://host.com:8080/root/relative/path")]
[TestCase("abc://host.com:8080/root/file.xml", "/relative/path", "abc://host.com:8080/relative/path")]
[TestCase("abc://host.com:8080/root/file.xml?query=1#fragment", "relative/path", "abc://host.com:8080/root/relative/path")]
[TestCase("abc://host.com:8080/root/file.xml?query=1#fragment", "/relative/path", "abc://host.com:8080/relative/path")]
[TestCase("abc://host.com:8080/root/api", "relative/path", "abc://host.com:8080/root/relative/path")]
[TestCase("abc://host.com:8080/root/api", "/relative/path", "abc://host.com:8080/relative/path")]
[TestCase("abc://host.com:8080/root/api/", "relative/path", "abc://host.com:8080/root/api/relative/path")]
[TestCase("abc://host.com:8080/root/api/", "/relative/path", "abc://host.com:8080/relative/path")]
[TestCase("abc://host.com:8080/root/api/", "//otherhost.com/path", "abc://otherhost.com/path")]
public void should_combine_uri(string basePath, string relativePath, string expected)
{
return new HttpUri("http", "localhost", 8989, basePath, null, null);
var newUri = new HttpUri(basePath) + new HttpUri(relativePath);
newUri.FullUri.Should().Be(expected);
}
[TestCase("", "", "")]
[TestCase("base", "", "/base")]
[TestCase("/", "", "/")]
[TestCase("base", "", "base")]
[TestCase("/base", "", "/base")]
[TestCase("", "relative", "/relative")]
[TestCase("/base/", "", "/base/")]
[TestCase("", "relative", "relative")]
[TestCase("", "/relative", "/relative")]
[TestCase("base", "relative", "/base/relative")]
[TestCase("base", "/relative", "/base/relative")]
[TestCase("/", "relative", "/relative")]
[TestCase("/", "/relative", "/relative")]
[TestCase("base", "relative", "base/relative")]
[TestCase("base", "/relative", "base/relative")]
[TestCase("/base", "relative", "/base/relative")]
[TestCase("/base", "/relative", "/base/relative")]
public void should_combine_base_path_and_relative_path(string basePath, string relativePath, string expected)
[TestCase("/base/", "relative", "/base/relative")]
[TestCase("/base/", "/relative", "/base/relative")]
[TestCase("base/sub", "relative", "base/sub/relative")]
[TestCase("base/sub", "/relative", "base/sub/relative")]
[TestCase("/base/sub", "relative", "/base/sub/relative")]
[TestCase("/base/sub", "/relative", "/base/sub/relative")]
[TestCase("/base/sub/", "relative", "/base/sub/relative")]
[TestCase("/base/sub/", "/relative", "/base/sub/relative")]
[TestCase("/base/sub/", "relative/", "/base/sub/relative/")]
[TestCase("/base/sub/", "/relative/", "/base/sub/relative/")]
[TestCase("abc://host.com:8080/root/file.xml", "relative/path", "abc://host.com:8080/root/file.xml/relative/path")]
[TestCase("abc://host.com:8080/root/file.xml", "/relative/path", "abc://host.com:8080/root/file.xml/relative/path")]
[TestCase("abc://host.com:8080/root/file.xml?query=1#fragment", "relative/path", "abc://host.com:8080/root/file.xml/relative/path?query=1#fragment")]
[TestCase("abc://host.com:8080/root/file.xml?query=1#fragment", "/relative/path", "abc://host.com:8080/root/file.xml/relative/path?query=1#fragment")]
[TestCase("abc://host.com:8080/root/api", "relative/path", "abc://host.com:8080/root/api/relative/path")]
[TestCase("abc://host.com:8080/root/api", "/relative/path", "abc://host.com:8080/root/api/relative/path")]
[TestCase("abc://host.com:8080/root/api/", "relative/path", "abc://host.com:8080/root/api/relative/path")]
[TestCase("abc://host.com:8080/root/api/", "/relative/path", "abc://host.com:8080/root/api/relative/path")]
public void should_combine_relative_path(string basePath, string relativePath, string expected)
{
GivenHttpUri(basePath).CombinePath(relativePath).Path.Should().Be(expected);
var newUri = new HttpUri(basePath).CombinePath(relativePath);
newUri.FullUri.Should().Be(expected);
}
}
}

View File

@ -131,7 +131,7 @@ private IList<KeyValuePair<string, string>> QueryParams
return _queryParams;
}
}
public HttpUri CombinePath(string path)
{
return new HttpUri(Scheme, Host, Port, CombinePath(Path, path), Query, Fragment);
@ -146,10 +146,32 @@ private static string CombinePath(string basePath, string relativePath)
if (basePath.IsNullOrWhiteSpace())
{
return "/" + relativePath.TrimStart('/');
return relativePath;
}
return basePath.TrimEnd("/") + "/" + relativePath.TrimStart('/');
return basePath.TrimEnd('/') + "/" + relativePath.TrimStart('/');
}
private static string CombineRelativePath(string basePath, string relativePath)
{
if (relativePath.IsNullOrWhiteSpace())
{
return basePath;
}
if (relativePath.StartsWith("/"))
{
return relativePath;
}
var baseSlashIndex = basePath.LastIndexOf('/');
if (baseSlashIndex >= 0)
{
return basePath.Substring(0, baseSlashIndex) + "/" + relativePath;
}
return relativePath;
}
public HttpUri SetQuery(string query)
@ -188,6 +210,7 @@ public HttpUri AddQueryParams(IEnumerable<KeyValuePair<string, string>> queryPar
return SetQuery(builder.ToString());
}
public override int GetHashCode()
{
return _uri.GetHashCode();
@ -240,7 +263,7 @@ public static explicit operator Uri(HttpUri url)
if (relativeUrl.Path.IsNotNullOrWhiteSpace())
{
return new HttpUri(baseUrl.Scheme, baseUrl.Host, baseUrl.Port, HttpUri.CombinePath(baseUrl.Path, relativeUrl.Path), relativeUrl.Query, relativeUrl.Fragment);
return new HttpUri(baseUrl.Scheme, baseUrl.Host, baseUrl.Port, CombineRelativePath(baseUrl.Path, relativeUrl.Path), relativeUrl.Query, relativeUrl.Fragment);
}
return new HttpUri(baseUrl.Scheme, baseUrl.Host, baseUrl.Port, baseUrl.Path, relativeUrl.Query, relativeUrl.Fragment);