Skip to content
This repository was archived by the owner on Jun 21, 2023. It is now read-only.

Commit 675c82c

Browse files
committed
Add method for appending relative paths to urls
Sadly we can't just do this: new Uri(url, path); because you get different results if the url ends with "/" or not. So we need to make absolutely sure it _does_ end with "/".
1 parent fe26c35 commit 675c82c

File tree

3 files changed

+42
-0
lines changed

3 files changed

+42
-0
lines changed

src/GitHub.Extensions/UriExtensions.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,22 @@ namespace GitHub.Extensions
55
{
66
public static class UriExtensions
77
{
8+
/// <summary>
9+
/// Appends a relative path to the URL.
10+
/// </summary>
11+
/// <remarks>
12+
/// The Uri constructor for combining relative URLs have a different behavior with URLs that end with /
13+
/// than those that don't.
14+
/// </remarks>
15+
public static Uri Append(this Uri uri, string relativePath)
16+
{
17+
if (!uri.AbsolutePath.EndsWith("/", StringComparison.Ordinal))
18+
{
19+
uri = new Uri(uri + "/");
20+
}
21+
return new Uri(uri, new Uri(relativePath, UriKind.Relative));
22+
}
23+
824
public static bool IsHypertextTransferProtocol(this Uri uri)
925
{
1026
return uri.Scheme == "http" || uri.Scheme == "https";
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using GitHub.Extensions;
3+
using Xunit;
4+
5+
public class UriExtensionTests
6+
{
7+
public class TheAppendMethod
8+
{
9+
[Theory]
10+
[InlineData("https://github.com/foo/bar", "graphs", "https://github.com/foo/bar/graphs")]
11+
[InlineData("https://github.com/foo/bar/", "graphs", "https://github.com/foo/bar/graphs")]
12+
[InlineData("https://github.com", "bippety/boppety", "https://github.com/bippety/boppety")]
13+
[InlineData("https://github.com/", "bippety/boppety", "https://github.com/bippety/boppety")]
14+
[InlineData("https://github.com/foo/bar", "bippety/boppety", "https://github.com/foo/bar/bippety/boppety")]
15+
public void AppendsRelativePath(string url, string relativePath, string expected)
16+
{
17+
var uri = new Uri(url, UriKind.Absolute);
18+
var expectedUri = new Uri(expected, UriKind.Absolute);
19+
20+
var result = uri.Append(relativePath);
21+
22+
Assert.Equal(expectedUri, result);
23+
}
24+
}
25+
}

src/UnitTests/UnitTests.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
<Compile Include="GitHub.App\ViewModels\RepositoryCreationViewModelTests.cs" />
142142
<Compile Include="GitHub.App\ViewModels\RepositoryPublishViewModelTests.cs" />
143143
<Compile Include="GitHub.Exports\VSServicesTests.cs" />
144+
<Compile Include="GitHub.Extensions\UriExtensionTests.cs" />
144145
<Compile Include="GitHub.Extensions\URITests.cs" />
145146
<Compile Include="GitHub.Primitives\UriStringTests.cs" />
146147
<Compile Include="GitHub.UI\TwoFactorInputTests.cs" />

0 commit comments

Comments
 (0)