1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-05 02:22:31 +01:00

Merge branch 'http-uri-combine-path' into develop

This commit is contained in:
Taloth Saldono 2016-04-06 21:34:20 +02:00
commit 96b7bd3b2b
3 changed files with 112 additions and 16 deletions

View File

@ -0,0 +1,84 @@
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Http;
using NzbDrone.Test.Common;
namespace NzbDrone.Common.Test.Http
{
public class HttpUriFixture : TestBase
{
[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)
{
var newUri = new HttpUri(basePath) + new HttpUri(relativePath);
newUri.FullUri.Should().Be(expected);
}
[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", "base/relative")]
[TestCase("base", "/relative", "base/relative")]
[TestCase("/base", "relative", "/base/relative")]
[TestCase("/base", "/relative", "/base/relative")]
[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)
{
var newUri = new HttpUri(basePath).CombinePath(relativePath);
newUri.FullUri.Should().Be(expected);
}
}
}

View File

@ -83,6 +83,7 @@
<Compile Include="Http\HttpClientFixture.cs" />
<Compile Include="Http\HttpRequestBuilderFixture.cs" />
<Compile Include="Http\HttpRequestFixture.cs" />
<Compile Include="Http\HttpUriFixture.cs" />
<Compile Include="InstrumentationTests\CleanseLogMessageFixture.cs" />
<Compile Include="LevenshteinDistanceFixture.cs" />
<Compile Include="OsPathFixture.cs" />

View File

@ -144,23 +144,34 @@ private static string CombinePath(string basePath, string relativePath)
return basePath;
}
var newPath = "/" + relativePath.TrimStart('/');
if (basePath != null && !relativePath.StartsWith("/"))
if (basePath.IsNullOrWhiteSpace())
{
return relativePath;
}
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 == basePath.Length - 1)
if (baseSlashIndex >= 0)
{
newPath = basePath.TrimEnd('/') + newPath;
}
else if (baseSlashIndex != 0)
{
newPath = basePath.Substring(0, baseSlashIndex) + newPath;
}
return basePath.Substring(0, baseSlashIndex) + "/" + relativePath;
}
return newPath;
return relativePath;
}
public HttpUri SetQuery(string query)
@ -252,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);