Skip to content

Commit

Permalink
Simplify logic for SafeGetPathAndQuery by converting relative URIs in…
Browse files Browse the repository at this point in the history
…to absolute ones

#163
  • Loading branch information
tintoy committed Oct 14, 2024
1 parent bb32297 commit 9ab1a6e
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/KubeClient/Utilities/UriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ namespace KubeClient.Utilities
/// </summary>
public static class UriHelper
{
/// <summary>
/// A dummy URI to be used as the base URI when dealing with relative URIs.
/// </summary>
static readonly Uri DummyBaseUri = new Uri("https://dummy-host");

/// <summary>
/// Get the path (and, if present, the query) of a URI.
/// </summary>
Expand All @@ -27,10 +32,10 @@ public static string SafeGetPathAndQuery(this Uri uri)
if (uri.IsAbsoluteUri)
return uri.PathAndQuery;

// Slightly ugly, but System.Uri doesn't attempt to parse relative URIs so we have to resort to System.UriBuilder.
UriBuilder uriComponents = new UriBuilder(uri.OriginalString);
// Slightly ugly, but System.Uri doesn't attempt to parse relative URIs so we have to convert it to an absolute URI.
Uri absoluteUri = new Uri(DummyBaseUri, relativeUri: uri);

return $"{uriComponents.Path}{uriComponents.Query}";
return absoluteUri.PathAndQuery;
}
}
}
96 changes: 96 additions & 0 deletions test/KubeClient.Tests/UriHelperTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace KubeClient.Tests
{
using Utilities;
using Xunit;

/// <summary>
/// Tests for <see cref="UriHelper"/>.
/// </summary>
public class UriHelperTests
{
static readonly Uri BaseUri = new Uri("https://localhost");

[Theory]
[InlineData("/")]
[InlineData("/?param1=value1&param2=value2")]
[InlineData("path1")]
[InlineData("path1?param1=value1&param2=value2")]
[InlineData("path1/path2")]
[InlineData("path1/path2?param1=value1&param2=value2")]
[InlineData("path1/path2/")]
[InlineData("path1/path2/?param1=value1&param2=value2")]
[InlineData("/path1")]
[InlineData("/path1?param1=value1&param2=value2")]
[InlineData("/path1/path2")]
[InlineData("/path1/path2?param1=value1&param2=value2")]
[InlineData("/path1/path2/")]
[InlineData("/path1/path2/?param1=value1&param2=value2")]
public void Can_SafeGetPathAndQuery_RelativeUri(string input)
{
Uri uri = new Uri(input, UriKind.RelativeOrAbsolute);
Assert.False(uri.IsAbsoluteUri);

string pathAndQuery = uri.SafeGetPathAndQuery();
Assert.Equal(pathAndQuery,
NormalizePath(input)
);
}

[Theory]
[InlineData("/")]
[InlineData("/?param1=value1&param2=value2")]
[InlineData("path1")]
[InlineData("path1?param1=value1&param2=value2")]
[InlineData("path1/path2")]
[InlineData("path1/path2?param1=value1&param2=value2")]
[InlineData("path1/path2/")]
[InlineData("path1/path2/?param1=value1&param2=value2")]
[InlineData("/path1")]
[InlineData("/path1?param1=value1&param2=value2")]
[InlineData("/path1/path2")]
[InlineData("/path1/path2?param1=value1&param2=value2")]
[InlineData("/path1/path2/")]
[InlineData("/path1/path2/?param1=value1&param2=value2")]
public void Can_SafeGetPathAndQuery_AbsoluteUri(string input)
{
Uri relativeUri = new Uri(input, UriKind.RelativeOrAbsolute);
Assert.False(relativeUri.IsAbsoluteUri);

Uri uri = new Uri(BaseUri, relativeUri);

string pathAndQuery = uri.SafeGetPathAndQuery();
Assert.Equal(pathAndQuery,
NormalizePath(input)
);
}

/// <summary>
/// Normalise the specified path and query for comparisons in tests.
/// </summary>
/// <param name="pathAndQuery">
/// The URI path and query components.
/// </param>
/// <returns>
/// The normalised path and query.
/// </returns>
/// <remarks>
/// System.Uri treats the path component of an absolute URI as an absolute path.
/// </remarks>
static string NormalizePath(string pathAndQuery)
{
if (pathAndQuery == null)
throw new ArgumentNullException(nameof(pathAndQuery));

if (pathAndQuery.StartsWith("/"))
return pathAndQuery;

return $"/{pathAndQuery}";
}
}
}

0 comments on commit 9ab1a6e

Please sign in to comment.