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

Cleanup HttpUri.PathCombine

This commit is contained in:
Mark McDowall 2016-04-03 18:47:43 -07:00
parent edea488dbe
commit 502298aab9
3 changed files with 33 additions and 15 deletions

View File

@ -0,0 +1,29 @@
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Http;
using NzbDrone.Test.Common;
namespace NzbDrone.Common.Test.Http
{
public class HttpUriFixture : TestBase
{
private HttpUri GivenHttpUri(string basePath)
{
return new HttpUri("http", "localhost", 8989, basePath, null, null);
}
[TestCase("", "", "")]
[TestCase("base", "", "/base")]
[TestCase("/base", "", "/base")]
[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)
{
GivenHttpUri(basePath).CombinePath(relativePath).Path.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,12 @@ private static string CombinePath(string basePath, string relativePath)
return basePath;
}
var newPath = "/" + relativePath.TrimStart('/');
if (basePath != null && !relativePath.StartsWith("/"))
if (basePath.IsNullOrWhiteSpace())
{
var baseSlashIndex = basePath.LastIndexOf('/');
if (baseSlashIndex == basePath.Length - 1)
{
newPath = basePath.TrimEnd('/') + newPath;
}
else if (baseSlashIndex != 0)
{
newPath = basePath.Substring(0, baseSlashIndex) + newPath;
}
return "/" + relativePath.TrimStart('/');
}
return newPath;
return basePath.TrimEnd("/") + "/" + relativePath.TrimStart('/');
}
public HttpUri SetQuery(string query)
@ -199,7 +188,6 @@ public HttpUri AddQueryParams(IEnumerable<KeyValuePair<string, string>> queryPar
return SetQuery(builder.ToString());
}
public override int GetHashCode()
{
return _uri.GetHashCode();