Cleanup HttpUri.PathCombine

pull/4/head
Mark McDowall 9 years ago
parent edea488dbe
commit 502298aab9

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

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

@ -144,23 +144,12 @@ namespace NzbDrone.Common.Http
return basePath; return basePath;
} }
var newPath = "/" + relativePath.TrimStart('/'); if (basePath.IsNullOrWhiteSpace())
if (basePath != null && !relativePath.StartsWith("/"))
{
var baseSlashIndex = basePath.LastIndexOf('/');
if (baseSlashIndex == basePath.Length - 1)
{ {
newPath = basePath.TrimEnd('/') + newPath; return "/" + relativePath.TrimStart('/');
}
else if (baseSlashIndex != 0)
{
newPath = basePath.Substring(0, baseSlashIndex) + newPath;
}
} }
return newPath; return basePath.TrimEnd("/") + "/" + relativePath.TrimStart('/');
} }
public HttpUri SetQuery(string query) public HttpUri SetQuery(string query)
@ -199,7 +188,6 @@ namespace NzbDrone.Common.Http
return SetQuery(builder.ToString()); return SetQuery(builder.ToString());
} }
public override int GetHashCode() public override int GetHashCode()
{ {
return _uri.GetHashCode(); return _uri.GetHashCode();

Loading…
Cancel
Save